C語言程序設計教程答案_第1頁
C語言程序設計教程答案_第2頁
C語言程序設計教程答案_第3頁
已閱讀5頁,還剩46頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、C 語言程序設計教程習題答案總主編 楊路明主審 郭浩志文件摘自 CSDN習題 11、算法描述主要是用兩種基本方法: 第一是自然語言 描述,第二是使用專用工具進行算法描述2、c 語言程序的結構如下:1、c 語言程序由函數(shù)組成, 每個程序必須具有一個 main 函數(shù)作為程序的主控函數(shù)。2、/* 與 */ 之間的內容構成 c 語言程序的注釋部 分。3、用預處理命令 #include 可以包含有關文件的信息。4、大小寫字母在 c 語言中是有區(qū)別的。5、除 main 函數(shù)和標準庫函數(shù)以外, 用戶可以自己 編寫函數(shù),程序一般由多個函數(shù)組成,這些函數(shù)制定 實際所需要做的工作。例如: void main()i

2、nt a,b,c,s; a=8;b=12;c=6; s=a+b*c; printf(s=%dn,s);3、c 語言的特點:1、c 語言具有結構語言的特點, 程序之間很容易實 現(xiàn)段的共享;2、c 語言的主要結構成分為函數(shù), 函數(shù)可以在程序 中被定義完成獨立的任務,獨立地編譯成代碼,以實 現(xiàn)程序的模塊化。3、c 語言運算符豐富,運算包含的范圍很廣;4、c 語言數(shù)據(jù)類型豐富。5、c 語言允許直接訪問物理地址, 即可直接對硬件 進行操作,實現(xiàn)匯編語言的大部分功能;6、c 語言語法限制不太嚴格,程序設計自由度大, 這樣是 c 語言能夠減少對程序員的束縛;7、用 c 語言編程,生成的目標代碼質量高,程序

3、執(zhí)行效率高,可移植性好;4、合法標識符: AB12 、leed_3、EF3_3、_762、PAS、 XYZ43K2不合法標識符: a*b2 、8stu、D.K.Jon 、if 、ave#xy、 #_DT5 、 C.D5、F2:將當前編輯器中文件存盤F10:調用主菜單F4:程序運行到光標所在行Ctrl+F9: 當前編輯環(huán)境下,進行編譯、連接且運行 程序;Alt+F5: 將窗口切換到 DOS 下,查看程序運行結果6、(1) :welcome youvery good(2) :please input three number;5,7,8max number is:87、 main8、User sc

4、reen、 Alt+F59、標識符必須是字母或下劃線開頭, 大小寫字母含義 不同。由數(shù)字、字母和下劃線組成;關鍵字是一種語言中規(guī)定具有特定含義的標識符。 關鍵字不能作為變量或函數(shù)名來使用,用戶只能根據(jù) 系統(tǒng)的規(guī)定使用它們。10、選擇主菜單 File 項下拉子菜單中 Save 項或直接按F2 鍵存盤。習題 21、符合 C 語法規(guī)定的常數(shù)為: 0x1e、abn 、1.e52、(1):錯誤如下: int x,y=5,z=5,aver;x=7;aver = (x+y+x)/3;結果如下:AVER=5(2):錯誤如下:char c1=a,c2=b,c3=c;printf(a=%db=%cendn,a,b

5、);結果如下:a=3b=Aendaabcc abc3、4、(1):9,11,9,10 (2):3,1,0,0(3) :11,19,31,15、(1):0(2):0(3):9.500000(4) :90(5) :10(6) :10(7) :65(8) :4(9) :4.500000(10) :1(11) :0(12) :20(13) :06、(5)7、求 x 的絕對值8、c(max=ab?a:b)?c:max;9、B10、D習題 31、輸入函數(shù) scanf 的參數(shù)錯誤,應該為:scanf(%f,&k);2、|1234 1234 |3、ff104、1,3,15、原字符串左邊加空格再加字符串本省,

6、字符個數(shù)總和為 5 個6、scanf(%d,%d,%c,%c,&a1,&a2,&c1,&c2);7、printf(a+b=%dn,a+b);printf(a-b=%dn,a-b);printf(a*b=%dn,a*b);printf(a/b=%dn,a/b);printf(float)a/b=%fn,(float)a/b); printf(a%b=%dn,a%b);8、void main()float r;float s,c;printf(please input the number:);scanf(%f,&r);if(r=0)s = 3.14*r*r;c = 2*3.14*r;printf

