c語言前四章例題_第1頁
c語言前四章例題_第2頁
c語言前四章例題_第3頁
c語言前四章例題_第4頁
c語言前四章例題_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、第一章:分支導例:輸入生日并顯示#include <stdio.h> /*編譯預處理命令*/int main( ) /*定義了一個名字為main的函數(shù)*/int month, day; /*定義變量month和day*/ printf("Enter the month and the day of your birthday:"); scanf("%d%d", &month,&day); printf(“Your birthday is :%d月%d日n", month, day);return 0;導例:匯率換算#

2、include <stdio.h> main( ) int rmb; /*定義整型變量,存放人民幣值*/ float rate; /*定義浮點型放匯率值*/ float dollar; /*定義浮點型放美元值*/ rate=0.1642; /*對變量rate賦值*/ printf("rmb="); scanf(“%d”,&rmb); /*接收用戶輸入*/ dollar=rmb*rate; /*匯率換算*/ printf("¥%d can exchange $%.2fn",rmb,dollar); /*調(diào)用printf()函數(shù)輸出結果*

3、/導例:字符加密#include <stdio.h>main () char ch; /*定義一個字符變量ch*/  ch=getchar(); /*調(diào)用getchar()函數(shù),從鍵盤上輸入一個字符,賦值給ch*/ ch=ch+3; /*為字符ch加密 */ putchar(ch); /*調(diào)用putchar()函數(shù),輸出加密后的ch*/  putchar(n); /*輸出回車換行符*/導例:考核通過了嗎#include <stdio.h> main ()int score1,score2,score3; /*定義成績變量*/ printf("

