c語(yǔ)言程序設(shè)計(jì)課后習(xí)題解答_第1頁(yè)
c語(yǔ)言程序設(shè)計(jì)課后習(xí)題解答_第2頁(yè)
c語(yǔ)言程序設(shè)計(jì)課后習(xí)題解答_第3頁(yè)
c語(yǔ)言程序設(shè)計(jì)課后習(xí)題解答_第4頁(yè)
c語(yǔ)言程序設(shè)計(jì)課后習(xí)題解答_第5頁(yè)
已閱讀5頁(yè),還剩10頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

第3章習(xí)題解答第1章語(yǔ)言程序主要由預(yù)辦理命令、函數(shù)、說(shuō)明等構(gòu)成。填空分號(hào)(2)main(3)3.源程序:#include<>main( ){printf(“*************************n”);printf(“HelloWorld!n”);printf(“*************************”);}4.源程序:#include<>main( ){inta,b,c;/*定義變量*/scanf(“%d”,&a);/*輸入第一個(gè)整數(shù)*/scanf(“%d”,&b);/*輸入第二個(gè)整數(shù)*/c=a-b;/*計(jì)算差*/printf(“%d-%d=%d”,a,b,c);/*輸出結(jié)果}5.(1)<>(2)x=10;(3)printf(“s=%dn”,s);

*/第2章1.cabgaded=4,y=6x=4,y=3=3,y=64.165.#include<>main( ){inta,b,c;scanf("%d%d",&a,&b);c=a*b;printf("%d*%d=%d",a,b,c);}第3章1.(1)b(2)b(3)d(4)a(5)b2.(1)&a,&b(2)l,s3.printf(“x=%.2f,y=%.2fn”,x,y);4.#include<>main( ){intnum1,num2,num3,sum;floataverage;scanf("%d%d%d",&num1,&num2,&num3);sum=num1+num2+num3;average=sum/;printf("sum=%d,average=%.2f\n",sum,average);}5.#include<>main( ){inthour,minute,second,total;/*定義變量代表時(shí)、分、秒和總秒數(shù)*/scanf("%d",&total);hour=total/3600;minute=total%3600/60;second=total%3600%60;printf("%dhours:%dminutes:%dseconds\n",hour,minute,second);}第4章1.(1)a(2)b(3)b(4)b(5)b2.03.(1)6(2)4(3)1(4)14.#include<>main( ){intx,y;scanf("%d",&x);if(x>-5&&x<0)y=5*x;if(x==0)y=-1;if(x>0&&x<10)y=2*x+1;printf("x=%d,y=%d\n",x,y);}5.#include<>main( ){intscore,rank;/*score

表示成績(jī)

,rank

表示級(jí)別

