C程序設(shè)計習(xí)題與實驗教程參考答案(習(xí)題部分)解讀_第1頁
C程序設(shè)計習(xí)題與實驗教程參考答案(習(xí)題部分)解讀_第2頁
C程序設(shè)計習(xí)題與實驗教程參考答案(習(xí)題部分)解讀_第3頁
C程序設(shè)計習(xí)題與實驗教程參考答案(習(xí)題部分)解讀_第4頁
C程序設(shè)計習(xí)題與實驗教程參考答案(習(xí)題部分)解讀_第5頁
已閱讀5頁,還剩37頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、第1章概述、選擇題題號1234答案BBBA、填空題1 .主 或 mai n2. # in clude 或 # in elude iostream.h”3. 編譯連接(構(gòu)件)4. 5. ;6. .cpp.obj.exe三、編程題#in cludevoid mai n()cout我的姓名是 XX , n”我的學(xué)號為 XXX ”5&!(x%2)或 x5&x%2!=07. true 或 1& b%a=0&c%a=09.(float)x或 float(x)10.5.411.1212.45勘誤:a和b的值分別為、。13. 203232162014. AB15. 3116. 9.017. 1318. 4三、

2、編程題1交換兩個整數(shù)的位置#in cludevoid mai n()int x,y,t;cin xy;coutx yendl;t=x;x=y;y=t;cout交換后的兩個數(shù)為: x t yendl;2. 求三角形面積#in clude #in clude void main (void)int a,b,c;double s,area;cout輸入三角形三邊長 a,b,c: abc;s=(a+b+c)/2.0 ;area=sqrt(s*(s_a)*(s_b)*(s_c);cout三角形面積為:area3)if(y5)z=6;else z=7 ”“不合法的”7.其中“合法的”改為二、填空題1.2.

3、.7.&2.13.switch字符型451,*5,y=-82,3,y=53,04,0,B1,2,35,0,D0,5,5015y=014.15.16. #0123401234#0.35711133 y1*y20 n999 x1=x,y1=3*x1*x1*x1-5*x1+13 b=n/10%10(a+b+c)%5=0 x2=x勘誤:“各個數(shù)位的數(shù)字之和等于5的整數(shù)”改為“各位數(shù)字之和是21. 12 1522. (100-cock-he n)%3=0)5的倍數(shù)的整數(shù)”勘誤:for(cock=0;cock=20;cock+)for(he n=0;he

4、 neps24. isprime=1j125. int r=1 break26. x=A&c=a&cs n/=10三、編程題1 .從大到小排列 3 個整數(shù)#include void main()int a,b,c,d;cinabc;if(ab) d=a; a=b;b=d; if(ac) d=a; a=c;c=d; if(bc) d=b; b=c;c=d; coutatbtcn;2. 100300 滿足條件的數(shù)#include void main()int k=0;for(int i=101;i300;i+=2)if(i*i)%6=3) coutit;k+; if(k%5=0)coutn;cou

5、tn;3. 逆序顯示 16 進制數(shù)#include void main()int a,k,n;cina;k=a;while(k) n=k%16;k/=16; switch(n) case 10:coutA;break;case 11:coutB;break;case 12:coutC;break;case 13:coutD;break;case 14:coutE;break;case 15:coutF;break; default:cout n;coutn;第4章數(shù)組、選擇題題號1234567891011答案DDBBBCDBABB題號1213141516171819202122答案DCBADB

6、AABCC勘誤:15.其中“ s320=”abc” 改為 “ s320= ”abc ”(字符 c 后有一個空格)、填空題1.3242.首地址3.ii+ji/7 i%7 i*7+j4.787505.字符(.char)型 cin .getli ne(str,80);6. 1234567. FMBS勘誤:“for(int i=0;i4;i+,p+)cout*pi;改為“ for(int i=0;i4;i+)cout*si;&a::5d: 1e: 4f :1g: 1i:1l: 1m: 1o:3r: 3s:2u: 1y: 39.It is 4:30 pm.I43010.100: 3200: 2300:

