C程序設(shè)計(jì)第四版譚浩強(qiáng)習(xí)題例題_第1頁
C程序設(shè)計(jì)第四版譚浩強(qiáng)習(xí)題例題_第2頁
C程序設(shè)計(jì)第四版譚浩強(qiáng)習(xí)題例題_第3頁
C程序設(shè)計(jì)第四版譚浩強(qiáng)習(xí)題例題_第4頁
C程序設(shè)計(jì)第四版譚浩強(qiáng)習(xí)題例題_第5頁
已閱讀5頁,還剩45頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、第7章函數(shù)tiincludeOint main()void print_star ();void print_message():print_star ();print_message ();print_star ();return 0;void print_star()printf(“*n);void print_message()printf(How do you do!n);xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxHow do you do?從從從冥夫夫夫夫夫關(guān)夫夫冥冥*獎(jiǎng)冥*冥*冥冥冥冥哭冥龔冥箕彼XPress any key to continueint

2、 max(int x,int y)int z;z二xyx:y;return(z);ttincludeOint main ()int max(int x,int y);int a, b, c;printf (please enter two integer number:,z);scanf (%d, %d, &a, &b);c二max (a, b);printf(max is %dn, c);return 0:please enter two integer number: 1 2,3坪 max is 12Press any key to continue例ftincludeO int main

3、()int max(float x,float y);f loat a, b;int c;printf(please enter two integer number:);scanf (%f, %f, &a, &b);c=max (a,b);printf(max is %dn, c);return 0:int max(float x,float y)float z;z二xyx:y;return(z);please enter two in teger n umber- :1 5,2 G max is 2Press any key to continue.例tiincludeOint main(

4、)float add (float x,float y):f loat a, b, c;printf(please enter a and b:);scanf (,z%f, %f, &a, &b);c=add(a, b);printf(sum is %fn, c);return 0;float add(float x,float y)float z;z二x+y;return (z);please enter a “nd b:36.6.5 sum is 10.100000P“g any key to continue例ttincludeOint main()int max4(int a, int

5、 b, int c,int d);int a, b, c, d, max;printf (please enter 4 interger number:,z):scanf (,z%d %d %d %d,z, &a, &b, &c, &d);int max4(int a, int b, int c, int d);int a, b, c, d, max;printf(please enter 4 interger number:);scanf (%d %d %d %d”, &a, &b, &c, &d);max=max4 (a, b, c, d);pri ntf (max 二dr/ max);r

6、eturn 0;int max4(int a, int b, int c,int d)int max2(int a, int b);return(max2(max2(max2(a, b), c), d);int max2(int a,int b)return(aba:b);例ttincludeOint main()int age(int n):printf age :%dnz,, age (5);return 0:int age (int n)int c;if(n=l) c=10;else c二age(n-l)+2;return(c);NO.5,age:18P廠gss any key to c

7、ontinuw例遞歸方法求n!ttincludeOint main ()int fac(int n);int n, y;printf(input a interger number:);scanf&n);y二fac(n);printf (%d! =%dn, n, y);return 0;int fac(int n)int f;if(nCA-BC-BA-CB-AB-CA-Cn+1);Press any key to continueint max(int x, int y)return(xyx:y);enter 10 interger numbers: 4 7 0 -3 H 34 67 曲2 3

8、1 -71The largest number is G7 it is the 7th numb廠.Press anu keu to continue例ttincludeOint main ()float average (float array10);float score10, aver;int i;printf(“enter 10 scores:n);for(i=0;i10;i+)scanf (/z%f,z, &score i);aver=average(score);pri ntf (z average score isaver);return 0:float average(floa

9、t array10)int i;float aver, sum二array0;for (i=l;i10;i+)sumsum+arrayi;aver二sum/10;return(aver);enter 10 scores:1GG 56 78 98 67.5 99 54 88.5 76 58average score is 了了.50Press 曰ny key to continue.例ttincludeOint main ()float average(float array10, int n):f loat scorel 5 = , 97, 60, 55;float score210 = ,