7、(s = %f, c = %fn,s,c);elseprintf(you input number is error!);9、void main()int n;printf(please input the number:); scanf(%d,&n);if(n=100 & n c | a+c b | b+c a)s = (a+b+c)/2;Area = sqrt(s*(s-a)*(s-b)*(s-c); printf(%fn,Area);elseprintf(you input the number is error!n);習題 41: 02: 203:(x20)|(x-100)4: *a=

8、25,b=14,c=16*5: 376: if(a=b) printf(1);else printf(2);7、#includevoid main() char a,b,t1,t2; scanf(%c,%c,&a,&b); t1=ab?a:b;t2=ab?a:b; if(t1-t2)%2=0)printf(%c,%c,a+1,b+1); else printf(%c,%c,a-1,b-1); getch();8、#includevoid main() int temp1=0,temp2=0,x,y,i=1; printf(Please input (x,y): ); scanf(%d,%d,&

9、x,&y); while(i*y) shang=%d,yushu=%d,x,y,temp2,x-y*temp2); getch();9、 #include void main() float x,y,m=0,n=0; scanf(%f,%f,&x,&y); n=(x-2)*(x-2); m=(y-2)*(y-2); if(m+n)=1) printf(%.3f,%.3f)In the yuan,x,y); elseprintf(%.3f,%.3f)out of the yuan,x,y); getch();10、 #include void main() int temp=0,month,ye

10、ar; printf(Please input (year,month):);scanf(%d,%d,&year,&month); if(year%400=0)|(year%4=0&year%100!=0) temp=1;if(month=2) if(temp)printf(%d year %d month have 29 ,year,month);else printf(%d year %d month have 28 ,year,month);else if(month%2=0)printf(%d year %d month have 30 ,year,month);else printf

11、(%d year %d month have 31 ,year,month); getch();11、 switch(a/10) case 5:m=4;break;case 4:m=3;break;case 3:m=2;break;case 2:m=1;break; default:m=5;12、 方法一:#includevoid main() int x,y;scanf(%d,&x); if(x-5) y=x-1;else if(x=0)y=x;else if(x0&x8) y=x+1;printf(%d,y);getch();方法二:#includevoid main() int x,y;

12、scanf(%d,&x);if(x-5) if(x=0)y=x;else if(x0&x8) y=y=x+1; else y=x-1;printf(%d,y);else printf(Input error!);getch();方法三:#includevoid main() int x,y,i;scanf(%d,&x);if(x-5) if(x=0)i=1;else if(x0&x8) i=2;else i=3;else i=4;switch(i) case 1:y=x;printf(%d,y);break; case 2:y=x+1;printf(%d,y);break; case 3:y=

13、x-1;printf(%d,y);break; case 4:printf(Input error!);break;getch();習題 51、 void main()int n,value;int i,count=0;float average = 0;long int sum = 0;scanf(%d,&n);for(i = 0; i = A & ch = a & ch = 0 & ch = 9)sz+;ch = getchar();printf(zm = %d ; sz = %dn,zm,sz);void main()long i_value;int sum = 0;int temp =

14、 0; scanf(%ld,&i_value);if(i_value = A & ch = a & ch 500)break;printf(the %d years later complete!n,count);6、void main()int i,temp=0,bit=0;for(i = 1 ; i = 999; i+)if(i%3 = 0)temp = i;for(;temp != 0;)bit = temp%10; if(bit = 5)printf(%d,i);break; temp = temp/10;printf(b n);7、#include int main(void)int

15、 steps = 29, i = 1;while ( steps % 7 )steps = (30 * +i) - 1;printf(Total steps: %dn, steps);return 0;8、 main() int i,j,k,n;printf(the narcissus number is:n); for(n=100;n1000;n+)i=n/100; j=n/10-i*10;k=n%10;if(i*i*i+j*j*j+k*k*k=n) printf(%dn,n); 9、 main() float i,j,temp,n,sum;int t;i=2;j=1;sum=0; scan

16、f(%d,&t);for(n=1;n=t;n+) sum=sum+i/j;temp=i;i=i+j;j=temp;printf(2/1+3/2+5/3+8/5+13/8+.=%fn,sum);10、 void main() int nWidth,a,b; scanf(%d,&nWidth);for(a=(nWidth%2);a=nWidth;a+=2) for(b=0;b=nleft&bnright) chOut =*;printf(%c,chOut);printf(n);11、 void main() int i=1,j=1; for(i=1;i=9;i+) for(j=1;j=i;j+)

