谭浩强课后习题答案

上传人:nt****6 文档编号:35913563 上传时间:2018-03-22 格式:DOC 页数:26 大小:156KB
返回 下载 相关 举报
谭浩强课后习题答案_第1页
第1页 / 共26页
谭浩强课后习题答案_第2页
第2页 / 共26页
谭浩强课后习题答案_第3页
第3页 / 共26页
谭浩强课后习题答案_第4页
第4页 / 共26页
谭浩强课后习题答案_第5页
第5页 / 共26页
点击查看更多>>
资源描述

《谭浩强课后习题答案》由会员分享,可在线阅读,更多相关《谭浩强课后习题答案(26页珍藏版)》请在金锄头文库上搜索。

1、15 请参照本章例题,编写一个 C 程序,输出以下信息:* Very Goodj!* 解:main()printf(“ * n“);printf(“n“);printf(“ Very Good! n“);printf(“n“);printf(“ *n“); 1.6 编写一个程序,输入 a b c 三个值,输出其中最大者。解:main()int a,b,c,max;printf(“请输入三个数 a,b,c:n“);scanf(“%d,%d,%d“,max=a;if(maxvoid main()char c1=a,c2=b,c3=c,c4=101,c5=116;printf(“a%c b%ctc%

2、ctabcn“,c1,c2,c3);printf(“tb%c %cn“,c4,c5);解:程序的运行结果为:aabb cc abcA N3.7 将“China“译成密码.密码规律:用原来的字母后面第 4 个字母代替原来的字母,例如,字母“A“后面第 4 个字母是“E“,用“E“代替“A“.因此,“China“应译为“Glmre“.请编一程序,用赋初值的议程使 c1,c2,c3,c4,c5 分别变成G,1,m,r,e,并输出.main()char c1=“C“,c2=“h“,c3=“i“,c4=n,c5=a;c1+=4;c2+=4;c3+=4;c4+=4;c5+=4;printf(“密码是%c%

3、c%c%c%cn“,c1,c2,c3,c4,c5);3.8 例 3.6 能否改成如下:#includevoid main()int c1,c2;(原为 char c1,c2)c1=97;c2=98;printf(“%c%cn“,c1,c2);printf(“%d%dn“,c1,c2);解:可以.因为在可输出的字符范围内,用整型和字符型作用相同.3.9 求下面算术表达式的值.(1)x+a%3*(int)(x+y)%2/4=2.5(x=2.5,a=7,y=4.7)(2)(float)(a+b)/2+(int)x%(int)y=3.5(设 a=2,b=3,x=3.5,y=2.5)3.10 写出下面程

4、序的运行结果:#includevoid main()int i,j,m,n;i=8;j=10;m=+i;n=j+;printf(“%d,%d,%d,%dn“,i,j,m,n);解:结果: 9,11,9,10第 4 章4.4.a=3,b=4,c=5,x=1.2,y=2.4,z=-3.6,u=51274,n=128765,c1=a,c2=b.想得到以下的输出格式和结果,请写出程序要求输出的结果如下:a= 3 b= 4 c= 5x=1.200000,y=2.400000,z=-3.600000x+y= 3.60 y+z=-1.20 z+x=-2.40u= 51274 n= 128765c1=a or

5、 97(ASCII)c2=B or 98(ASCII)解:main()int a,b,c;long int u,n;float x,y,z;char c1,c2;a=3;b=4;c=5;x=1.2;y=2.4;z=-3.6;u=51274;n=128765;c1=a;c2=b;printf(“n“);printf(“a=%2d b=%2d c=%2dn“,a,b,c);printf(“x=%8.6f,y=%8.6f,z=%9.6fn“,x,y,z);printf(“x+y=%5.2f y=z=%5.2f z+x=%5.2fn“,x+y,y+z,z+x);printf(“u=%6ld n=%9l

6、dn“,u,n);printf(“c1=%c or %d(ASCII)n“,c1,c2);printf(“c2=%c or %d(ASCII)n“,c2,c2);4.5 请写出下面程序的输出结果.结果:575 767.856400,-789.12396267.856400 ,-789.12396267.86,-789.12,67.856400,-789.123962,67.856400,-789.1239626.785640e+001,-7.89e+002A,65,101,411234567,4553207,d68765535,17777,ffff,-1COMPUTER, COM4.6 用下面

7、的 scanf 函数输入数据,使 a=3,b=7,x=8.5,y=71.82,c1=A,c2=a,问在键盘上如何输入?main()int a,b;float x,y;char c1,c2;scanf(“a=%d b=%d,scanf(“ x=%f y=%e“,scanf(“ c1=%c c2=%c“,解:可按如下方式在键盘上输入:a=3 b=7x=8.5 y=71.82c1=A c2=a说明:在边疆使用一个或多个 scnaf 函数时,第一个输入行末尾输入的“回车“被第二个 scanf 函数吸收,因此在第二三个 scanf 函数的双引号后设一个空格以抵消上行入的“回车“.如果没有这个空格,按上面

8、输入数据会出错,读者目前对此只留有一初步概念即可,以后再进一步深入理解.4.7 用下面的 scanf 函数输入数据使 a=10,b=20,c1=A,c2=a,x=1.5,y=-3.75,z=57.8,请问在键盘上如何输入数据?scanf(“%5d%5d%c%c%f%f%*f %f“,解:main()int a,b;float x,y,z;char c1,c2;scanf(“%5d%5d%c%c%f%f“,运行时输入:10 20Aa1.5 -3.75 +1.5,67.8注解:按%5d 格式的要求输入 a 与 b 时,要先键入三个空格,而后再打入 10 与 20。%*f是用来禁止赋值的。在输入时,

9、对应于%*f 的地方,随意打入了一个数 1.5,该值不会赋给任何变量。3.8 设圆半径 r=1.5,圆柱高 h=3,求圆周长,圆面积,圆球表面积,圆球体积,圆柱体积,用 scanf输入数据,输出计算结果,输出时要求有文字说明,取小数点后两位数字.请编程.解:main()float pi,h,r,l,s,sq,vq,vz;pi=3.1415926;printf(“请输入圆半径 r 圆柱高 h:n“);scanf(“%f,%f“,l=2*pi*r;s=r*r*pi;sq=4*pi*r*r;vq=4.0/3.0*pi*r*r*r;vz=pi*r*r*h;printf(“圆周长为: =%6.2fn“,

10、l);printf(“圆面积为: =%6.2fn“,s);printf(“圆球表面积为: =%6.2fn“,sq);printf(“圆球体积为: =%6.2fn“,vz);4.9 输入一个华氏温度,要求输出摄氏温度,公式为 C=5/9(F-32),输出要有文字说明,取两位小数.解: main()float c,f;printf(“请输入一个华氏温度:n“);scanf(“%f“,c=(5.0/9.0)*(f-32);printf(“摄氏温度为:%5.2fn“,c);第五章 逻辑运算和判断选取结构5.4 有三个整数 a,b,c,由键盘输入,输出其中最大的数.main()int a,b,c;pri

11、ntf(“请输入三个数:“);scanf(“%d,%d,%d“,if(ab)?a:b;max=(tempc)? temp:c;printf(“ A,B,C 中最大数是%d,“,max);5.5 main()int x,y;printf(“输入 x:“);scanf(“%d“,if(x9999)place=5;else if(num999)place=4;else if(num99)place=3;else if(num9)place=2;else place=1;printf(“place=%dn“,place);printf(“每位数字为:“);ten_thousand=num/10000;

12、thousand=(num-tenthousand*10000)/1000;hundred=(num-tenthousand*10000-thousand*1000)/100;ten=(num-tenthousand*10000-thousand*1000-hundred*100)/10;indiv=num-tenthousand*10000-thousand*1000-hundred*100-ten*10;switch(place)case 5:printf(“%d,%d,%d,%d,%d“,tenthousand,thousand,hundred,ten,indiv);printf(“n

13、反序数字为:“);printf(“%d%d%d%d%dn“,indiv,ten,hundred,thousand,tenthousand);break;case 4:printf(“%d,%d,%d,%d“,thousand,hundred,ten,indiv);printf(“n 反序数字为:“);printf(“%d%d%d%dn“,indiv,ten,hundred,thousand);break;case 3:printf(“%d,%d,%dn“,hundred,ten,indiv);printf(“n 反序数字为:“);printf(“%d%d%dn“,indiv,ten,hundr

14、ed);case 2:printf(“%d,%dn“,ten,indiv);printf(“n 反序数字为:“);printf(“%d%dn“,indiv,ten);case 1:printf(“%dn“,indiv);printf(“n 反序数字为:“);printf(“%dn“,indiv);5.8 1.if 语句main()long i;float bonus,bon1,bon2,bon4,bon6,bon10;/*初始化变量*/bon1=100000*0.1;bon2=100000*0.075+bon1;bon4=200000*0.05+bon2;bon6=200000*0.03+bo

15、n4;bon10=400000*0.015+bon6;printf(“请输入利润“);scanf(“%ld“,/*计算*/if(i10)branch=10;/*计算*/switch(branch)case 0:bonus=i*0.1;break;case 1:bonus=bon1+(i-100000)*0.075;break;case 2:case 3:bonus=bon2+(i-200000)*0.05;break;case 4:case 5:bonus=bon4+(i-400000)*0.03;break;case 6:case 7:case 8:case 9:bonus=bon6+(i-600000)*0.015;break;case 10:bonus=bon10+(i-1000000)*0.01;printf(“ 奖金是 %10.2f“,bonus);5.9 输入四个整数,按大小顺序输出.main()int t,a,b,c,d;printf(“请输入四个数:“);scanf(“%d,%d,%d,%d“,printf(“nn a=%d,b=%d,c=%d,d=%d n“,a,b,c,d);if(ab)t=a;a=b;b=t;if(ac)t=a;a=c;c=t;if(ad)t=a;a=d;d=t;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号