10、99, 77, 54, 60,; printf (The average of class A is %n,z, average (scorel, 5); printf (The average of class B is %n,z, average (score2, 10): return 0;float average(float array10,int n)int i;float aver, sum二array0;for (i二l;in;i+)sumsum+arrayi;aversum/n;return(aver);The average of class A is 80.40The a

11、uerage of class B is 了8.20 Press any key to continue例ttincludeOint main()void sort (int array, int n);int a10,i;printf(enter array:n);for(i=0;i10;i+)scanf (%d,&ai);sort(a, 10);printf (The sorted array: n,/);for(i=0;i10;i+)printf (,z%d ,ai);printf(n);return 0;void sort(int array ,int n)int i, j, k, t

12、;for(i=0;in-l;i+)k=i;for(j=i+l;jn;j+)if (arrayjarraykJ)k=j;t=arrayk;arrayk=arrayi;array i=t;wnter array:45 2 9 0 7 54 12 5 GG 33 The sorted array:-3 0 2 5 9 12 33 45 54 66 Press any key to continue.ftincludeOint main ()int max_value(int array 4);int a3 4 = 1,3, 5, 7, 2, 4, 6, 8, 15,17,34, 12;printf

13、(Max value is %dn,z, max_value (a):return 0;int max_value(int array4)int i, j, max;max二array0_ 0;for(i=0;i3;i+)for(j=0;jmax) max二arrayij;return(max);Max ualuG is 34Press any key to continue例ttincludeO float Max=0, MinO;int mainOfloat average (float array, int n):float ave,score10;int i;printf(Please

14、 enter 10 score:);for(i=0;i10;i+)scanf, &score i);ave=average(score, 10);printf (max 二nminnaverage二n, Max, Min, ave); return 0;float average(float array,int n)int i;float aver, sum二array0;Max二Min二array0;for(i=l;iMax) Max=arrayLi;else if(arrayiba:b;return(c);max=8Press any key to continue.ttincludeO

15、int main()int f(int);int a二2, i ;for (i二0;i3;i+)printf(%dn, f(a);return 0:int f(int a)auto int b=0;static c二3;b二b+l;c二c+1;return(a+b+c);789Preos any key to continue1ttincludeOint main ()int fac(int n);int i;for (i=l;iBA:B;if(Cm)m=C;return(m);Please enter three interger numbers37 67 12max is 67Press

16、any key to continu例ttincludeOint A;int main()int power(int);6UH(D4X(D 亠oUJEnpQJ:二P dv、戈泰芯鑒*爰、)Tupd-(lu) xomodHP(。q、5p 竄p藍(lán)爰、)3pu=d-q*vu。二 Incaq.u(D、) J-PUIJCI-lu p 2uq-PUIeprqou 耳ODCCOO 04 Rom Rumr-6L*exKCL 62僉TTeL 30d S4-HpueoLQqEnuQXZ4 GI4ua-(A) UJmpoJ:U*A(+U2二 ).103二 HA.Hpu-rH(u pul) Ho占 OQ 4u-rdi

17、nt mainOextern void enter_string(char str);extern void delete_string(char strL,char ch);extern void print_string(char str):char c,strL80;enter_string(str);scanf&c);delete_string (str,c);print_string(str);return 0;void enter_string(char str 80)gets (str);void delete_string(char str ,char ch)int i, j;

18、for(i=j=0;striU 0, ;i+)if (str i !=ch)strj+=stri;strj二0;void print_string(char str)printf (%sn, str);This io a C programThisisaCprogramPress any key to continue習(xí)題1.ftincludeOint divisor (x,y)int r;if(x0) disci (a,b,c);else if(fabs(d)=le6) disc2 (a,b,c);else disc3(a,b,c);return 0;請(qǐng) TAa,b,c:1 .0,3.0,2

19、.0 方程有兩不等實(shí)杈:x1 -1 .00,x2-2.00 Press any key to continue了稈有兩相等氓棍:X1=XZ1 .00Press any key to oontinue方程有兩不等實(shí)根:Xl=-0.17+ 9.99i,x2=-0.17- 0.991 Press any key to continue3.tiincludeOttincludeOvoid f (int a)int i, n二(int)sqrt (a),t;for(i=2, t=l;i=n;i+)if(a%i=O) t 二0;printf (,z這是一個(gè)合數(shù).n) ;break;if(t) printf

20、CS是一個(gè)素?cái)?shù).n);int main ()int a;printf (請(qǐng)輸入一個(gè)正整數(shù)a: );scanf&a);f (a);return 0;請(qǐng)輸入一個(gè)正整數(shù)a: 23這是個(gè)去數(shù).Press any key to continuq請(qǐng)輸入一個(gè)正整數(shù)a:102 這是一個(gè)合藪Pss any key to continuettincludeO void f(int ai33J)4.int i, j, t;for(i=0;i=2;i+)for(j=0;ji;j+)aij二 aji;aji二 t;void main ()int i, j,a3 3 = 1, 2, 3, 4, 5, 6, 7, 8, 9

21、;for(i=0;i=2;i+)for(j=0;j=2;j+)printf(%5d, aij);printf (n);printf(n);f (a);for(i=0;i=2;i+)for(j=0;j=2;j+)printf (%5d, ai j); printf(n);2 5 8 u- 5 6continue,23Press5.ttincludeOttincludeOvoid f(char a)int i, t;char b:t=strlen(a);for(i=0;i=t/2;i+)b=ai;aEi=at-i-l: void main ()char ch80;gets(ch); f(ch);

22、puts(ch);I miss you so much,and you? ?uoy dna,hcum os uoy osim I Press any key to continuo.6.tiincludeOvoid f (char a, char b )int i, j;for(i=0;ai!= 0* ; i*);for(j=0;bj!= 0J ;i+, j+) ai二bj;void main ()char chi 80=,How do you do!, ch2二Fine, thank you, and you;f (chi, ch2);puts(chi);How do you do?Fine

23、,thank you T and you? Press any key to continue-7.ttincludeOvoid f (char a80, char b40)int i, j;for(i=0, j=0;ai!= 0 ;i卄)辻(ai二二A|ai=E | ai=I丨 | ai=0 丨 | ai=U丨 | ai=,a |ai=e|ai=iT ai=,o, | ai=u)bj=ai;j卄;bj二0;int main ()char chi80二How are youFine,thank you,and you,ch240;f (chi, ch2);puts(ch2);return 0;

24、oaeouieaouaouPress any key to continue8.ttincludeOvoid f (int n, int ch)ch0=n/1000;ch1 = (n-ch01*1000)/100;ch2=(n-ch0*1000-chl*100)/10;ch3=n%10;void main ()int n, i, ch4;printf C輸入一個(gè)四位整數(shù):n);scanf&n);f(n, ch);for(i=0;i二A&ci二Z) upp+;else if (c i =,a &c i二O&ci二9) num+;else if(ci=, ) space+;else other+;

