您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 交通运输 > 42C语言课后答案--谭浩强 清华大学出版社
第一章1.3写出一个C程序的构成。一个C程序可以由若干个源程序文件(分别进行编译的文件模块)组成,一个源文件可以由若干个函数和预处理命令以及全局变量声明部分组成,一个函数由数据声明部分和执行语句组成。1.5#includestdio.hvoidmain(){printf(********************);printf(\n);printf(VeryGood!);printf(\n);printf(********************);scanf(%d);}1.6编写一个C程序,输入a,b,c三个值,输出其中最大值。#includestdio.hvoidmain(){inta,b,c,max;scanf(%d,%d,%d,&a,&b,&c);if(ab){max=b;b=a;a=max;}if(ac){max=c;c=a;a=max;}printf(%d\n,a);}第三章3.5字符常量和字符串常量有什么区别?答:字符常量是一个字符,在程序中字符是用单引号括起来的。字符串常量有0个或若干个字符组合而成,在程序中字符串是用双引号括起来的,在储存时系统自动在字符串最后面加一个结束符号’\0’。3.6写出下列程序的运行结果:aa︼bb︼︼︼cc︼︼︼︼︼︼abc︼︼︼︼︼︼︼A︼N3.7#includestdio.hvoidmain(){charc1,c2,c3,c4,c5;c1='C';c2='h';c3='i';c4='n';c5='a';c1=c1+4;c2=c2+4;c3=c3+4;c4=c4+4;c5=c5+4;printf(%c%c%c%c%c,c1,c2,c3,c4,c5);}3.9求下面算术表达式的值。(1)2.5(2)3.53.109,11,9,103.12(1)24(2)10(3)60(4)0(5)0(6)0第四章4.4#includestdio.hvoidmain(){inta,b,c;floatx,y,z;longu,n;charc1,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(a=%2db=%2dc=%2d\n,a,b,c);printf(x=%8.6f,y=%8.6f,z=%9.6f\n,x,y,z);printf(x+y=%5.2fy+z=%5.2fz+x=%5.2f\n,x+y,y+z,z+x);printf(u=%6ldn=%9ld\n,u,n);printf(c1='%c'or%d(ASCII)\n,c1,c1);printf(c2='%c'or%d(ASCII)\n,c2-32,c2);}4.5请写出程序的输出结果:575767.856400,-789.12402367.856400,-789.12402367.86,-789.12,67.856400,-789.124023,67.856400,-789.1240236.785640e+01,-7.89e+02A,65,101,411234567,4553207,d68765533,177777,ffff,-1COMPUTER,COM4.6a=3︼b=7︼8.5︼71.82︼A︼a4.7︼︼︼10︼︼︼20Aa1.5︼-3.75︼1︼67.84.8#includestdio.hvoidmain(){floatr,h,a,b,c,d,e;floatp=3.141592653;scanf(r=%fh=%f,&r,&h);a=2*p*r;b=p*r*r;c=4*p*r*r;d=(3.0/4.0)*p*r*r*r;e=p*r*r*h;printf(yuanzhouchang:a=%.2f\n,a);printf(yuanmianji:b=%.2f\n,b);printf(yuanqiubiaomianji:c=%.2f\n,c);printf(yuanqiutiji:d=%.2f\n,d);printf(yuanzhutiji:e=%.2f\n,e);}4.10(1)#includestdio.hvoidmain(){charc1,c2;printf(pleaseprintc1,c2:);c1=getchar();c2=getchar();printf(useputchartoprinttheresult:);putchar(c1);putchar(c2);printf(\n);printf(useprintfprinttheresult:);printf(%c%c\n,c1,c2);}c1和c2可以定义为字符型或整型,二者皆可。(2)要使程序结果输出c1和c2的ASCII码,要在printf函数中用%d格式符输出,即:printf(%d%d\n,c1,c2);(3)字符变量在计算机内占用一个字节,而整型变量占用两个字节。因此整型变量在可输出字符的范围内(ASCII码为0~255之间的字符)是可以与字符数据互相转换的。如果整数在此范围外,则不能替代。第五章习题5.3#includestdio.hvoidmain(){inta,b,c,x,y;a=3;b=4;c=5;printf(%d\n,a+bc&&b==c);printf(%d\n,a||b+c&&b-c);printf(%d\n,!(ab)&&!c||1);printf(%d\n,!(x=a)&&(y=b)&&0);printf(%d\n,!(a+b)+c-1&&b+c/2);}5.4#includestdio.hvoidmain(){inta,b,c;scanf(%d,%d,%d,&a,&b,&c);if(ab)if(bc)printf(max=%d\n,c);elseprintf(max=%d\n,b);elseif(ac)printf(max=%d\n,c);elseprintf(max=%d\n,a);}5.5#includestdio.hvoidmain(){intx,y;scanf(%d,&x);if(x1)y=x;elseif(x=10)y=3*x-11;elsey=2*x-1;printf(x=%d,y=%d\n,x,y);}5.6#includestdio.hvoidmain(){floatscore;chargrade;printf(student'sscoreis:);scanf(%f,&score);switch((int)(score/10)){case10:case9:grade='A';break;case8:grade='B';break;case7:grade='C';break;case6:grade='D';break;default:grade='E';}printf(gradeis%c\n,grade);}5.9#includestdio.hvoidmain(){inta,b,c,d,t;printf(pleaseprintfournumbers:);scanf(%d%d%d%d,&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;}if(bc){t=b;b=c;c=t;}if(bd){t=b;b=d;d=t;}if(cd){t=c;c=d;d=t;}printf(theorderis:%d%d%d%d\n,a,b,c,d);}第六章习题6.1#includestdio.hvoidmain(){intm,n,t,s,i;scanf(m=%dn=%d,&m,&n);if(mn){t=m;m=n;n=t;}s=m*n;while(m!=0){i=n%m;n=m;m=i;}printf(highestcommondivissor=%d\n,n);printf(leastcommonmultiple=%d\n,s/n);}6.4#includestdio.hvoidmain(){intn;floats,i;s=0;i=1;for(n=1;n=20;n++){i=i*n;s=s+i;}printf(%.1f\n,s);}6.6#includestdio.hvoidmain(){intn,a,b,c;for(n=100;n1000;n++){a=n/100;b=n/10-a*10;c=n%10;if(n==a*a*a+b*b*b+c*c*c)printf(%d,n);}printf(\n);}6.8#includestdio.hvoidmain(){inti,t;floata,b,c;a=1;b=2;c=0;for(i=1;i=20;i++){c=c+b/a;t=b;b=a+b;a=t;}printf(%f\n,c);}6.11#includestdio.h#includemath.hvoidmain(){floata,x0,x1;printf(thenumberis:);scanf(%f,&a);x0=a;x1=(x0+a/x0)/2;while(fabs(x0-x1)=1e-5){x0=x1;x1=(x0+a/x0)/2;}printf(thesquarerootof%.0fis:%f\n,a,x1);}6.14#includestdio.hvoidmain(){inti,j,n;for(i=0;i=3;i++){for(j=0;j=2-i;j++)printf();for(n=0;n=2*i;n++)printf(*);printf(\n);}for(i=0;i=2;i++){for(j=0;j=i;j++)printf();for(n=0;n=4-2*i;n++)printf(*);printf(\n);}}第七章习题7.2#includestdio.hvoidmain(){inti,j,min,t,a[11];for(i=1;i=10;i++){printf(a[%d]=,i);scanf(%d,&a[i]);}printf(\n);for(i=1;i=10;i++)printf(%5d,a[i]);printf(\n);for(i=1;i=9;i++){min=i;for(j=i+1;j=10;j++)if(a[min]a[j])min=j;t=a[i];a[i]=a[min];a[min]=t;}printf(\nThesortednumbers:\n);for(i=1;i=10;i++)printf(%5d,a[i]);}7.4#includestdio.hvoidmain(){inta[5]={2,4,6,8};intnum,i,j;printf(arraya:\n);for(i=0;i4;i++)printf(%5d,a[i]);printf(\n);printf(enterdatais:);scanf(%d,&num);if(numa[3])a[4]=num;else{for(i=0;i4;i++)if(a[i]num){for(j=3;j=i;j--)a[j+1]=a[j];a[i]=num;break;}}printf(newarraya:\n);for(i=0;i5;i++)printf(%5d,a[i]);printf(\n);}7.5#includestdio.hvoidmain(){inti,j,a[5];printf(enterarraya:\n);for(i=0;i5;i++)scanf(%d,&a[i]);printf(arraya:\n);for(i=0;i5;i++)printf(%5d,a[i]);printf(\n);for(i=0;i5/2;i++){j=a[i];a[i]=a[4-i];a[4-i]=j;}printf(newarra
本文标题:42C语言课后答案--谭浩强 清华大学出版社
链接地址:https://www.777doc.com/doc-4442035 .html