17、printf(%d*%d=%d,i,j,i*j);printf(n) 12、 #include stdio.h void main() char c;/count1 是正數(shù)的個數(shù) /count2 是負數(shù)的個數(shù) int count1=0,count2=0;/sum1 是正數(shù)之和/sum2 是負數(shù)之和 int sum1=0,sum2=0; int flage = 0;c=getchar();while(c != *)if(c = -)flage = 1; if(flage = 0)sum1+=(c-48);count1+; elseif(c != -)sum2-=(c-48);flage=0;co

18、unt2+; c = getchar();printf(%d,%dn,count1,count2); printf(%f,%fn,sum1/count1,sum2/count2);13、 void main() int i,j;int s = 0;for(i = 100 ; i = 999; i+)s = 0;for(j=1; j i-1 ; j+)if( i%j = 0 ) s = s + j;if(s = i)printf(%5d,i);14、#include #include void main()int n;long k = 1;float e = 1;n = 1;clrscr();w

19、hile ( fabs(1.0/k) = 0.000001)n+;e = e+ 1.0/k;k = k * n;printf(%fn,e);15、 #include math.h main() float x0,x1,x2,f0,f1,f2;x1=-10; f1=2*x1*x1*x1-4*x1*x1+3*x1;x2=10;f2=2*x2*x2*x2-4*x2*x2+3*x2;do x0=(x1+x2)/2; f0=2*x0*x0*x0-4*x0*x0+3*x0; if(f0*f1)1e-6);printf(2*x*x*x-4*x*x+3*x=0n);printf(the root is %fn

20、,x0);習題 61、#includeint divisor(int a,int b) int r;while(r=a%b)!=0) a=b;b=r; return b;int multiple(int a,int b) int d; d=divisor(a,b); return a*b/d;void main() int a,b,c,d; printf(intput (a,b): ); scanf(%d,%d,&a,&b); c=divisor(a,b); d=multiple(a,b); printf(ndivisor=%dttmultiple=%d,c,d);2、#includevoid

21、 tongji(char a) int b3=0,0,0,i=0; while(ai!=0) if(ai=65)|(ai=97) b0+;else if(ai=48) b1+;elseb2+;i+; printf(zimu have: %d; shuzi have: %d; qita have: %d,b0,b1,b2); getch();void main() char a100; printf(Please input a string:);gets(a); tongji(a);3、#includeint flower(int n) int x=0,i,j,k;i=(n%10);j= (n

22、/10%10); k=(n/100); x=i*i*i+j*j*j+k*k*k; if(x=n) return 1; else return 0;void main() int i,n; printf(Please intput n: ); scanf(%d,&n); if(n999|n100)printf(Input error!); else for(i=100;in;i+) if(flower(i)printf(%d ,i);getch();4、#include#define SWAP(a,b) t=b;b=a;a=t;main() float x,y,t;printf(Enter tw

23、o number (x,y): ); scanf(%f,%f,&x,&y);SWAP(x,y); printf(nnExchanged:x=%f,y=%f,x,y); getch();5、#includeint fib(int n) int p; if(n=0) p=0; else if(n=1) p=1; else p=fib(n-1)+fib(n-2); return p;void main() int n;printf(Please input fib: ); scanf(%d,&n); printf(nnfibonacci=%d,fib(n);6、#includelong fac(in

24、t n) long f;if(n=0) f=1;else f=n*fac(n-1);return f;void main() int m,n;long f;printf(Please input (m,n): ); scanf(%ld,%ld,&m,&n); f=fac(n+m)+fac(n); printf(nnfibonacci=%ld,f);7、#includevoid list() int i,j;for(i=1;i=9;i+) for(j=1;j=i;j+) printf(%d*%d=%d ,i,j,i*j);printf(n);void main() list();8、#inclu

25、deint he(int n) int i,s=0;for(i=1;in;i+) if(n%i=0)s+=i;return s;void main() int i,j;for(i=1;i30000;i+)for(j=1;j30000;j+) if(he(i)=j)&(he(j)=i)&(i!=j) printf(%d%d,i,j);9、#include#define max 100 struct work long sid;char name15;workermax;int size=0;struct work *set_list() do printf(nnPlease input (sid