25、void main ()char ch80;printf C輸入一句英文,限80字符內(nèi):n);gets (ch);f (ch);printfC大寫字母%d個(gè),小寫字母%(1個(gè),數(shù)字%d個(gè),空格%d個(gè),其他d 個(gè)upp, low, num, space, other);輸入一句英文,限80口符內(nèi):I miss you so much,and you?大寫宇母1個(gè),小寫宇母19個(gè),數(shù)宇0個(gè),空格5個(gè),其他2個(gè)Press anu keu to continue10.ftincludeOvoid f (char a100,char b4030)int i, m, n, k, g;for(i二m二n二k

26、=g=0;ai!二0 ;i+)if (ai= a &ai=A&aiV Z)請(qǐng)輸入一句英文:Nihilumbra is my fauorite mobile game 最長的單詞為?NihilumbraP廠esskey to continuettincludeO void f(char a10)int i, j;char ch;for(i=0;i=8;i+)for(j=i+l;jaj)ch二ai;ai=aj;aj二 ch;void main ()char a10;printfC請(qǐng)輸入10個(gè)字符:n);gets(a);f (a);puts(a);請(qǐng)輸入10個(gè)宇符:忡$沁冷。Press any ke

27、y to continue,12.ttincludeO float f (float a, float b,float c, float d, float x)float z;z二x-(a*x*x*x+b*x*x+c*x+d)/(3*a*x*x+2*b*x+c);return (z);void main ()float a, b, c, d, x;printf (請(qǐng)輸入 a, b, c, d:n);scanf (%f, %f, %f, %f, &a, &b, &c, &d);printf (方程為f*x*x*x+%f*x*x+%f*x+%f二0n,a, b, c, d);while(a*x*x

28、*x+b*x*x+c*x+dle-6)x二f (a, b, c, d, x);printf (”方程在1附近的根為:%fn,z, x);請(qǐng)輸 Aa.b.c.d:1,2,3,q方程為1 000000xxxxxx+2 00OOOOxxxx+-3 0OOOOOxx+2t. 000000 = 0 方程在1附近的根為-1-728395Press any key to continue.ftincludeO float p (int n, float x)13.if (n0)y=l;else if(n=l)y=x;else y= (2*n-l) *x*p(n-1, x) -(n-l)*p(n-2, x)