7、5400: 2 500: 311. D8012.500 80090013.26 2014.s2+31nt%2+0 !strcmp(s1,p2)15.numi+s=0flag=1 j=k16.i=len2bk=bk-1+i%5=017.pa=a&pb=b j+k+carry ps+p=A&*p1=a&*p1=zp2+char (*p4)2022.&ai*pk*pj int *t*pi三、編程題1交換一維數(shù)組中的最大元素與最小元素的位置 #include void main(void )int a6=18,16,19,15,11,8;int i,max,min,j,k,t;cout 原數(shù)組為: n

8、; for(i=0;i6;i+)coutait; coutendl; for(j=k=0,max=min=a0,i=1;i6;i+)if(maxai)min=ai,k=i;t=aj,aj=ak,ak=t;cout 交換后的數(shù)組為: n; for(i=0;i6;i+)coutait; coutendl;2二維數(shù)組右移一列#include#define ROW 3#define COL 4void main(void)int aROWCOL=1,2,3,4,5,6,7,8,9,10,11,12,i,j,t; cout 移動前的數(shù)組為: n;for(i=0;iROW;i+)for(j=0;jCOL;

9、j+) coutaijt;coutn;for(i=0;i0;j-) aij=aij-1;aij=t; / ai0=t;cout 移動后的數(shù)組為: n;for(i=0;iROW;i+)for(j=0;jCOL;j+) coutaijt;coutn; 3統(tǒng)計字符串中種類字符的個數(shù) #include void main()char str50,*p=str;int con=0,num=0,eng=0,oth=0;cout 請輸入一個字符串: ;cin.getline(str,50);while(*p) if(*p=0&*p=A&*p=a&*p=z)eng+; else oth+;p+;coutstr

10、 中有控制字符 con 個,數(shù)字字符 num 個,英文字母 eng 個,其 它字符 oth 個。 n;4一維數(shù)組插入有序二維數(shù)組 #include #include #define ROW 4#define COL 3void main()int aROWCOL=1,3,2,4,6,8,8,9,10,bCOL=2,7,1;int i,j;cout 插入前的數(shù)組為: n;for(i=0;iROW-1;i+)for(int j=0;jCOL;j+)coutsetw(5)aij;coutendl;for(i=0;i0;j-)if(aj-1ibi)aji=aj-1i;else break;aji=bi

11、;cout 插入后的數(shù)組為: n;for(i=0;iROW;i+)for(int j=0;jCOL;j+)coutsetw(5)aij;coutendl;5二維數(shù)組正數(shù)和負(fù)數(shù)的均值#includevoid main(void )int b35=1,12,3,0,-2,7,-12,-13,18,-20,0,9,-5,7;float ave1=0,ave2=0;int i,j,count1=0,count2=0,(*p)5=b;for(i=0;i3;i+)for(j=0;j0)ave1+=bij;count1+;if(*(*(p+i)+j)0) ave2+=bij; count2+;ave2/co

12、unt2endl; cout 正數(shù)的平均值為 ave1/count1 ,負(fù)數(shù)的平均值為 6查找一維數(shù)組中的元素#includevoid main()float a10=3,5,8,1,7,9,10,2,6,4;float k,*p=a; coutk; int i,index=-1;for(i=0;i10;i+) if(*p=k) index=i; break;p+; if(index=-1)coutk 沒有出現(xiàn)在數(shù)組中。 n; else coutk 是數(shù)組中下標(biāo)為 index 的元素。 n;7刪除字符串中的指定字符#include #include void main()char string