26、,name): ); scanf(%ld,%s,&workersize.sid,); size+;while(workersize-1.sid!=0);return worker;void paixu(struct work a) int i,j,k;long t;char v15;for(i=0;isize-1-2;i+) k=i; for(j=i;jaj.sid)k=j; if(k!=i) t=ai.sid; ai.sid=aj.sid; aj.sid=t; strcpy(v,);strcpy(,);strcpy(aj

27、.name,v); for(i=0;i=size-2;i+) printf(sid: %ldtname:%snn,ai.sid,);getch();void select(struct work a) int i=0,found=1;long num; printf(Input select sid: ); scanf(%ld,&num);for(i=0;i=size-2;i+) if(ai.sid=num) found=0;printf(%ld: %s,num,); break;if(found)printf(nnNO this sid!); getch();vo

28、id main() struct work *p;p=set_list();paixu(p); select(p);10、 #include float jiecheng(int n) long s=1;int i;for(i=1;i=n;i+) s=s*i; return s;float chengfang(float x,int n) float s=1;int i; for(i=1;i=n;i+) s=s*x; return s;float f(float x,int n) float s=1,t=0;int i=1,j=-1; for(i=1;i=0 ak=temp break k=8

29、(2) %sstri=Z cstri+26-a+ i+a4、/* 題目:求一組成績的平均分數(shù)以及高于平均分的成績。*/#include#define max 100void main()float amax,s=0,average=0;int i,n;printf(Please input N: ); /* 輸入要處理元素的個數(shù) */ scanf(%d,&n);printf(nPlease input %d ge shu:,n); /*輸入 90, 85, 92,77,80,for(i=0;in;i+)scanf(%f,&ai); s+=ai;average = s / n;printf(na

30、verage = %.2fn,average); /* 輸出 81.00*/ for(i=0;iaverage) printf(%.2f ,ai); /* 輸出 90.00 ,85.00 ,92.00*/5、/* 題目:編寫程序,輸入一組整數(shù),將他們排序后由小到大輸出。*/#include#define max 100void main()int amax,j,i,k=0,t=0,n=0;printf(Please input N: ); /* 輸入要處理元素的個數(shù) */ scanf(%d,&n);for(i=0;in;i+) scanf(%d,&ai);for(i=0;in-1;i+)/*比

31、較 n-1 遍 */ k=i;for(j=i+1;jaj) k=j;if(k!=i)/* 元素排序前下標與排序后下標不符,則交換其值 t=ai;ai=ak;ak=t;printf(nn);for(i=0;in;i+)printf(%d ,ai);6、/*題目:從鍵盤輸入一個 4x4 的整數(shù)矩陣,以主對角線( )為對稱軸 */ /* 將左下角元素中較大者替換右上角元素,并將右上角含對稱軸輸出。*/#include #include void main()int d44=0,0,0,0,i,j;clrscr();printf(nPlease input 16 num:);for(i=0;i4;i+

32、)for(j=0;j4;j+) scanf(%d,&dij);for(i=0;i4;i+)for(j=0;jdji) /* 左下角元素大于右上角元素則交換 */ dji=dij;for(i=0;i4;i+) printf(nnnnn);/* 為了保持每行的間隔與每列的相同 */ for(j=0;j=i)printf(t%d,dij);/* 為了使位數(shù)不同的數(shù)輸出依然保持三角型 */ else printf(t);7、* 題目:輸入一個 3x4 的二維數(shù)組,然后分別按行和按列輸出。 */ #include #include void main()int d34=0,0,0,0,i,j;print

33、f(Please input 12 num:);for(i=0;i3;i+)for(j=0;j4;j+) scanf(%d,&dij); printf(nnAn hang: );for(i=0;i3;i+)for(j=0;j4;j+)printf(%d ,dij); printf(nnAn lie: ); for(i=0;i4;i+) for(j=0;j3;j+)printf(%d ,dji);8、/*題目:編寫程序,將兩個字符串連接起來,不用strcat函數(shù)。 */#include #includevoid main()char s150,s250; int i=0,j=0,t=0; pri