29、/n; return (y);void main ()f loat x, y;int n;printf (請(qǐng)輸入正整數(shù)n和實(shí)數(shù)x: n);scanf (“%u, %f ”, &n, &x);y二p (n, x);printf (”%fn, y);請(qǐng)輸入正整數(shù)n和實(shí)數(shù)x:154.000000Press eny key to continue14.tiincludeOvoid fO(double a.10 5)int i,j;double sum, aver;for(i=0;i=9;i+)sum=0, aver=0;for(j=0;j=4;j+)sum+=aij;%分,i+1, aver);ave

30、r=sum/;printf (第%d夕學(xué)生的平均分為:void fl (double a105)int i, j;double sum, aver;for(j=0;j=4;j+)sum=0, aver=0;for(i=0;i=9;i+)sum+=aij;aver=sum/;printf (第%d門學(xué)科的平均分為:%分,j+1, aver);void f2(double a105)int i, j, s, t;double max:for(j=0;j=4;j+)max=0;for(i=0;i=9;i+)if(maxaijj)max=aijj;s=i;t=j;printf C第d門學(xué)科的最高分為第

31、%d學(xué)生:%分11, t+1, s+1, max);void f3(double a105)int i, j;double s, t, y;for(i=0;iU9;i+)s二t二y=0;for(j=0;j i+1, y);void main ()doublea10 5 = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 2& 29, 30, 31, 32, 33, 34, 35, 36, 37 ,3& 39, 40, 41, 42, 43, 44

32、, 45, 46, 47, 4& 49, 50;f0 (a);fl(a);f2(a);f3(a);第1名學(xué)生的平均分為 第2名學(xué)生的平均分為 均分為 均分為 均分為 均分為 均分為 為分為 均分為3分80分 13.0分 18 &分 23&分. 28 &分 330分 380分 43 0448.0; 23.5分 24 5分 25.5分 26.5分 275分第3名學(xué)生的 笫耳名學(xué)生的平 笫5名學(xué)生的平 笫6名學(xué)生的平 笫7名學(xué)生的平 笫8名學(xué)生的平 第9名學(xué)生的平 第名學(xué)生的平均分為: 第1門學(xué)科的平均分為: 第2門第3門 第4門 第5門 第1門 第2門 第3門 第4門 第5門學(xué)科的最高分為第10學(xué)

33、生 第1名學(xué)生的平均分方差為: 第2名學(xué)生的平均分方差為: 第3名學(xué)生的平均分方差為? 第4名學(xué)生的平均分方差為? 第5名學(xué)生的平均分方差為? 第6名學(xué)生的平均分方差為? 第了名學(xué)生的平均分方差為? 第8名學(xué)生的平均分方差為! 第9名學(xué)生的平均分方差為: 第W名學(xué)生的平均分方差為: Press anu keu to continue笫6名學(xué)均分為 均分為 均分為15.學(xué)科肉 學(xué)科的 學(xué)科的 學(xué)呆的平均分為 學(xué)芒的最鬲分為第2學(xué)生 學(xué)儀的最鬲分為第W學(xué)生 學(xué)科的最高分為第W學(xué)生 學(xué)科的最高分為第W學(xué)生ftincludeOtiincludeO46.0分47.0448.0449.050.042.0

34、2.0 2 0 2.0 2.0 2.0 2.02.02.02.0void f(int a10, char b10 20)int 1, j, t;char ch20;for(i=0;iU8;i+)for(j=i+l;j=9;j 卄)t=ai;ai=aj;aj二t;strcpy (ch, bi);strcpy(bi, bj);strcpy(bLj,ch);void g(int k, int a10, char b1020)int i, m=l;if(ka4)for(i=9;i=5;i-)if(k=ai)printf(%d %sn, ai, bi); m二 0;elsefor(i=0;i=4;i+)

35、if (k=ai)printf (%d %sn, ai, bi) ;m二 0;if (m) printfC輸入職工號(hào)錯(cuò)誤n);void main ()inti, k,a10 = 2411, 2123, 2135, 2147, 2169, 2172, 2184, 2196, 2108, 2110;char b10 20 = f馬云,李娜,張靚穎,吳彥祖,劉徳華,孫 燕姿,田震,那英,梁朝偉,劉悴;for(i=0;i=9;i+)printf (”d %sn, ai, bi);printf(n);f (a, b);for(i=0;i=9;i+)printf (z,%d %sn, ai, bi);printf(

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論