c语言程序设计(科学出版社)课后习题解答

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

第3章习题解答第1章1.C语言程序主要由预处理命令、函数、注释等组成。2.填空(1)分号(2)main(3)stdio.h3.源程序:#includestdio.hmain(){printf(“*************************\n”);printf(“HelloWorld!\n”);printf(“*************************”);}4.源程序:#includestdio.hmain(){inta,b,c;/*定义变量*/scanf(“%d”,&a);/*输入第一个整数*/scanf(“%d”,&b);/*输入第二个整数*/c=a-b;/*计算差*/printf(“%d-%d=%d”,a,b,c);/*输出结果*/}5.(1)stdio.h(2)x=10;(3)printf(“s=%d\n”,s);第2章1.(1)c(2)a(3)bg(4)ade(5)d2.a.5b.295c.4d.29e.93.a.x=4,y=6b.x=4,y=3f.x=3,y=64.165.#includestdio.hmain(){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=%.2f\n”,x,y);4.#includestdio.hmain(){intnum1,num2,num3,sum;floataverage;scanf(%d%d%d,&num1,&num2,&num3);sum=num1+num2+num3;average=sum/3.0;printf(sum=%d,average=%.2f\n,sum,average);}5.#includestdio.hmain(){inthour,minute,second,total;/*定义变量代表时、分、秒和总秒数*/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.#includestdio.hmain(){intx,y;scanf(%d,&x);if(x-5&&x0)y=5*x;if(x==0)y=-1;if(x0&&x10)y=2*x+1;printf(x=%d,y=%d\n,x,y);}5.#includestdio.hmain(){intscore,rank;/*score表示成绩,rank表示级别*/printf(Pleaseinputscore:);scanf(%d,&score);rank=score/10;switch(rank){case10:case9:printf(成绩等级为:A\n);break;case8:printf(成绩等级为:B\n);break;case7:printf(成绩等级为:C\n);break;case6:printf(成绩等级为:D\n);break;default:printf(成绩等级为:E\n);break;}}6.#includestdio.hvoidmain(){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.(1)b(2)a(3)b(4)d(5)d2.7,13.y=-14.m=65.10,146.37.58.ABABCDCD9.#includestdio.hmain(){inti;longintsum=0;for(i=2;i=200;i+=2)sum+=i;printf(2+4+6+...+200=%ld,sum);}10.#includestdio.hmain(){longintn;intsum=0;printf(Pleaseinputthenber:);scanf(%ld,&n);while(n!=0){sum+=n%10;n/=10;}printf(%d\n,sum);}第6章1.(1)d(2)b(3)d(4)c(5)a2.113.34.56895.12,66.(1)i--(2)n7.(1)chark;(2)ij8.#includestdio.hmain(){floata[10];inti;floatsum=0,average;for(i=0;i10;i++){printf(a[%d]=?,i);scanf(%f,&a[i]);sum+=a[i];}average=sum/10;printf(averageis%.2f\n,average);}9.#includestdio.h#defineN10main(){inta[N],i,target,found;for(i=0;iN;i++)scanf(%d,&a[i]);printf(Pleaseinputanumber:);scanf(%d,&target);i=0;while(iN&&target!=a[i])i++;found=iN?i:-1;printf(%d\n,found);}10.#includestdio.h#defineN80main(){charstr[N];intlen=0,i=0;printf(Pleaseinputastring:);gets(str);while(str[i++]!=0)len++;printf(thelengthofstringis%d.\n,len);}第7章1.212.1363.164.(1)func(2)m++5.96.1,6,3,2,37.2,5,1,2,3,-28.#includestdio.hintfun(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=ab?(ac?a:c):(bc?b:c);returnmax;}9.#includestdio.hlongintsum(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.#includestdio.hvoidfun(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.(1)b(2)d(3)b(4)c(5)c2.83.1234567894.234565.3456.1234567.b[i]8.bcdefgha9.p=sum10.#includestdio.h#includestring.hmain(){charstr[80];char*p1,*p2;gets(str);p1=str;p2=str+strlen(str)-1;while(p1p2&&*p1++==*p2--);puts(p1p2?不是回文:是回文);}第9章1.a.#defineF(x)(x)*(x)*(x)b.#defineF(x)(x)%4c.#defineF(x,y)(x)*(y)100?1:02.a.4b.4c.#defineDOUBLE(x)2*(x)3.d4.-205.Nisundefined6.7.57.y=68.#includestdio.h#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;2.123.合法的有a,b,dc.改成s.u.rectangle.length=25;e.改成s.u.circle.radius=5;f.改成s.u.circle.radius=5;4.a,b,c,d(说明:变量b有确定的值之后,b++是合法的。)5.改为typedefstructproduct{charname[10];floatprice;}PRODUCT;PRODUCTproducts[10];6.(1)structemployee(2)printemp(emp)7.243988.#includestdio.hstructtime_struct{inthour;intminute;intsecond;};main(){structtime_structtime;printf(Inputtime?\n(Example18:28:38)\n);scanf(%d:%d:%d,&time.hour,&time.minute,&time.second);printf(Timeis%d:%d:%d\n,time.hour,time.minute,time.second);}9.#includestdio.hstructtime_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,&time.hour,&time.minute,&time.second);}voiddisplay_time(){printf(Timeis%d:%d:%d.\n,time.hour,time.minute,time.second);}\10.#includestdio.h#defineN3structhotel{charname[31];/*旅馆名称*/charaddress[31];/*旅馆地址*/intgrade;/*旅馆级别*/floataverage_charge;/*平均房价*/intnumber;/*房间数量*/}h[N]={{h1,上海路,5,500.00,80},{h2,北京大街,5,480.00,70},{h3,南京大街,3,300.50,100}};main

1 / 15
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功