34、ntf(Input one: ); gets(s1);while(s1i!=0) i+; printf(nnInput second: gets(s2);while(s2j!=0) for(t=0;t=j;t+) s1i=s2t;/* 統(tǒng)計 s1 中字符個數(shù) );/* 統(tǒng)計 s2 中字符個數(shù)*/*/j+;/*把 s2中字符連接到 s1 后面*/ i+;printf(nnone + second: );puts(s1);9、/*輸入一行字符串,統(tǒng)計字符對ab 的個數(shù)。 */ #include #include#define MAX 100 void main()char strMAX;int i

35、=0,flage=0;int count=0;printf(nPlease input a string:);gets(str);while(stri) if(stri=a) flage=1;else if(stri=b)if(flage=1) count+;flage=0;else flage=0;i+;printf(the double char ab count is %dn,count);10、#include /* 相對于第一種方法,這種方法能把同為最大的字符串全部打印出來。*/#includeint paixu(int a,int n) /* 構造對長度為 n 的數(shù)組排序的函數(shù) *

36、/int i,j,t,k,temp;for(i=0;in-1;i+) k=i;for(j=i+1;jaj)k=j;if(k!=i) t=ai;ai=ak;ak=t;temp=an-1;return temp; /* 返回最大字符串的長度的值 */void main()char str150,str250,str350,str450,str550,str650;char str750,str850,str950,str1050;int count10=0,temp10=0,i,j;int paixu(int a,int n);for(i=1;i=10;i+)/*輸入 10 個字符串 */prin

37、tf(nPlease input %d string:,i);if(i=1) gets(str1);while(str1count0)count0+;temp0+; if(i=2) gets(str2);while(str2count1)count1+;temp1+;if(i=3) gets(str3);while(str3count2)count2+;temp2+; if(i=4) gets(str4);while(str4count3)count3+; temp3+;if(i=5) gets(str5);while(str5count4)count4+; temp4+;if(i=6) ge

38、ts(str6);while(str6count5)count5+; temp5+;if(i=7) gets(str7);while(str7count6)count6+; temp6+;if(i=8) gets(str8);while(str8count7)count7+; temp7+;if(i=9) gets(str9);while(str9count8)count8+; temp8+; if(i=10) gets(str10);while(str10count9)count9+;temp9+; j=paixu(temp,10);for(i=1;i=10;i+)if(counti-1=j

39、) switch(i)/*只要字符串長度與最大長度相等就打印出來*/ 11、case 1:puts(str1);case 2:puts(str2);case 3:puts(str3);case 4:puts(str4);case 5:puts(str5);case 6:puts(str6);case 7:puts(str7);case 8:puts(str8);case 9:puts(str9);printf(n); break; printf(n); break; printf(n); break; printf(n); break; printf(n); break; printf(n);

40、 break; printf(n); break; printf(n); break; printf(n); break;case 10:puts(str10); printf(n); break;/*給數(shù)組 a輸入 m 個按升序排列的數(shù),給數(shù)組 /*將 a與 b 中的元素按降序排列存在數(shù)組 #include void main()int a100,b100,c100,i,m,n,j,k,temp=0,s; printf(nm = );scanf(%d,&m); printf(nn = );scanf(%d,&n); printf(nPlease input m ge shu:);for(s=

41、0;sm;s+)scanf(%d,&as);printf(nPlease input n ge shu: for(s=0;sn;s+)scanf(%d,&bs);i=m-1;j=0;k=0;for(;) if(i=0) if(ai=bj) ck=bj;else/*i ,);c 中。b 輸入 n 個按降序排列的數(shù) */ */* 確定 M 的個數(shù) */ /*確定 N 的個數(shù) */j,分別標記 a與 b且都是從最大元素開始做標記*/*a 中只有一個數(shù)或者 a 處理到了最后一個元素 */ /* 如果 a 的最后一個元素 =b 中當前元素 */ k+; j+; ck=ai;k+;for(;)/*把 b數(shù)

42、組接到 c后面 */ ck=bj; j+; k+;if(j=n) /* 如果 b 處理完,結束。并做標記 temp=1*/ temp=1;break; if(temp=1)break; /* 標記 temp=1 ,全部處理結束 */ if(aibj)&(i!=0)&(j!=n) ck=ai; i-; k+; if(j=n) /*b 處理到完 */ for(;)/*把 b數(shù)組接到 c后面 */ if(i=0)/* 如果 a 處理完,結束。并做標記 temp=1*/ ck=ai;temp=1;break; ck=ai;i-; k+;if(temp=1) break;/* 標記 temp=1 ,全部處理結束 */ for(s=0;s=*q; q+(2) i11 &aii-;if(i=0)break;i!=04、/* 題目:輸入一個字符串,再刪除指定的字符*/* 如果字符串中沒有指定字符,則輸出原字符串。*/#include void fun(char *

溫馨提示

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

評論

0/150

提交評論