*/printf("Pleaseinputscore:");scanf("%d",&score);rank=score/10;switch(rank){case10:case9:printf("case8:printf("

成績(jī)等級(jí)為:成績(jī)等級(jí)為:

A\n");break;B\n");break;case7:printf("case6:printf("default:printf("

成績(jī)等級(jí)為:成績(jī)等級(jí)為:成績(jī)等級(jí)為:

C\n");break;D\n");break;E\n");break;}}6.#include<>voidmain( ){intn;printf("Pleaseinputthedigit:");scanf("%d",&n);switch(n){case1:printf("Jan\n");break;case2:printf("Feb\n");break;case3:printf("Mar\n");break;case4:printf("Apr\n");break;case5:printf("May\n");break;case6:printf("Jun\n");break;case7:printf("Jul\n");break;case8:printf("Agu\n");break;case9:printf("Sep\n");break;case10:printf("Oct\n");break;case11:printf("Nov\n");break;case12:printf("Dec\n");break;}}第5章1.babdd2.7,13.y=-14.m=65.10,1435ABABCDCDinclude<>main( ){inti;longintsum=0;for(i=2;i<=200;i+=2)sum+=i;printf("2+4+6+...+200=%ld",sum);}10.#include<>main( ){longintn;intsum=0;printf("Pleaseinputthenber:");scanf("%ld",&n);while(n!=0){sum+=n%10;n/=10;}printf("%d\n",sum);}第6章1.dbdca6895.12,66.(1)i--(2)n7.(1)chark;(2)i<j8.#include<>main( ){floata[10];inti;floatsum=0,average;for(i=0;i<10;i++){printf("a[%d]=?",i);scanf("%f",&a[i]);sum+=a[i];}average=sum/10;printf("averageis%.2f\n",average);}9.#include<>#defineN10main( ){inta[N],i,target,found;for(i=0;i<N;i++)scanf("%d",&a[i]);printf("Pleaseinputanumber:");scanf("%d",&target);i=0;while(i<N&&target!=a[i])i++;found=i<N?i:-1;printf("%d\n",found);}10.#include<>#defineN80main( ){charstr[N];intlen=0,i=0;printf("Pleaseinputastring:");gets(str);while(str[i++]!=0)len++;printf("thelengthofstringis%d.\n",len);}第7章21136164.(1)func(2)m++91,6,3,2,32,5,1,2,3,-28.#include<>intfun(inta,intb,intc);main( ){inta,b,c,max;printf("Pleaseinputthreenumbers:");scanf("%d%d%d",&a,&b,&c);max=fun(a,b,c);printf("Maxis%d.\n",max);}intfun(inta,intb,intc){intmax;max=a>b?(a>c?a:c):(b>c?b:c);returnmax;}9.#include<>longintsum(intn);main( ){intn;printf("n=?");scanf("%d",&n);printf("Sum=%ld.\n",sum(n));}longintsum(intn){if(n==1)return1;elsereturnsum(n-1)+n;}10.#include<>voidfun(n);main( ){intn;printf("n=?");scanf("%d",&n);fun(n);}voidfun(n){if(n==0)return;else{fun(n/2);printf("%-2d",n%2);}}第8章1.bdbcc34565.3456.123456b[i]bcdefghap=sum#include<>#include<>main( ){charstr[80];char*p1,*p2;gets(str);p1=str;p2=str+strlen(str)-1;while(p1<p2&&*p1++==*p2--);puts(p1<p2?"不是回文":"是回文");}第9章1.a.#defineF(x)(x)*(x)*(x)b.#defineF(x)(x)%4c.#defineF(x,y)(x)*(y)<100?1:02.44#defineDOUBLE(x)2*(x)3.d-20Nisundefined7.5y=68.#include<>#defineSWAP(a,b){inttemp;temp=a;a=b;b=temp;}main( ){intx,y;printf("x=?");scanf("%d",&x);printf("y=?");scanf("%d",&y);SWAP(x,y)printf("x=%d,y=%d.\n",x,y);}第10章1.structstudent{intsno;charsname[10];charsex;}stu1,stu2;12合法的有a,b,d改成改成改成a,b,c,d(說(shuō)明:變量b有確立的值以后,b++是合法的。)改為typedefstructproduct{charname[10];floatprice;}PRODUCT;PRODUCTproducts[10];6.structemployeeprintemp(emp)24398#include<>structtime_struct{inthour;intminute;intsecond;};main( ){structtime_structtime;printf("Inputtime?\n(Example18:28:38)\n");scanf("%d:%d:%d",&,&,&;printf("Timeis%d:%d:%d\n",,,;}9.#include<>structtime_struct{inthour;intminute;intsecond;}time;voidenter_time( );voiddisplay_time( );main( ){enter_time( );display_time( );}voidenter_time( ){printf("Enterthetime(example18:28:38)?");scanf("%d:%d:%d",&,&,&;}voiddisplay_time( ){printf("Timeis%d:%d:%d.\n",,,;10.#include<>#defineN3structhotel{charname[31];/*酒店名稱(chēng)*/charaddress[31];/*酒店地點(diǎn)*/intgrade;/*酒店級(jí)別*/floataverage_charge;/*均勻房?jī)r(jià)*/intnumber;/*房間數(shù)目*/}h[N]={{"h1","上海路",5,,80},{"h2","北京大街",5,,70},{"h3","南京大街",3,,100}};main( ){intgrade,i;printf("請(qǐng)輸入級(jí)別(3-5)?");scanf("%d",&grade);for(i=0;i<N;i++)if(h[i].grade==grade)printf("名稱(chēng):%s\n地點(diǎn):%s\n級(jí)別:%d\n均勻房?jī)r(jià)%.2f\n

房間數(shù)目

:%d\n",h[i].name,h[i].address,h[i].grade,h[i].average_charge,h[i].number);}第11章1.cdbbbBasicanfgetc(fp)“”,“w”5.#include<>#include<>#include<>typedefstructEmployee{intid;charname[20];chargender[20];intage;charaddress[20];}Employee;intmain(void){FILE*fp;charanother,choice;Employeeemp;longintrecsize;fp=fopen("","rb+");if(fp==NULL){fp=fopen("","wb+");if(fp==NULL){printf("Can'tOpenFile");exit(0);}}recsize=sizeof(emp);while(1){printf("1.增添記錄2.顯示男職工3.退出\n");printf("Enteryourchoice(1-3):");fflush(stdin);scanf("%c",&choice);switch(choice){case'1':fseek(fp,0,SEEK_END);another='Y';while(another=='Y'||another=='y'){printf("輸入信息(id姓名性別年紀(jì)地址):\n");scanf("%d%s%s%d%s",&,&,&,&,&;fwrite(&emp,recsize,1,fp);printf("能否連續(xù)(Y/N):");fflush(stdin);another=getchar( );}break;case'2':printf("學(xué)號(hào)\t姓名\t性別\t年紀(jì)\t地址\n");rewind(fp);while(fread(&emp,recsize,1,fp)==1)if(strcmp,"男")==0)printf("%d\t%s\t%s\t%d\t%s\n",,,,,;break;case'3':fclose(fp);exit(0);}}}6.#include""#include""#defineM2#definestustructstudentstu{intnum;charname[20];floats1;floats2;floats3;floatavg;};main( ){stust[M];FILE*fp;inti;printf("請(qǐng)輸入5名同學(xué)生的成績(jī),依據(jù)學(xué)號(hào),姓名,成績(jī)1,成績(jī)2,成績(jī)3的次序,中間用空格隔開(kāi):\n");for(i=0;i<M;i++){scanf("%d%s%f%f%f",&st[i].num,st[i].name,&st[i].s1,&st[i].s2,&st[i].s3);st[i].avg=(st[i

溫馨提示

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

評(píng)論

0/150

提交評(píng)論