4、;請輸入三項考核成績:");scanf("%d%d%d",&score1,&score2,&score3);printf(“第一項考核果:%dn",score1>=60);printf("第二項考核果:%dn",score2>=60);printf("第三項考核果:%dn",score3>=60);printf("綜合考核結果:%dn",(score1>=60) && (score2>=60) && (scor

5、e3>=60);算術運算符算術運算符用于各種數(shù)值運算,包括5個二元運算符:加(+)、減(-)、乘(*)、除(/)、求余(%),和2個一元運算符:自增(+)、自減(-)。判斷數(shù)字的奇偶性#include <stdio.h> int main(void) int number; printf("Enter a number: "); scanf("%d", &number); if(number % 2 = 0) printf(“偶數(shù). n"); else printf(“奇數(shù). n"); return 0;今年

6、是閏年嗎?#include <stdio.h> main( ) int year;  printf("Enter year:"); scanf("%d", &year);  if(year%4=0&&year%100!=0|year%400=0) /*閏年的條件*/ printf("YESn"); else printf("NOn"); 求解一元二次方程從鍵盤輸入一元二次方程ax2+bx+c=0的3個系數(shù)a,b,c,編程計算該方程的解,并輸出之#in

7、clude <stdio.h>#include <math.h>main()int a, b, c, s; double x1,x2 ;printf("Enter 3 integers:");scanf(“%d%d%d”, &a, &b, &c);s=b*b-4*a*c ;/*條件判斷,輸出結果*/if (s>0) x1=(-b+sqrt(s)/(2*a) ; x2=(-b-sqrt(s)/(2*a) ; printf("x1=%f, x2=%fn", x1,x2) ; else if (s=0) x

8、1=x2=(-b)/(2.0*a) ; printf("x=%fn", x1) ; else printf(“No solutionn”);求解簡單的四則運算表達式輸入一個形式如“操作數(shù) 運算符 操作數(shù)”的四則運算表達式,根據(jù)輸入的運算符,做相應運算,輸出運算結果。使用if語句實現(xiàn)四則運算# include <stdio.h>int main(void) double value1, value2; char op; /*定義字符型的變量*/ scanf("%lf%c%lf", &value1, &op, &value

9、2); if(op = '+') printf("=%.2fn", value1 + value2); else if(op = '-') printf("=%.2fn", value1 - value2); else if(op = '*') printf("=%.2fn", value1 * value2); else if(op = '/') printf("=%.2fn", value1 / value2); else printf("

10、;Unknown operatorn"); return 0;使用switch語句實現(xiàn)四則運算# include <stdio.h>int main(void) double value1, value2; char op; scanf("%lf%c%lf", &value1, &op, &value2); switch (op) case '+': printf("=%.2fn", value1 + value2); break; case '-': printf("

11、;=%.2fn", value1 - value2); break; case '*': printf("=%.2fn", value1 * value2); break; case '/': printf("=%.2fn", value1 / value2); break; default:printf("Unknown operatorn"); break; return 0;計算存款利息銀行對整存整取存款期限不同對應的存款利率也不同,鍵盤輸入存款本金和存期,計算到期時的利息及利息與本金的

12、和。當前整存整取年息利率:一年:3.25% 二年:3.75% 三年:4.25% 五年:4.75%#include <stdio.h>main( ) double money, rate, rest, total; int time;  printf("輸入本金:"); scanf("%lf",&money); printf("請輸入存期:"); scanf("%d",&time); /*使用switch語句實現(xiàn)利率的選擇*/ switch(time) case 1:

13、rate=0.0325; break; case 2: rate=0.0375; break; case 3: rate=0.0425; break; case 5: rate=0.0475; break; default: rate=0; rest=money*rate*time; total=money+rest;  printf("到期利息:%.2fn",rest); printf("本息合計:%.2fn",total);輸入三條邊,判斷它們能否構成三角形,若能則指出是何種三角形。方法一#include <stdio.h>ma

14、in()float a,b,c;printf("請輸入三條邊的值:n");scanf("%f%f%f",&a,&b,&c);if(a<(b+c)&&(b<(a+c)&&(c<(a+b) if(a =b|b=c|a=c) if (a=b&&b=c) printf("可以構成等邊三角形!n"); else printf("可以構成等腰三角形!n"); else printf("可以構成不等邊三角形!n");el

15、se printf("不能構成三角形!n");方法二#include <stdio.h>main()float a,b,c;printf("請輸入三條邊的值:n");scanf("%f%f%f",&a,&b,&c);if(b+c)<=a)|(a+c)<=b)|(a+b)<=c)printf("不能構成三角形!n");elseif(a !=b&&b!=c&&a!=c) printf("可以構成不等邊三角形!n")

16、;else if (a=b&&b=c) printf("可以構成等邊三角形!n"); else printf("可以構成等腰三角形!n");第二章:循環(huán)2.2 求解n!n! = 1×2×3×4××n用for語句#include <stdio.h>main( ) int i, n,fact; printf ("input n:");scanf ("%d", &n);fact=1;for(i=1;i<=n;i+) fact =

17、fact*i;printf(“%d! = %dn", n, fact);return 0;用while語句#include <stdio.h>main( ) int i, n,fact; printf ("input n:"); scanf ("%d", &n); fact=1; i=1; while(i<=n) fact = fact*i; i+; printf(“%d! = %dn", n, fact);return 0;2.1 求偶數(shù)和求解100以內(nèi)所有偶數(shù)的和 。sum=2+4+6+#include

18、<stdio.h>int main(void)int i, sum = 0;  for(i = 1; i <= 100; i+) if (i%2 = 0) sum = sum + i; printf(“%dn", sum); return 0; while 和 for 的比較用 forfor (i = 1; i <= n; i+) sum = sum + i; 用 whilei = 1; /循環(huán)變量賦初值while (i <= n) /循環(huán)條件 sum = sum + i; i+; /循環(huán)變量的改變2.3求解最大公約數(shù)輸入兩個整數(shù),求解這兩個數(shù)

19、的最大公約數(shù)。最大公約數(shù):是能夠同時整除這兩個整數(shù)的最大的正整數(shù)。#include <stdio.h>main( ) int num1,num2; int a,b,temp; printf("Input num1 & num2:"); scanf("%d%d", &num1, &num2) ; a=num1; b=num2; while (b!=0 ) temp = a%b ; a = b; b = temp ; if (num1!=0&&num2!=0) printf("%dn",

20、a); 2.5 統(tǒng)計一個整數(shù)的位數(shù)int main(void) int count, number; count = 0;printf("Enter a number: ");scanf ("%d", &number) ;if (number < 0) number = -number; do number = number / 10; count +; while (number != 0);printf("It contains %d digits.n", count); return 0;2.4 用格里高利公式求的

21、近似值 #include <stdio.h>#include<math.h> main( ) double item, sum, pi; int flag, i; i = 1; flag=1; sum=0; item=1; do sum = sum + item; flag = -flag ; i = i+2 ; item = flag*1.0/i ; while ( fabs (item) >= 0.0001 ); pi = sum * 4 ; printf("pi = %fn", pi); 從鍵盤輸入一批學生的成績,計算平均成績,并統(tǒng)計不及

22、格學生的人數(shù)。#include <stdio.h>Int main(void) int num; double grade, total; num = 0; total = 0;printf(“Enter grades: n"); scanf("%lf", &grade); /* 輸入第1個數(shù)*/while (grade >= 0) /* 輸入負數(shù),循環(huán)結束 */ total = total + grade; num+; if(grade<60) count+; scanf (“%lf”, &grade);if(num !=

23、 0) printf(“Grade average is %.2fn", total/num); printf("Number of failures is %dn", count); else printf(“ Grade average is 0。n"); return 0;輸入一批學生的成績,求最高分(do-while)#include <stdio.h>int main(void) int mark, max; max = -1; /* 給max賦一個小初值 */ printf(“Enter marks: "); do sc

24、anf ("%d", &mark ); if (max < mark) max = mark; while(mark >= 0); printf("Max = %dn", max);輸入一批學生的成績,求最高分(for)#include <stdio.h>int main(void) int i, mark, max, n; printf("Enter n: "); scanf ("%d", &n); printf("Enter %d marks: ",

25、n); scanf ("%d", &mark); /* 讀第一個成績 */ max = mark; /* 假設第一個成績最高分 */ for (i = 1; i < n; i+ ) scanf ("%d", &mark); if (max < mark) max = mark; printf("Max = %dn", max); return 0;輸入一批學生的成績,求最高分(while)#include <stdio.h>int main(void) int mark, max; printf

26、(“Enter marks:"); scanf ("%d", &mark);/* 讀入第一個成績 */ max = mark; /* 假設第一個成績最高分 */ while (mark >= 0) if(max < mark) max = mark ; scanf ("%d", &mark ); ; printf("Max = %dn", max); return 0;2.6 判斷素數(shù)int main(void)int i, m;printf(“Enter a number: ");sc

27、anf ("%d", &m);for (i = 2; i <= m/2; i+) if (m % i = 0) break; if (i > m/2&&m!=1 ) printf("%d is a prime number! n", m); else printf("No!n"); 求100以內(nèi)的全部素數(shù),每行輸出10個#include <stdio.h>#include <math.h> int main(void) int count, i, m, n; count =

28、0; for (m = 2; m <= 100; m+) n = sqrt(m); for (i = 2; i <= n; i+) if(m % i = 0) break; if(i > n) /* 如果m是素數(shù) */ printf("%6d", m); count+; if (count %10 = 0) /* 每行10個*/ printf(“n”); 2.7 求1! + 2! + . + 100!#include <stdio.h>int main(void)int i, j;double item, sum; sum = 0;for(i

29、= 1; i <= 100; i+) item = 1; for (j = 1; j <= i; j+) item = item * j; sum = sum + item; printf("= %en", sum);第三章:函數(shù)導例:平方根表輸出100以內(nèi)整數(shù)的平方根表,要求每行輸出10個。#include <stdio.h>#include <math.h> /數(shù)學類頭文件int main() int m,n; for (n=0;n<10;n+) printf("%7d", n); /輸出表頭 printf(

30、"n");for(m=0;m<10;m+) printf("%d",m); for (n=0;n<10;n+) printf("%7.4f" ,sqrt(m*10+n); printf("n"); return 0; math.h: 包含與數(shù)學相關的函數(shù) ctype.h:包含與字符處理有關的函數(shù)string.h:包含與字符串處理有關的函數(shù)stdio.h : 包含與輸入輸出有關的函數(shù)stdlib.h : 包含與動態(tài)分配存儲空間和數(shù)值轉(zhuǎn)換有關的函數(shù) process.h:包含與過程控制有關的函數(shù)導例:計算圓柱

31、體積#include <stdio.h>int main( void ) double height, radius, volume;double cylinder (double r, double h); printf ("Enter radius and height: ");scanf ("%lf%lf", &radius, &height);volume = cylinder (radius, height );printf ("Volume = %.3fn", volume);return 0;

32、double cylinder (double r, double h)double result;result =3.1415926 * r * r * h; return result;例 求兩個數(shù)中的最大值#include <stdio.h>void main( ) int a,b,c,max(int x, int y); scanf(“%d,%d”,&a,&b); c=max(a, b); printf(“Max is %d”,c);int max(int x, int y) int z; if(x>y)z=x; else z=y; return(z)

33、; 4.2.1導例:階乘累加和從鍵盤輸入1個整數(shù),計算1n的各個數(shù)的階乘的累加和,即1+2!+3!+n!。#include <stdio.h>double fact (int i); /函數(shù)聲明int main(void) int i, n; double sum=0; printf(“Enter 1 integers:”); scanf("%d",&n); for(i = 1; i <=n; i+ ) sum = sum + fact (i); /函數(shù)調(diào)用 printf("1!+%d! = %.0fn", n,sum); re

34、turn 0;double fact (int i) /函數(shù)定義 int j; double result = 1; for (j = 1; j<= i;j+) result = result *j ; return result ; /返回結果導例:判斷素數(shù)的函數(shù)#include <stdio.h>#include <math.h>int main(void) int count, m; int prime (int m); count = 0;for(m = 2; m <= 100; m+) if ( prime(m) != 0) printf(&quo

35、t;%6d", m ); count+; if (count %10 = 0) printf ("n"); printf ("n");prime(int m)int i,n;if(m=1) return 0;n=sqrt(m);for(i=2;i<=n;i+) if(m%i=0) return 0;return 1;輸出5之內(nèi)的數(shù)字金字塔。* 輸出數(shù)字金字塔 */#include <stdio.h>int main (void) void pyramid (int n);pyramid(5); return 0;void py

36、ramid (int n) int i, j;for (i = 1; i <= n; i+) for (j = 1; j <= n-i; j+) printf(" "); for (j = 1; j <= i; j+) printf(" %d ", i); /*前后各有一個空格 */ putchar ('n'); 第四章:數(shù)組5.1.1 導例:如何存儲和操作某班C語言課程的成績#include <stdio.h>#define N 10void main ( )/*定義一維數(shù)組并初始化,長度必須為常量*/in

37、t scoreN=82,76,69,92,53,78,80,88,65,72;int i, t;for(i=0;i<N;i+) /*顯示所有學生的成績*/ printf("%d ",scorei);printf("n");score4=60; /*修改第5名同學的成績*/t=score1; /*互換第2和第7名同學的成績*/score1=score6;score6=t;for(i=0;i<N;i+)/*再次顯示所有學生的成績*/ printf("%d ",scorei);例5-2在數(shù)組中查找一個給定的數(shù)輸入5個整數(shù),將它們

38、存入數(shù)組a中,再輸入1個數(shù)x,然后在數(shù)組中查找x,如果找到,輸出相應的下標,否則,輸出“Not Found”。#include <stdio.h>int main(void) int i, flag, x; int a5; printf(“Enter 5 integers: "); for(i = 0; i < 5; i+) scanf("%d", &ai); printf(“Enter x: "); scanf("%d", &x); flag = 0; for(i = 0; i < 5; i+

39、) if(ai = x) printf("Index is %dn", i); flag = 1; break; if(flag = 0) printf("Not Foundn"); return 0; 例 5-3 求最小值#include <stdio.h>int main(void) int i, min, n; int a10; printf(“Enter n: "); scanf("%d", &n); printf(“Enter %d integers: ", n); for(i = 0

40、; i < n; i+) scanf("%d", &ai); min = a0; for(i = 1; i < n; i+) if(ai < min) min = ai; printf("min is %d n", min); return 0;求最小值及下標#include <stdio.h>int main(void) int i, index, n; int a10; printf(“Enter n: "); scanf("%d", &n); printf(“Enter %

41、d integers: ", n); for(i = 0; i < n; i+) scanf("%d", &ai); index = 0; for(i = 1; i < n; i+) if(ai < aindex) index = i; printf("min is %dtsub is %dn", aindex, index); return 0;導例5.1.2 選擇法排序利用選擇排序方法將5.1.1導例中全班同學C語言課程成績按照從低到高的順序排列。for( i =0; i < n -1; i +)k = i; /*查找最小元素的下標*/for( j = i +1; j < n; j +)if( aj< ak)k = j; if( k != i)/*將ak和ai交換*/t = ak;ak = ai;ai = t;二分法查找 (程序段)int binarySearch(int a, int n, int x) int low = 0; int high = n - 1; while(lo

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論