13、100,ch,*p=string; cout 請輸入字符串: n; cin.getline(p,100);cout請輸入要刪除的字符:”;cin .get(ch);while(*p)if(*p=ch)strcpy(p,p+1);else p+;cout刪除字符ch后的字符串為:n; coutstri nge ndl;第5章函數(shù)與編譯預(yù)處理、選擇題題號12345678910答案DCDABCDDCC題號11121314151617181920答案ABCCCADCDC勘誤:15. D選項“和類型均不同”改為“或類型不同”二、填空題1. 值傳遞地址傳遞引用傳遞2. in li ne3.文件包含宏定義條

14、件編譯4.形參名函數(shù)先使用后定義5.形式參數(shù)實際參數(shù)參數(shù)類型參數(shù)個數(shù)參數(shù)順序6. voidint7.靜態(tài)存儲區(qū)域08. void (*fp )(char *s, i nt a) fp=f;或(*fp)=f;9. sum1=5 sum2=-1510.s3=2.8 s1=0,s2=4,s3=25011.1 113313112.0101010111013.1 23453214514.-414-18 14-321415.x=5,y=3y=1016.15-112141026714332017.1 a3 b5c18.i+1 k=j strk=strk+1del(s)19.i+ aj=x add_sort(

15、b,x ,n)n20.*sumn/=10fun (i, &s)i%11=0&s=1321.k=jai%2!=0coun t+on_off(a,LAMP,CHILD)22.strisubstrk+1=0k+f(s1,s2)三、編程題1. 字符排序#in clude void sort_char(char *str)char *p1,*p2,*q;char c;for(p1=str;*(p1+1)!=0;p1+)q=p1;for(p2=p1+1;*p2!=0;p2+)if(*p2*q)q=p2;if(q!=p1)c=*p1;*p1=*q; *q=c;void main()char s=gabhde

16、cf;cout 原字符串為 :sendl;sort_char(s);cout 排序后的字符串為 :sendl; 2. 互換前后奇數(shù)位置的元素#include#includevoid fun(char *str)int i,j;char c;if(strlen(str)3) return; if(strlen(str)%2=0) j=strlen(str)-1;else j=strlen(str);for(i=1;ij;i+=2,j-=2)c=stri;stri=strj;strj=c;void main()char s=ggoi romndno;cout 原字符串為 :sendl;fun(s)

17、;cout 交換后的字符串為 :sendl;3. 查找自反數(shù) #include #include int fun(int n)int a50;int i=0,k=n;while(k) ai=k%10; k/=10; i+;i-;for(int j=0;j=i) return 1;else return 0;void main()int base,num;num:endl;coutbasenum;int n=base,count=0;while(countnum) if(fun(n)coutnt; count+;if(count%5=0)coutendl; n+;coutn ext- n ext

18、=NULL7.p=head p- next=NULL pO-n ext=p-n ext delete p pO-n ext=p1 p1- n ext=p8.20129.1343110.gh11. ai.co n=0 ai.c on+ continue12. h1-data=h2-data h1!=NULL&h2!=NULL p- next=h1 h=merge(h1,h2)13. head-n ext=p1 return head head-n ext=p2 p2=p2- next14. new Node p-n ext=top top-n ext delete p15.p return h3

19、p=new ND p-n ext=h16.node *h h2- next temph217. p=p-n ext p1- n ext=p h=h-n ext p-n ext=h118. p-n ext=head p1=p1- next pmaxl- n ext=pmax- n ext DeleteMax(head)三、編程題1.建立鏈表,求輸出總分最高和最低的學(xué)生#in cludestruct no deint data;node* next;node *fun(node *head)node *p=head,*even=0,*old=0,*p1=0,*p2=0; if(head=NULL)

20、return head;while(p)if(p-data%2)/ 改為 if(p-data%2) if(old=0)old=p,p1=p; elsep1-next=p;p1=p;else if(even=0)even=p;p2=p; elsep2-next=p;p2=p; p=p-next;if(old)p1-next=0;if(even)head=even; p2-next=old; else head=old;return head;void print(node *p) / 輸出鏈表while(p) coutdatanext ;coutn ;void main(void)node* h

21、ead=NULL,*p,*p1;int a;n;couta;while( a!=-1 ) p=new node; p-data=a ; if(head=0) head=p ; p1=p ;else p1-next=p ; p1=p ;couta; p-next=0; print(head);head=fun(head);print(head);2建立鏈表,輸出總分最高和最低的學(xué)生 #include/#define NULL 0struct NODEint id;char name10;float c,maths,english;NODE *next;NODE *Create(int n) NO

22、DE *p1,*p2,*h=0;int i=0;while(ip1-idp1-namep1-cp1-mathsp1-english; if(h=0)h=p1;p2=p1;elsep2-next=p1;p2=p1;i+;p2-next=0;return h;void print(NODE *h)NODE *p;p=h;NODE *max,*min;max=min=h;float sum=0;while(p!=0)sum=(p-c+p-maths+p-english);if(sum(max-c+max-maths+max-english)max=p; if(sumc+min-maths+min-e

23、nglish)min=p; p=p-next;cout 最高分: idtnamet ctmathstenglishendl;cout 最低分: idtnamet ctmathstenglishendl; coutnext;delete p;void main() int n;cout 請輸入班級人數(shù)! endln;coutendl;cout 請輸入班級學(xué)生信息! endl;cout 學(xué)號 姓名 C+ 成績 maths englishendl;NODE *h;h=Create(n);coutendl 學(xué)號 姓名 C+ maths englishendl; print(h);deletechain

24、(h);3. 將鏈表按照產(chǎn)品類別分類#include #include struct nodechar product20;char category10;node *next;node *create(void)node temp,*p2=&temp;char product20,category10;while coutproductcategory;if(strcmp(product,end)=0) break;p2-next=new node; p2=p2-next; strcpy(p2-product,product); strcpy(p2-category,category);p2

25、-next=NULL;return temp.next;void print(node *h)while(h) coutproducttcategorynext;coutnext; delete p;node *insertascategory(node *h,node *p)node *p2=0,*p1=h;if(h=0) p-next=0;return p; while(p1&strcmp(p1-category,p-category) p2=p1;p1=p1-next; while(p1&strcmp(p1-category,p-category)=0) p2=p1;p1=p1-next

26、;p-next=p1;p2-next=p;return h;node *classify(node *h)node *h2=0,*p;while(h)p=h;h=h-next;h2=insertascategory(h2,p);return h2;void main(void)node *h;h=create(); print(h);h=classify(h); print(h); deletechain(h);4. 以 x 值為界把鏈表分為兩部分struct nodeint data;node *next;node *change(node *pa,int x)node *p1,*p2,*p

27、,*q;p1=p2=pa;p=new node;p-data=x;while(p2-datanext!=0)p1=p2;p2=p2-next;if(p2-next!=0)p-next=p2;p1-next=p;q=p1;while(p2!=0) if(p2-datanext=p2-next; p2-next=p; q-next=p2;q=p2; p2=p1-next; elsep1=p2; p2=p2-next; else p1=p2; p-next=0; return pa;void main()node a10=3,4,2,1,9,6,5,0,10,6,*head=a,*p; int i,

28、x;for(i=0;i9;i+)ai.next=&ai+1; a9.next=NULL; coutx; head=change(head,x); p=head; while(p)coutdatanext; coutendl;5. 通訊錄 #include #include #include #define N 3 struct STUDENT char id15; char name20; char phone15; char room5;STUDENT stuN;int flage;void Input(STUDENT*);void Output(STUDENT*);void Output1

29、(STUDENT*);void Search_face(STUDENT*);void Search1(STUDENT*);void Search2(STUDENT*);void Search3(STUDENT*);void Search4(STUDENT*);void Sort_face(STUDENT*);void Sort1(STUDENT*);void Sort2(STUDENT*);void Sort3(STUDENT*);void Sort4(STUDENT*);void face() int i; coutendlt;for(i=0;i57;i+)cout*; coutendl;c

30、outt*ttttttt*n;coutt*ttt 學(xué)生通訊錄 ttt*n;coutt*ttttttt*n;coutt*t1 、輸入 t2、查詢 t3、排序 t4、輸出 t5、退出 tt*n; coutt*ttttttt*nt;for(i=0;i57;i+)cout*;coutendlendl;docouti; while(i5);coutendl;switch(i)case 1:Input(stu);break;case 2:Search_face(stu);break;case 3:Sort_face(stu);break;case 4:Output(stu);break;case 5:ex

31、it(0);void Input(STUDENT*ps) flage=1;cout 請輸入學(xué)生信息! nn;cout 學(xué)號姓名 電話 宿舍號 n;for(int i=0;psi.phonepsi.room; face();void Search_face(STUDENT*ps) if(flage=0)cout 你還沒輸入數(shù)據(jù)!請輸入! ; face(); coutendlt; for(int i=0;i57;i+)cout*; coutendl;coutt*ttttttt*n; coutt*ttt 查詢 tttt*n;coutt*ttttttt*n;coutt*t

32、1 、姓名 t2、學(xué)號 t3、電話 t4、宿舍 t5、退出 tt*n; coutt*ttttttt*nt;for(i=0;i57;i+)cout*;coutendlendl;docouti;while(i5);coutendl;switch(i)case 1:Search1(stu);break;case 2:Search2(stu);break;case 3:Search3(stu);break;case 4:Search4(stu);break;case 5:face();void Sort_face(STUDENT*ps) if(flage=0)cout 你還沒輸入數(shù)據(jù)!請輸入! ; f

33、ace(); coutendlt; for(int i=0;i57;i+)cout*; coutendl;coutt*ttttttt*n; coutt*ttt 排序 tttt*n;coutt*ttttttt*n;coutt*t1 、姓名 t2、學(xué)號 t3、電話 t4、宿舍 t5、退出 tt*n; coutt*ttttttt*nt; for(i=0;i57;i+)cout*; coutendlendl;docouti;while(i5);coutendl;switch(i)case 1:Sort1(stu);break;case 2:Sort2(stu);break;case 3:Sort3(s

34、tu);break;case 4:Sort4(stu);break;case 5:face();void Print(STUDENT st) cout 學(xué)號 姓名 電話 宿舍號 n;cout tst.phonetst.roomn; void Search1(STUDENT*ps) int k=0;char str20;coutstr;for(int i=0;iN;i+) if(strcmp(,str)=0) k=1; break;if(k)cout 查詢結(jié)果是: n;Print(psi);else cout 該學(xué)生不存在! ;Search_face(s

35、tu);void Search2(STUDENT*ps) int k=0;char str20;coutstr;for(int i=0;iN;i+)if(strcmp(psi.id,str)=0)k=1;break;if(k)cout 查詢結(jié)果是: n;Print(psi);else cout 該學(xué)生不存在! ;Search_face(stu);void Search3(STUDENT*ps) int k=0;char str20;coutstr;for(int i=0;iN;i+) if(strcmp(psi.phone,str)=0) k=1;break;if(k)cout 查詢結(jié)果是:

36、n;Print(psi);else cout該學(xué)生不存在!;Search_face(stu);void Search4(STUDENT*ps) int k=0;char str20;coutstr;for(int i=0;iN;i+)if(strcmp(psi.room,str)=0)k=1;break;if(k)cout 查詢結(jié)果是: n;Print(psi);else cout該學(xué)生不存在!;Search_face(stu);void Sort1(STUDENT*ps)STUDENT s;for(int i=0;iN-1;i+) for(int j=i;j0) s=psi;psi=psj;psj=s;cout 排序結(jié)果是: n;Output1(ps);Sort_face(stu);void Sort2(STUDENT*ps) STUDENT s;for(int

溫馨提示

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

最新文檔

評論

0/150

提交評論