本篇文章给大家谈谈c语言程序设计实训教程答案,以及c语言程序设计实训教程答案详解对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
1、急求===《C语言程序设计实验·设计·习题》的答案2、求C语言程序设计习题集与上机指导-第三版 (及答案)……3、C语言程序设计教程(第二版) 周宇 课后答案4、C语言程序设计教程答案~有追加悬赏100分!5、c语言程序设计教程课后习题答案 黄复贤的
急求===《C语言程序设计实验·设计·习题》的答案
这是谭浩强版习题题答案,看与你的是否一致
1.5请参照本章例题,编写一个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”,a,b,c);
max=a;
if(maxB)
max=b;
if(maxC)
max=c;
printf(“最大数为:%d”,max);
}
第三章
3.3 请将下面各数用八进制数和十六进制数表示:
(1)10 (2)32 (3)75 (4)-617
(5)-111 (6)2483 (7)-28654 (8)21003
解:十 八 十六
(10)=(12)=(a)
(32)=(40)=20
(75)=(113)=4b
(-617)=(176627)=fd97
-111=177621=ff91
2483=4663=963
-28654=110022=9012
21003=51013=520b
3.5字符常量与字符串常量有什么区别?
解:字符常量是一个字符,用单引号括起来。字符串常量是由0个或若干个字符
而成,用双引号把它们括起来,存储时自动在字符串最后加一个结束符号’\0′.
3.6写出以下程序的运行结果:
#include
void main()
{
char c1=’a’,c2=’b’,c3=’c’,c4=’\101′,c5=’\116′;
printf(“a%c b%c\tc%c\tabc\n”,c1,c2,c3);
printf(“\t\b%c %c\n”,c4,c5);
解:程序的运行结果为:
aabb cc abc
A N
3.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%c%c%c%c\n”,c1,c2,c3,c4,c5);
}
3.8例3.6能否改成如下:
#include
void main()
{
int c1,c2;(原为 char c1,c2)
c1=97;
c2=98;
printf(“%c%c\n”,c1,c2);
printf(“%d%d\n”,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写出下面程序的运行结果:
#include
void main()
{
int i,j,m,n;
i=8;
j=10;
m=++i;
n=j++;
printf(“%d,%d,%d,%d\n”,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= 5
x=1.200000,y=2.400000,z=-3.600000
x+y= 3.60 y+z=-1.20 z+x=-2.40
u= 51274 n= 128765
c1=’a’ or 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=%2d\n”,a,b,c);
printf(“x=%8.6f,y=%8.6f,z=%9.6f\n”,x,y,z);
printf(“x+y=%5.2f y=z=%5.2f z+x=%5.2f\n”,x+y,y+z,z+x);
printf(“u=%6ld n=%9ld\n”,u,n);
printf(“c1=’%c’ or %d(ASCII)\n”,c1,c2);
printf(“c2=’%c’ or %d(ASCII)\n”,c2,c2);
}
4.5请写出下面程序的输出结果.
结果:
57
5 7
67.856400,-789.123962
67.856400 ,-789.123962
67.86,-789.12,67.856400,-789.123962,67.856400,-789.123962
6.785640e+001,-7.89e+002
A,65,101,41
1234567,4553207,d687
65535,17777,ffff,-1
COMPUTER, COM
4.6用下面的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,a,b);
scanf(” x=%f y=%e”,x,y);
scanf(” c1=%c c2=%c”,c1,c2);
}
解:可按如下方式在键盘上输入:
a=3 b=7
x=8.5 y=71.82
c1=A c2=a
说明:在边疆使用一个或多个scnaf函数时,第一个输入行末尾输入的”回车”被第二
个scanf函数吸收,因此在第二\三个scanf函数的双引号后设一个空格以抵消上行
入的”回车”.如果没有这个空格,按上面输入数据会出错,读者目前对此只留有一
初步概念即可,以后再进一步深入理解.
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”,a,b,c1,c2,y,z);
解:
main()
{
int a,b;
float x,y,z;
char c1,c2;
scanf(“%5d%5d%c%c%f%f”,a,b,c1,c2,x,y,z);
}
运行时输入:
10 20Aa1.5 -3.75 +1.5,67.8
注解:按%5d格式的要求输入a与b时,要先键入三个空格,而后再打入10与20。%*f
是用来禁止赋值的。在输入时,对应于%*f的地方,随意打入了一个数1.5,该值不
会赋给任何变量。
4.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”,r,h);
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.2f\n”,l);
printf(“圆面积为: =%6.2f\n”,s);
printf(“圆球表面积为: =%6.2f\n”,sq);
printf(“圆球体积为: =%6.2f\n”,vz);
}
4.9输入一个华氏温度,要求输出摄氏温度,公式为C=5/9(F-32),输出要有文字说明,
取两位小数.
解: main()
{
float c,f;
printf(“请输入一个华氏温度:\n”);
scanf(“%f”,f);
c=(5.0/9.0)*(f-32);
printf(“摄氏温度为:%5.2f\n”,c);
}
第五章 逻辑运算和判断选取结构
5.4有三个整数a,b,c,由键盘输入,输出其中最大的数.
main()
{
int a,b,c;
printf(“请输入三个数:”);
scanf(“%d,%d,%d”,a,b,c);
if(aB)
if(bC)
printf(“max=%d\n”,c);
else
printf(“max=%d\n”,b);
else if(aC)
printf(“max=%d\n”,c);
else
printf(“max-%d\n”,a);
}
方法2:使用条件表达式.
main()
{int a,b,c,termp,max;
printf(” 请输入 A,B,C: “);
scanf(“%d,%d,%d”,a,b,c);
printf(“A=%d,B=%d,C=%d\n”,a,b,c);
temp=(ab)?a:b;
max=(tempc)? temp:c;
printf(” A,B,C中最大数是%d,”,max);
}
5.5 main()
{int x,y;
printf(“输入x:”);
scanf(“%d”,x);
if(x1)
{y=x;
printf(“X-%d,Y=X=%d \n”,x,y);
}
else if(x10)
{y=2*x-1;
printf(” X=%d, Y=2*X-1=%d\n”,x,y);
}
else
{y=3*x-11;
printf(“X=5d, Y=3*x-11=%d \n”,x,y);
}
}
(习题5-6:)自己写的已经运行成功!不同的人有不同的算法,这些答案仅供参考! 818pp.com
# include
void main()
{
float s,i;
char a;
scanf(“%f”,s);
while(s100||s0)
{
printf(“输入错误!error!”);
scanf(“%f”,s);
}
i=s/10;
switch((int)i)
{
case 10:
case 9: a=’A’;break;
case 8: a=’B’;break;
case 7: a=’C’;break;
case 6: a=’D’;break;
case 5:
case 4:
case 2:
case 1:
case 0: a=’E’;
}
printf(“%c”,a);
}
5.7给一个不多于5位的正整数,要求:1.求它是几位数2.分别打印出每一位数字3.
按逆序打印出各位数字.例如原数为321,应输出123.
main()
{
long int num;
int indiv,ten,hundred,housand,tenthousand,place;
printf(“请输入一个整数(0-99999):”);
scanf(“%ld”,num);
if(num9999)
place=5;
else if(num999)
place=4;
else if(num99)
place=3;
else if(num9)
place=2;
else place=1;
printf(“place=%d\n”,place);
printf(“每位数字为:”);
ten_thousand=num/10000;
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反序数字为:”);
printf(“%d%d%d%d%d\n”,indiv,ten,hundred,thousand,tenthousand);
break;
case 4:printf(“%d,%d,%d,%d”,thousand,hundred,ten,indiv);
printf(“\n反序数字为:”);
printf(“%d%d%d%d\n”,indiv,ten,hundred,thousand);
break;
case 3:printf(“%d,%d,%d\n”,hundred,ten,indiv);
printf(“\n反序数字为:”);
printf(“%d%d%d\n”,indiv,ten,hundred);
case 2:printf(“%d,%d\n”,ten,indiv);
printf(“\n反序数字为:”);
printf(“%d%d\n”,indiv,ten);
case 1:printf(“%d\n”,indiv);
printf(“\n反序数字为:”);
printf(“%d\n”,indiv);
}
}
5.8
1.if语句
main()
{long i;
float bonus,bon1,bon2,bon4,bon6,bon10;
bon1=100000*0.1;
bon2=bon1+100000*0.075;
bon4=bon2+200000*0.05;
bon6=bon4+200000*0.03;
bon10=bon6+400000*0.015;
scanf(“%ld”,i);
if(i=1e5)bonus=i*0.1;
else if(i=2e5)bonus=bon1+(i-100000)*0.075;
else if(i=4e5)bonus=bon2+(i-200000)*0.05;
else if(i=6e5)bonus=bon4+(i-400000)*0.03;
else if(i=1e6)bonus=bon6+(i-600000)*0.015;
else bonus=bon10+(i-1000000)*0.01;
printf(“bonus=%10.2f”,bonus);
}
用switch语句编程序
main()
{long i;
float bonus,bon1,bon2,bon4,bon6,bon10;
int branch;
bon1=100000*0.1;
bon2=bon1+100000*0.075;
bon4=bon2+200000*0.05;
bon6=bon4+200000*0.03;
bon10=bon6+400000*0.015;
scanf(“%ld”,i);
branch=i/100000;
if(branch10)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(“bonus=%10.2f”,bonus);
}
5.9 输入四个整数,按大小顺序输出.
main()
{int t,a,b,c,d;
printf(“请输入四个数:”);
scanf(“%d,%d,%d,%d”,a,b,c,d);
printf(“\n\n 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;}
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(“\n 排序结果如下: \n”);
printf(” %d %d %d %d \n”,a,b,c,d);
}
5.10塔
main()
{
int h=10;
float x,y,x0=2,y0=2,d1,d2,d3,d4;
printf(“请输入一个点(x,y):”);
scanf(“%f,%f”,x,y);
d1=(x-x0)*(x-x0)+(y-y0)(y-y0);
d2=(x-x0)*(x-x0)+(y+y0)(y+y0);
d3=(x+x0)*(x+x0)+(y-y0)*(y-y0);
d4=(x+x0)*(x+x0)+(y+y0)*(y+y0);
if(d11 d21 d31 d41)
h=0;
printf(“该点高度为%d”,h);
}
求C语言程序设计习题集与上机指导-第三版 (及答案)……
C语言程序设计第三版谭浩强课后习题答案完整版
第一章
1.5请参照本章例题,编写一个C程序,输出以下信息:
**************************
Very Good!
**************************
解:
mian()
{printf(“**************************”);
printf(“\n”);
printf(“Very Good!\n”);
printf(“\n”);
printf(“**************************”);
}
1.6 编写一个程序,输入a、b、c三个值,输出其中最大值。
解:
mian()
{int a,b,c,max;
printf(“请输入三个数a,b,c:\n”);
scanf(“%d,%d,%d”,a,b,c);
max=a;
if(maxb)
max=b;
if(maxc)
max=c;
printf(“最大数为:“%d”,max);
}
第三章
3.6写出以下程序运行的结果。
main()
{char c1=’a’,c2=’b’,c3=’c’,c4=’\101’,c5=’\116’;
printf(“a%cb%c\tc%c\tabc\n”,c1,c2,c3);
printf(“\t\b%c %c”,c4,c5);
}
解:
aaㄩbbㄩㄩㄩccㄩㄩㄩㄩㄩㄩabc
AㄩN
3.7 要将”China”译成密码,译码规律是:用原来字母后面的第4个字母代替原来的字母.例如,字母”A”后面第4个字母是”E”.”E”代替”A”。因此,”China”应译为”Glmre”。请编一程序,用赋初值的方法使cl、c2、c3、c4、c5五个变量的值分别为,’C’、’h’、’i’、’n’、’a’,经过运算,使c1、c2、c3、c4、c5分别变为’G’、’l’、’m’、’r’、’e’,并输出。
解:
#include stdio.h
main()
{ char c1=’C’,c2=’h’,c3=’i’,c4=’n’,c5=’a’;
c1+=4;
c2+=4;
c3+=4;
c4+=4;
c5+=4;
printf(“密码是%c%c%c%c%c\n”,c1,c2,c3,c4,c5);
}
运行结果:
密码是Glmre
3.9求下面算术表达式的值。
(1)x+a%3*(int)(x+y)%2/4
设x=2.5,a=7,y=4.7
(2)(float)(a+b)/2+(int)x%(int)y
设a=2,b=3,x=3.5,y=2.5
(1)2.5
(2)3.5
3.10写出程序运行的结果。
main()
{int i,j,m,n;
i=8;
j=10;
m=++i;
n=j++;
printf(“%d,%d,%d,%d”,i,j,m,n);
}
解:
9,11,9,10
3.12 写出下面表达式运算后a的值,设原来a=12。设a和n都已定义为整型变量。
(1)a+=a (2) a-=2 (3) a*=2+3 (4)a/=a+a
(5) a%=(n%=2),n的值等于5
(6)a+=a-=a*=a
解:
(1) 24 (2) 10 (3) 60 (4) 0 (5) 0 (6) 0
第四章
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=_5
x=1.200000,y=2.400000,z=-3.600000
x+y=_3.600_ _y+z=-1.20_ _z+x=-2.40
c1=ˊaˊ_or_97(ASCII)
c2=ˊbˊ_or_98(ASCII)
main()
{int a=3,b=4,c=5;
long int u=51274,n=128765;
float x=1.2,y=2.4,z=3.6;
char c1=’a’,c2=’b’;
printf(“a=%2d b=%2d c=%2d\n”,a,b,c);
printf(“x=%f,y=%f,z=%f\n”,x,y,z);
printf(“x+y=%5.2f y+z=%5.2f z+x=%5.2f\n”,x+y,y+z,z+x);
printf(“u=%6ld n=%9ld\n”,u,n);
printf(“%s %s %d%s\n”,”c1=’a’”,”or”,c1,”(ASCII)”);
printf(“%s %s %d%s\n”,”c2=’a’”,”or”,c2,”(ASCII)”);
}
4.7用scanf下面的函数输入数据,使a=3,b=7,x=8.5,y=71.82,c1=ˊAˊ,c2=ˊaˊ,问在键盘上如何输入?
main()
{
int a,b;float x,y;char c1c2;
scanf(”a=%d_b=%d”,a,b);
scanf(”_x=%f_y=%e”,x,y);
scanf(”_c1=%c_c2=%c”,c1,c2);
}
a=3_b=7
_x=8.5_y=71.82
_c1=A_c2=a
4.8设圆半径r=1.5,圆柱高h=3,求圆周长、圆面积、圆球表面积、圆球体积、圆柱体积。用scanf输入数据,输出计算结果,输出时要求文字说明,取小数点后两位数字。请编程序。
main()
{float r,h,C1,Sa,Sb,Va,Vb;
scanf(“%f,%f”,r,h);
C1=2*3.14*r;
Sa=3.14*r*r;
Sb=4*Sa;
Va=4*3.14*r*r*r/3;
Vb=Sa*h;
printf(“C1=%.2f\n”,C1);
printf(“Sa=%.2f\nSb=%.2f\nVa=%.2f\nVb=%.2f\n”,Sa,Sb,Va,Vb);
}
4.9输入一个华氏温度,要求输出摄氏温度。公式为
c=5(F-32)/9
输出要求有文字说明,取位2小数。
main()
{float F,c;
scanf(“%f”,F);
c=5*(F-32)/9;
printf(“c=%.2f”,c);
}
4.10编程序,用getchar函数读入两个字符给c1、c2,然后分别用函数和函数输出这两个字符。并思考以下问题:(1)变量c1、c2应定义为字符型或整形?抑二者皆可?(2)要求输出c1和c2值的ASCII码,应如何处理?用putchar函数还是printf函数?(3)整形变量与字符变量是否在任何情况下都可以互相代替?如:
char c1,c2;
与
int c1,c2;
是否无条件的等价?
#include”stdio.h”
main()
{char c1,c2;
c1=getchar();c2=getchar();
putchar(c1);putchar(’\n’);putchar(c2);putchar(’\n’);
and Settings/Administrator/桌面/C语言程序设计第三版谭浩强课后习题答案完整版.txt(第 1/19 页)[2010-5-10 9:14:34]
and Settings/Administrator/桌面/C语言程序设计第三版谭浩强课后习题答案完整版.txt
1.5请参照本章例题,编写一个C程序,输出以下信息:
**************************
Very Good!
**************************
解:
mian()
{printf(“**************************”);
printf(“\n”);
printf(“Very Good!\n”);
printf(“\n”);
printf(“**************************”);
}
1.6 编写一个程序,输入a、b、c三个值,输出其中最大值。
解:
mian()
{int a,b,c,max;
printf(“请输入三个数a,b,c:\n”);
scanf(“%d,%d,%d”,a,b,c);
max=a;
if(maxb)
max=b;
if(maxc)
max=c;
printf(“最大数为:“%d”,max);
}
第三章
3.6写出以下程序运行的结果。
main()
{char c1=’a’,c2=’b’,c3=’c’,c4=’\101’,c5=’\116’;
printf(“a%cb%c\tc%c\tabc\n”,c1,c2,c3);
printf(“\t\b%c %c”,c4,c5);
}
解:
aaㄩbbㄩㄩㄩccㄩㄩㄩㄩㄩㄩabc
AㄩN
3.7 要将”China”译成密码,译码规律是:用原来字母后面的第4个字母代替原来的字母.例如,字母”A”后面第4个字母是”E”.”E”代替”A”。因此,”China”应译为”Glmre”。请编一程序,用赋初值的方法使cl、c2、c3、c4、c5五个变量的值分别为,’C’、’h’、’i’、’n’、’a’,经过运算,使c1、c2、c3、c4、c5分别变为’G’、’l’、’m’、’r’、’e’,并输出。
解:
#include stdio.h
main()
{ char c1=’C’,c2=’h’,c3=’i’,c4=’n’,c5=’a’;
c1+=4;
c2+=4;
c3+=4;
c4+=4;
c5+=4;
printf(“密码是%c%c%c%c%c\n”,c1,c2,c3,c4,c5);
}
运行结果:
密码是Glmre
3.9求下面算术表达式的值。
(1)x+a%3*(int)(x+y)%2/4
设x=2.5,a=7,y=4.7
(2)(float)(a+b)/2+(int)x%(int)y
设a=2,b=3,x=3.5,y=2.5
(1)2.5
(2)3.5
3.10写出程序运行的结果。
main()
{int i,j,m,n;
i=8;
j=10;
m=++i;
n=j++;
printf(“%d,%d,%d,%d”,i,j,m,n);
}
解:
9,11,9,10
3.12 写出下面表达式运算后a的值,设原来a=12。设a和n都已定义为整型变量。
(1)a+=a (2) a-=2 (3) a*=2+3 (4)a/=a+a
(5) a%=(n%=2),n的值等于5
(6)a+=a-=a*=a
解:
(1) 24 (2) 10 (3) 60 (4) 0 (5) 0 (6) 0
第四章
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=_5
x=1.200000,y=2.400000,z=-3.600000
x+y=_3.600_ _y+z=-1.20_ _z+x=-2.40
c1=ˊaˊ_or_97(ASCII)
c2=ˊbˊ_or_98(ASCII)
main()
{int a=3,b=4,c=5;
long int u=51274,n=128765;
float x=1.2,y=2.4,z=3.6;
char c1=’a’,c2=’b’;
printf(“a=%2d b=%2d c=%2d\n”,a,b,c);
printf(“x=%f,y=%f,z=%f\n”,x,y,z);
printf(“x+y=%5.2f y+z=%5.2f z+x=%5.2f\n”,x+y,y+z,z+x);
printf(“u=%6ld n=%9ld\n”,u,n);
printf(“%s %s %d%s\n”,”c1=’a’”,”or”,c1,”(ASCII)”);
printf(“%s %s %d%s\n”,”c2=’a’”,”or”,c2,”(ASCII)”);
}
4.7用scanf下面的函数输入数据,使a=3,b=7,x=8.5,y=71.82,c1=ˊAˊ,c2=ˊaˊ,问在键盘上如何输入?
main()
{
int a,b;float x,y;char c1c2;
scanf(”a=%d_b=%d”,a,b);
scanf(”_x=%f_y=%e”,x,y);
scanf(”_c1=%c_c2=%c”,c1,c2);
}
a=3_b=7
_x=8.5_y=71.82
_c1=A_c2=a
4.8设圆半径r=1.5,圆柱高h=3,求圆周长、圆面积、圆球表面积、圆球体积、圆柱体积。用scanf输入数据,输出计算结果,输出时要求文字说明,取小数点后两位数字。请编程序。
main()
{float r,h,C1,Sa,Sb,Va,Vb;
scanf(“%f,%f”,r,h);
C1=2*3.14*r;
Sa=3.14*r*r;
Sb=4*Sa;
Va=4*3.14*r*r*r/3;
Vb=Sa*h;
printf(“C1=%.2f\n”,C1);
printf(“Sa=%.2f\nSb=%.2f\nVa=%.2f\nVb=%.2f\n”,Sa,Sb,Va,Vb);
}
4.9输入一个华氏温度,要求输出摄氏温度。公式为
c=5(F-32)/9
输出要求有文字说明,取位2小数。
main()
{float F,c;
scanf(“%f”,F);
c=5*(F-32)/9;
printf(“c=%.2f”,c);
}
4.10编程序,用getchar函数读入两个字符给c1、c2,然后分别用函数和函数输出这两个字符。并思考以下问题:(1)变量c1、c2应定义为字符型或整形?抑二者皆可?(2)要求输出c1和c2值的ASCII码,应如何处理?用putchar函数还是printf函数?(3)整形变量与字符变量是否在任何情况下都可以互相代替?如:
char c1,c2;
与
int c1,c2;
是否无条件的等价?
#include”stdio.h”
main()
{char c1,c2;
c1=getchar();c2=getchar();
putchar(c1);putchar(’\n’);putchar(c2);putchar(’\n’);
switch(place)
{ case 5: printf(“%d,%d,%d,%d,%d”,ten_thousand,thousand,hundred,ten,indiv);
printf(“\n反序数字为;”);
printf(“%d%d%d%d%d\n”,indiv,ten,hundred,thousand,ten_thousand);
break;
case 4: printf(“%d,%d,%d,%d”,thousand,hundred,ten,indiv);
printf(“\n反序数字为:”);
printf(“%d%d%d%d\n”,indiv,ten,hundred,thousand);
break;
case 3: printf(“%d,%d,%d”,hundred,ten,indiv);
printf(“\n反序数字为:”);
printf(“%d%d%d\n”,indiv,ten,hundred);
break;
case 2: printf(“%d,%d”,ten,indiv);
printf(“\n反序数字为:”);
printf(“%d%d\n”,indiv,ten);
break;
case 1: printf(“%d”,indiv);
printf(“\n反序数字为:”);
printf(“%d\n”,indiv);
break;
}
}
第五章
5.8 企业发放的奖金根据利润提成。利润I低于或等于10万元时,奖金可提成10% ;利润高于10万元,低于20万元(100000I≤200000)时,其中10万元按10%提成,高于10万元的部分,可提成7.5% ;200000I≤400000时,其中20万元仍按上述办法提成(下同),高于20万元的部分按5%提成;400000I≤600000时,高于40万元的部分按3%提成;600000〈I≤1000000时,高于60万的部分按1.5%提成;I1000000时,超过100万元的部分按1%提成。从键盘输入当月利润I,求应发放奖金总数。要求:(1)用if语句编程序;(2)用switch语句编程序。
解:计算利润时,要特别注意不同利润的不同提成比例。例如,利润为15万元,其中有10万元按10%的比例提成,另外5万元则按7.5%提成。
(1) 用if语句编程序。
#include stdio.h
main()
{ long i;
float bonus,bon1,bon2,bon4,bon6,bon10;
bon1=100000*0.1; /*利润为10万元时的奖金*/
bon2=bon1+100000*0.075; /*利润为20万元时的奖金*/
bon4=bon2+200000*0.05; /*利润为40万元时的奖金*/
bon6=bon4+200000*0.03; /*利润为60万元时的奖金*/
bon10=bon6+400000*0.015; /*利润为100万元时的奖金*/
printf(“请输入利润i:”);
scanf(“%ld”,i);
if(i=100000)
bonus=i*0.1; /*利润在10万元以内按0.1提成奖金*/
else if(i=200000)
bonus=bon1+(i-100000)*0.075; /*利润在10万至20万元时的奖金*/
else if(i=400000)
bonus=bon2+(i-200000)*0.05; /*利润在20万至40万元时的奖金*/
else if(i=600000)
bonus=bon4+(i-400000)*0.03; /*利润在40万至60万元时的奖金*/
else if(i=1000000)
bonus=bon6+(i-600000)*0.015; /*利润在60万至100万元时的奖金*/
else
bonus=bon10+(i-1000000)*0.01; /*利润在100万元以上时的奖金*/
printf(”奖金是%10.2f\n”,bonus);
}
此题的关键在于正确写出每一区间的奖金计算公式。例如利润在10万元至20万时,奖金应由两部分组成:①利润为10万元时应得的奖金。即100000ⅹ0.1;②10万元以上部分应得的奖金。即(num-100000)ⅹ0.075。同理,20万~40万这个区间的奖金也应由两部分组成:①利润为20万元时应得的奖金,即100000ⅹ0.1ⅹ10万ⅹ0.075;②20万元以上部分应得的奖金,即(num-200000)ⅹ0.05。程序中先把10万、20万、40万、60万、100万各关键点的奖金计算出来,即bon1、bon2、bon4、bon6、hon10;然后再加上各区间附加部分的奖金。
(2) 用switch语句编程序。
输入利润i,确定相应的提成等级branch
根据branch确定奖金值
0 奖金=i*0.1
1 奖金=bon1+(i-105)*0.075
2 奖金=bon2+(i-2*105)*0.05
3
4 奖金=bon4+(i-4*105)*0.03
5
6 奖金=bon6+(i-6*105)*0.015
7
8
9
10 奖金=bon10+(i-106)*0.01
输出奖金
#include stdio.h
main()
{ long i;
float bonus, bon1, bon2, bon4, bon6, bon10;
int c;
bon1=100000*0.1;
bon2=bon1+100000*0.075;
bon4=bon2+200000*0.05;
bon6=bon4+200000*0.03;
bon10=bon6+400000*0.015;
printf(“请输入利润i:”);
scanf(“%ld”,i);
c=i/100000;
if(c10) c=10;
switch(c)
{ case 0: bonus=1*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 输入4个整数,要求按由大到小的顺序输出。
解:此题采用依次比较的方法排出其大小顺序。在学习了循环和数组以后,可以有更多的排序方法。
#include stdio.h
main()
{ int t,a,b,c,d;
printf(“请输入4个整数:”);
scanf(“%d,%d,%d,%d”,a,b,c,d);
printf(“\n 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;}
if(bc) {t=a; b=c; c=t;}
if(bd) {t=b; b=d; d=t;}
if(cd) {t=c; c=d; d=t;}
printf(“排序结果如下:\n”);
C语言程序设计教程(第二版) 周宇 课后答案
二、 1. I love China! printf(“we are students.\n”) 2. 6 项目实训题参考答案 1.编写一个C程序,输出以下信息: * * * * * * * * * * * * * * * * * * * * I am a student! * * * * * * * * * * * * * * * * * * * * main() { printf(“********************\n”); printf(” I am a student!\n “); printf(“********************\n”); } 2222....已知立方体的长、宽、高分别是10cm、20cm、15cm,编写程序,求立方体体积。 解: main() { int a,b,c,v; a=10; b=20; c=15; v=a*b*c; printf(“v=%d”,v); } 本程序运行结果为: v=3000 第第第第2章章章章 编制编制编制编制C程序的基础知识程序的基础知识程序的基础知识程序的基础知识 一 选择题 C B A B A C C 二 操作题 2 21. 3,2,-8,2 3.000000,2.500000,-8.000000 2. ABC DE FGH why is 21+35 equal 52 3. 3 1 4 3 2 3 1 2 4. aa bb cc abc A N 项目实训题 1.定义一个符号常量M为5和一个变量n值为2,把它们的乘积输出。 #define M 5 main() { int n,c; n=2; c=M*n; printf(“%d\n”,c); } 2.编程求下面算术表达式的值。 (1)x+a%3*(int)(x+y)%2/4,设x=2.5,a=7,y=4.7; (2)(float)(a+b)/2+(int)x%(int)y,设a=2,b=3,x=3.5,y=2.5。 (1)main() { int a=7; float x=2.5,y=4.7; printf(“%f\n”,x+a%3*(int)(x+y)%2/4); } (2)main() { int a=2,b=3; float x=3.5,y=2.5; printf(“%f\n”,(float)(a+b)/2+(int)x%(int)y); 第三章第三章第三章第三章 顺序结构程序设计顺序结构程序设计顺序结构程序设计顺序结构程序设计 一 选择题 A C D C C 二 操作题 1. x=3,a=2,b=3 2. z=12.700000 3. 1 2 1 a 2 1 2 三三三三....编程题 编程题编程题编程题编程题 1. 某工种按小时计算工资,每月劳动时间(小时)×每小时工资=总工资,总工资中扣除10%公积金,剩余的为应发工资。编写一个程序从键盘输入劳动时间和每小时工资,打印出应发工资。 解: #include stdio.h main() { float sj,gz,yfgz; printf(“time,salary:”); scanf(“%f,%f”,sj,gz); yfgz=sj*gz*0.9; printf(“total salary:%f\n”,yfgz); } 本程序运行结果为: time,salary:4,3CR total salary:10.800000 2.编写一个程序求出任意一个输入字符的ASCII码 解: #include stdio.h main() { char c; printf(“Input a string:”); scanf(“%c”,c); printf(“%c ASCII is %d\n”,c,c); } 本程序运行结果为: Input a string:aCR a ASCII is 97 3、编写一个程序用于水果店售货员算帐:已知苹果每斤2.50元,鸭梨每斤1.80元,香蕉每斤2元,橘子每斤1.6元,要求输入各类水果的重量,打印出应付第四章第四章第四章第四章 选择结构程序设计选择结构程序设计选择结构程序设计选择结构程序设计 一、略 二、B B A B C B A 三、1. 1 0 2. 2 3 2 2 3. 10 20 0 4. ch=’A’ch=’Z’||ch=’a’ch=’z’ ch=’0’ch=’9’ ch==’ ’ 5. -1 四、上机操作 1. 从键盘输入一个英文字母,如果是大写字母,则将它变为小写字母输出;如果是小写字母,则将其变为大写字母输出。 #includestdio.h main() {char ch; ch=getchar(); if(ch=’A’ch=’Z’) ch+=32; else if(ch=’a’ch=’z’) ch-=32; putchar(ch); putchar(‘\n’); } 2. 根据输入的x值依据下列表达式,计算y的值。 2x (x-1) y = 3 (x=-1) 4+x (x-1) 解: main() { float x,y; scanf(“%f”,x); if(x-1) y=2*x; else if(x==1) y=3; else y=4+x; printf(“y=%f”,y); } 本程序运行结果为: -2CR y=2.000000 3.编写程序,输入一个整数,判断它是奇数还是偶数,若是奇数,输出“Is Odd“;若是偶数,输出“Is Even“。 main() { int x; scanf(“%d”,x); if(x%2==0) printf(“Is Even\n”); else printf(“Is Odd\n”); } 4.设计应用程序,求二次方程ax2+bx+c=0的解。 #includemath.h main() { float a,b,c,disc,x1,x2,p,q; scanf(“%f,%f,%f”,a,b,c); if(fabs(a)=1e-6) printf(” The equation is not a quadratic\n”); else { disc=b*b-4*a*c; if(fabs(disc) 1e-6) printf(“x1=x2=%8.4f\n”,-b/(2*a)); else if(disc1e-6) {x1=(-b+sqrt(disc)/(2*a)); x2=(-b-sqrt(disc)/(2*a)); printf(“x1=%8.4f,x2=%8.4f\n”,x1,x2); } else { p=-b/(2*a); q=sqrt(-disc/(2*a)); printf(“%8.4f+%x8.4fi\n”,p,q); printf(“%8.4f-%8.4fi\n”,p,q);} } } 5555....按托运规则,行李不超过50公斤时,运费为0.15元/公斤,如超过50公斤,超过部分的运费为0.22元/公斤,现有行李w公斤,编写一个程序计算运费。 解: #include stdio.h main() { float w,f,x; printf(“weight:”); scanf(“%f”,w); if(w=50) x=0.15*w; else x=0.15*50+0.22*(w-50); printf(“money:%6.2f yuan\n”,x); } 本程序运行结果为: weight:20CR money:3.00 yuan weight:60CR money:9.70 yuan 6. 某商场给与顾客购物的折扣率如下: 购物金额200元 不打折 500元购物金额=200元 9折 1000元购物金额=500元 8折 购物金额=1000元 7.5折 输入一个购物金额,输出打折率、购物实际付款金额。 #includestdio.h main() { float x,y,realx; scanf(“%f”,x); if(x=0) { printf(“Error! You input a worry number!\n”); y=0;} else { if(x200) y=1.0; else if(x500) y=0.9; else if(x1000) y=0.8; else y=0.75;} if(y!=0) {realx=x*y; printf(“y=%f, the realx=%5.2f\n”, y,realx);} } 第五章第五章第五章第五章 循环结构程序设计循环结构程序设计循环结构程序设计循环结构程序设计 一、选择题 C C A A D D第六章第六章第六章第六章 数组数组数组数组 、选择题 D A D A A C C A D 二、程序阅读题 13 13 13 13 13 13第七章第七章第七章第七章 函数函数函数函数 一、选择题 B D C B B D A A D第第第第8888章章章章 指针指针指针指针 一、选择题 D A C C(D) D C D 二、填空题 1. m 2. 指针数组名 3. ABCDCD 4.49 5. 25
C语言程序设计教程答案~有追加悬赏100分!
1 【C语言】《C语言程序设计教程(第二版)》习题答案
说 明
1. 本文所指的《C语言程序设计教程(第二版)》是李凤霞主编、北京理
工大学出版社出版的,绿皮。
2 第1章 程序设计基础知识
一、单项选择题(第23页)
1-4.CBBC 5-8.DACA
二、填空题(第24页)
1.判断条件 2.面向过程编程 3.结构化 4.程序 5.面向对象的程序设计语言 7.有穷性 8.直到型循环 9.算法 10.可读性 11.模块化 12.对问题的分析和模块的划分
三、应用题(第24页)
2.源程序:
main()
{int i,j,k; /* i:公鸡数,j:母鸡数,k:小鸡数的1/3 */
printf(“cock hen chick\n”);
for(i=1;i=20;i++)
for(j=1;j=33;j++)
for(k=1;k=33;k++)
if (i+j+k*3==100i*5+j*3+k==100)
printf(” %d %d %d\n”,i,j,k*3);}
执行结果:
cock hen chick
4 18 78
8 11 81
12 4 84
3.现计算斐波那契数列的前20项。
递推法 源程序:
main()
{long a,b;int i;
a=b=1;
for(i=1;i=10;i++) /*要计算前30项,把10改为15。*/
{printf(“%8ld%8ld”,a,b);
a=a+b;b=b+a;}}
递归法 源程序:
main()
{int i;
for(i=0;i=19;i++)
printf(“%8d”,fib(i));}
fib(int i)
{return(i=1?1:fib(i-1)+fib(i-2));}
执行结果:
1 1 2 3 5 8 13 21 34 55
89 144 233 377 610 987 1597 2584 4181 6765
4.源程序:
#include “math.h”;
main()
{double x,x0,deltax;
x=1.5;
do {x0=pow(x+1,1./3);
deltax=fabs(x0-x);
x=x0;
}while(deltax1e-12);
printf(“%.10f\n”,x);}
执行结果:
1.3247179572
5.源程序略。(分子、分母均构成斐波那契数列)
结果是32.66026079864
6.源程序:
main()
{int a,b,c,m;
printf(“Please input a,b and c:”);
scanf(“%d %d %d”,a,b,c);
if(ab){m=a;a=b;b=m;}
if(ac){m=a;a=c;c=m;}
if(bc){m=b;b=c;c=m;}
printf(“%d %d %d\n”,a,b,c);}
执行结果:
Please input a,b and c:123 456 789
789 456 123
7.源程序:
main()
{int a;
scanf(“%d”,a);
printf(a%21==0?”Yes”:”No”);}
执行结果:
42
Yes
3 第2章 C语言概述
一、单项选择题(第34页)
1-4.BDCB 5-8.AABC
二、填空题(第35页)
1.主 2.C编译系统 3.函数 函数 4.输入输出 5.头 6. .OBJ 7.库函数 8.文本
三、应用题(第36页)
5.sizeof是关键字,stru、_aoto、file、m_i_n、hello、ABC、SIN90、x1234、until、cos2x、s_3是标识符。
8.源程序:
main()
{int a,b,c;
scanf(“%d %d”,a,b);
c=a;a=b;b=c;
printf(“%d %d”,a,b);}
执行结果:
12 34
34 12
4 第3章 数据类型与运算规则
一、单项选择题(第75页)
1-5.DBACC 6-10.DBDBC 11-15.ADCCC 16-20.CBCCD 21-25.ADDBC 26-27.AB
二、填空题(第77页)
1.补码 2.±(10^-308~10^308) 3.int(整数) 4.单目 自右相左 5.函数调用 6.a或b 7.1 8.65,89
三、应用题(第78页)
1.10 9
2.执行结果:
11
12
1
5 第4章 顺序结构程序设计
一、单项选择题(第90页)
1-5.DCDAD 6-10.BACBB
二、填空题(第91页)
1.一 ;2. 5.169000 3.(1)-2002500 (2)I=-200,j=2500 (3)i=-200
j=2500 4.a=98,b=765.000000,c=4321.000000 5.略 6.0,0,3 7.3 8.scanf(“%lf%lf%lf”,a,b,c); 9. 13 13.000000,13.000000 10.a=a^c;c=c^a;a=a^c;(这种算法不破坏b的值,也不用定义中间变量。)
三、编程题(第92页)
1.仿照教材第27页例2-1。
2.源程序:
main()
{int h,m;
scanf(“%d:%d”,h,m);
printf(“%d\n”,h*60+m);}
执行结果:
9:23
563
3.源程序:
main()
{int a[]={-10,0,15,34},i;
for(i=0;i=3;i++)
printf(“%d\370C=%g\370F\t”,a[i],a[i]*1.8+32);}
执行结果:
-10℃=14°F 0℃=32°F 15℃=59°F 34℃=93.2°F
4.源程序:
main()
{double pi=3.14159265358979,r=5;
printf(“r=%lg A=%.10lf S=%.10lf\n”,r,2*pi*r,pi*pi*r);}
执行结果:
r=5 A=31.4159265359 S=49.3480220054
5.源程序:
#include “math.h”;
main()
{double a,b,c;
scanf(“%lf%lf%lf”,a,b,c);
if (a+bca+cbb+ca)
{double s=(a+b+c)/2;
printf(“SS=%.10lf\n”,sqrt(s*(s-a)*(s-b)*(s-c)));}
else printf(“Data error!”);}
执行结果:
4 5 6
SS=9.9215674165
6.源程序:
main()
{int a=3,b=4,c=5;float d=1.2,e=2.23,f=-43.56;
printf(“a=%3d,b=%-4d,c=**%d\nd=%g\ne=%6.2f\nf=%-10.4f**\n”,a,b,c,d,e,f);}
7.源程序:
main()
{int a,b,c,m;
scanf(“%d %d %d”,a,b,c);
m=a;a=b;b=c;c=m;
printf(“%d %d %d\n”,a,b,c);}
执行结果:
5 6 7
6 7 5
8.源程序:
main()
{int a,b,c;
scanf(“%d %d %d”,a,b,c);
printf(“average of %d,%d and %d is %.2f\n”,a,b,c,(a+b+c)/3.);
执行结果:
6 7 9
average of 6,7 and 9 is 7.33
9.不能。修改后的源程序如下:
main()
{int a,b,c,x,y;
scanf(“%d %d %d”,a,b,c);
x=a*b;y=x*c;
printf(“a=%d,b=%d,c=%d\n”,a,b,c);
printf(“x=%d,y=%d\n”,x,y);}
6 第5章 选择结构程序设计
一、单项选择题(第113页)
1-4.DCBB 5-8.DABD
二、填空题(第115页)
1.非0 0 2.k==0
3.if (abs(x)4) printf(“%d”,x);else printf(“error!”);
4.if((x=1x=10||x=200x=210)x1)printf(“%d”,x);
5.k=1 (原题最后一行漏了个d,如果认为原题正确,则输出k=%。)
6. 8! Right!11 7.$$$a=0 8.a=2,b=1
三、编程题(第116页)
1.有错。正确的程序如下:
main()
{int a,b,c;
scanf(“%d,%d,%d”,a,b,c);
printf(“min=%d\n”,ab?bc?c:b:ac?c:a);}
2.源程序:
main()
{unsigned long a;
scanf(“%ld”,a);
for(;a;printf(“%d”,a%10),a/=10);}
执行结果:
12345
54321
3.(1)源程序:
main()
{int x,y;
scanf(“%d”,x);
if (x-5×0)y=x;
if (x=0x5)y=x-1;
if (x=5×10)y=x+1;
printf(“%d\n”,y);}
(2)源程序:
main()
{int x,y;
scanf(“%d”,x);
if(x10) if(x-5) if(x=0) if(x=5)y=x+1;
else y=x-1; else y=x;
printf(“%d\n”,y);}
(3)源程序:
main()
{int x,y;
scanf(“%d”,x);
if(x10) if(x=5)y=x+1;
else if(x=0)y=x-1;
else if(x-5)y=x;
printf(“%d\n”,y);}
(4)源程序:
main()
{int x,y;
scanf(“%d”,x);
switch(x/5)
{case -1:if(x!=-5)y=x;break;
case 0:y=x-1;break;
case 1:y=x+1;}
printf(“%d\n”,y);}
4.本题为了避免考虑每月的天数及闰年等问题,故采用面向对象的程序设计。
现给出Delphi源程序和C++ Builder源程序。
Delphi源程序:
procedure TForm1.Button1Click(Sender: TObject);
begin
edit3.Text:=format(‘%.0f天’,[strtodate(edit2.text) -strtodate(edit1.text)]);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Edit2.Text:=datetostr(now);
button1click(form1)
end;
C++ Builder源程序:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Edit3-Text=IntToStr(StrToDate(Edit2-Text)-StrToDate(Edit1-Text))+”天”;
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Edit2-Text=DateToStr(Now());
Button1Click(Form1);
}
执行结果:(运行于Windows下)
5.源程序:
main()
{unsigned a,b,c;
printf(“请输入三个整数:”);
scanf(“%d %d %d”,a,b,c);
if(abca==ba==c)printf(“构成等边三角形\n”);
else if(a+bca+cbb+ca)
if(a==b||a==c||b==c)printf(“构成等腰三角形\n”);
else printf(“构成一般三角形\n”);
else printf(“不能构成三角形\n”);}
执行结果:
请输入三个整数:5 6 5
构成等腰三角形
6.源程序:
main()
{int x,y;
scanf(“%d”,x);
if(x20)y=1;
else switch(x/60)
{case 0:y=x/10;break;
default:y=6;}
printf(“x=%d,y=%d\n”,x,y);}
7.源程序:
main()
{unsigned m;float n;
scanf(“%d”,m);
if(m100)n=0;
else if(m600)n=0.06;
else n=(m/100+0.5)/100;
printf(“%d %.2f %.2f\n”,m,m*(1-n),m*n);}
执行结果:
450
450 429.75 20.25
8. 2171天(起始日期和终止日期均算在内)
本题可利用第4小题编好的程序进行计算。把起始日期和终止日期分别打入“生日”和“今日”栏内,单击“实足年龄”按钮,将所得到的天数再加上1天即可。
9.源程序:
#include “math.h”;
main()
{unsigned long i;
scanf(“%ld”,i);
printf(“%ld %d\n”,i%10,(int)log10(i)+1);}
执行结果:
99887
7 5
10.源程序:
main()
{unsigned long i;unsigned j[10],m=0;
scanf(“%ld”,i);
for(;i;){j[m++]=(i+2)%10;i/=10;}
for(;m;m–)i=i*10+j[m-1];
printf(“%ld\n”,i);}
执行结果:
6987
8109
(注:要加密的数值不能是0或以0开头。如果要以0开头需用字符串而不能是整数。)
7 第6章 循环结构程序设计
一、单项选择题(第142页)
1-4.BCCB 5-8.CBCA
二、填空题(第143页)
1.原题可能有误。如无误,是死循环 2.原题有误。如果把b=1后面的逗号改为分号,则结果是8。 3.20 4.11 5. 2.400000 6.*#*#*#$ 7.8 5 2 8.①d=1.0 ②++k ③k=n 9.①x=0 ②xamin
三、编程题(第145页)
1. 源程序:
main()
{int i=1,sum=i;
while(i101){sum+=i=-i-2;sum+=i=-i+2;}
printf(“%d\n”,sum);}
执行结果:
51
2.源程序:
main()
{double p=0,n=0,f;int i;
for(i=1;i=10;i++)
{scanf(“%lf”,f);
if (f0)p+=f; else n+=f;}
printf(“%lf %lf %lf\n”,p,n,p+n);}
3.源程序:
main()
{unsigned a;
scanf(“%ld”,a);
for (;a;printf(“%d,”,a%10),a/=10);
printf(“\b \n”);}
执行结果:
23456
6,5,4,3,2
4.源程序:
main()
{unsigned long a,b,c,i;
scanf(“%ld%ld”,a,b);
c=a%1000;
for(i=1;ib;i++)c=c*a%1000;
if(c100)printf(“0”);
if(c10)printf(“0”);
printf(“%ld\n”,c);}
执行结果:
129 57
009
5.略
6.原题提供的计算e的公式有误(前面漏了一项1)。正确的公式是e= 1 + 1 + 1/2! + 1/3! + … + 1/n! + …
(1)源程序:
main()
{double e=1,f=1;int n;
for(n=1;n=20;n++){f/=n;e+=f;}
printf(“e=%.14lf\n”,e);}
执行结果:
e=2.71828182845905
(2)源程序:
main()
{double e=1,f=1;int n;
for(n=1;f1e-4;n++){f/=n;e+=f;}
printf(“e=%.4f\n”,e);}
执行结果:
e=2.7183
7.源程序:
main()
{unsigned long a=0,b=1,c=0;int i,d;
scanf(“%d”,d);
for (i=1;i=(d+2)/3;i++)
printf(“%10ld%10ld%10ld”,a,b,(a+=b+c,b+=c+a,c+=a+b));}
本题还可以用递归算法(效率很低),源程序如下:
unsigned long fun(int i)
{return i=3?i:fun(i-1)+fun(i-2)+fun(i-3);}
main()
{int i,d; scanf(“%d”,d);
for(i=1;i=d;i++)
printf(“%10ld”,fun(i));}
执行结果:
15
1 2 3 6 11 20 37 68
125 230 423 778 1431 2632 4841
8.源程序:
main()
{int i;
for(i=1010;i=9876;i+=2)
if(i/100%11i%100%11i/10%100%11i/1000!=i%10i/1000!=i/10%10i/100%10!=i%10)printf(” %d”,i);}
执行结果:
1024 1026 1028 1032 1034 1036 …… …… 9874 9876
9.源程序:
main()
{int i,j,k;
printf(“apple watermelon pear\n”);
for(i=1;i=100;i++)
for(j=1;j=10;j++)
if((k=100-i-j)*2==400-i*4-j*40)
printf(“%4d%7d%9d\n”,i,j,k);}
执行结果:
apple watermelon pear
5 5 90
24 4 72
43 3 54
62 2 36
81 1 18
10.源程序:
#include “stdio.h”;
#define N 4 /* N为阶数,可以改为其他正整数 */
main()
{int m=N*2,i,j;
for(i=1;im;printf(“\n”),i++)
for(j=1;jm;
putchar(N-abs(i-N)=abs(j++-N)?’ ‘:’*’));}
如果把N值改为5,则执行结果如下:
*
***
*****
*******
*********
*******
*****
***
*
作者:宁西贯通 2006-5-7 23:41 回复此发言
——————————————————————————–
8 说明
注意:上面最后一题的输出结果应该是由星号组成的一个菱形,
9 第7章 数 组
一、单项选择题(第192页)
1-4.BBCC 5-8.AABA
二、填空题(第194页)
1.1
2
4
8
16
32
64
128
256
512
2. ①a[age]++ ②i=18;i26
3. ①break ②i==8
4. ①a[i]b[j] ②i3 ③j5
5. ①b[j]=a[j][0] ②b[j]a[j][k] 6.a[k++]=a[j]
三、编程题(第196页)
1.源程序:
main()
{int a[4][4],i,j,s=0;
for(i=0;i4;i++)
for(j=0;j4;j++)
scanf(“%d”,a[i][j]);
for(i=0;i4;i++)
for(j=0;j4;j++)
if(i==j||i+j==3)s+=a[i][j];
printf(“%d\n”,s);} /* 注:5×5矩阵不能照此计算! */
执行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
68
2. 源程序:
main()
{int i,a[36];
a[0]=2;
for(i=1;i=29;i++)a[i]=a[i-1]+2;
for(;i=35;i++)a[i]=a[(i-30)*5+2];
for(i=0;i=35;i++)printf(“%d\t”,a[i]);}
执行结果:
2 4 6 8 10 12 14 16 18 20
22 24 26 28 30 32 34 36 38 40
42 44 46 48 50 52 54 56 58 60
6 16 26 36 46 56
3. 源程序:
#include “stdlib.h”
#include “time.h”
main()
{int a[30],i,m=0;
randomize();
for(i=0;i=29;i++)
{a[i]=rand();
if(ma[i])m=a[i];
printf(“%d\t”,a[i]);}
for(i=0;i=29;i++)if(a[i]==m)a[i]=-1;
printf(“\n—————–\n”);
for(i=0;i=29;i++)
if(~a[i])printf(“%d\t”,a[i]);
printf(“\n”);}
执行结果:
20679 29377 18589 9034 27083 4959 3438 5241 32278 23344
32499 29305 22340 5927 13031 2161 2583 31855 22977 14283
4851 22038 6992 11394 20887 27381 6293 18347 16414 10210
—————–
20679 29377 18589 9034 27083 4959 3438 5241 32278 23344
29305 22340 5927 13031 2161 2583 31855 22977 14283 4851
22038 6992 11394 20887 27381 6293 18347 16414 10210
4.源程序:
main()
{int i,n=0,b[16];
scanf(“%d”,i);
for(;i;i=1)b[n++]=i1;
for(;n;)printf(“%d”,b[–n]);}
执行结果:
9876
10011010010100
本题也可以不用数组。源程序如下:
#include “stdio.h”
main()
{int i,n;
scanf(“%d”,i);
for(n=16;n;n–)
{asm ROL i,1
putchar(i1|48);}
} /* ROL是循环左移的汇编指令 */
5. 源程序:
#include “stdlib.h”
#include “time.h”
#define M 5
#define N 6
main()
{int a[M][N],i,j,t[M];
randomize();
/*生成M行N列随机数*/
for(i=0;iM;printf(“\n”),t[i++]=0)
for(j=0;jN;j++)
printf(“%4d”,a[i][j]=random(50));
/*找出每行的最小数,t[M]是第M行的最小数所在的列数*/
for(i=0;iM;i++)
for(j=0;jN;j++)
if(a[i][t[i]]a[i][j])t[i]=j;
/*比较每个最小数在其所在的列上是否也是最小*/
for(j=0;jM;j++)
for(i=0;iM;i++)
{if(i==j)continue;
if(a[j][t[j]]a[i][t[j]])
{t[j]=-1;break;}
}
printf(“——————-\n”);
/*输出在行和列上均为最小的数*/
for(i=0;iM;i++)
if(t[i]!=-1)
printf(“a[%d,%d]=%d\n”,i,t[i],a[i][t[i]]);
}
执行结果:
13 19 13 20 0 1
20 41 6 16 35 30
3 5 37 8 23 15
6 36 24 29 18 1
1 5 28 21 46 34
——————-
a[0,4]=0
a[1,2]=6
a[3,5]=1
a[4,0]=1
6. 源程序:
#include “stdlib.h”
#include “time.h”
#define M 5
#define N 7
main()
{int a[M][N],i,j,t=0;
randomize();
for(i=0;iM;i++)
{a[i][N-1]=0;
for(j=0;jN-1;j++)
{printf(“%4d”,a[i][j]=random(91)+10);
a[i][N-1]+=a[i][j];}
printf(“%4d\n”,a[i][N-1]);}
for(i=1;iM;i++)
if(a[i][N-1]a[t][N-1])t=i;
if(t)for(j=0;jN;j++)
{i=a[0][j];a[0][j]=a[t][j];a[t][j]=i;}
printf(“—————–\n”);
for(i=0;iM;printf(“\n”),i++)
10 第7章 数 组
for(j=0;jN;j++)
printf(“%4d”,a[i][j]);
}
执行结果:
89 17 32 95 35 20 288
39 48 22 27 73 22 231
51 87 39 71 84 46 378
84 94 97 77 27 26 405
69 50 56 89 37 46 347
—————–
84 94 97 77 27 26 405
39 48 22 27 73 22 231
51 87 39 71 84 46 378
89 17 32 95 35 20 288
69 50 56 89 37 46 347
7. 源程序:
#include “stdlib.h”
#include “time.h”
#define M 5
#define N 6
main()
{int a[M][N],i,j;
struct data{int value,x,y;}max,min;
max.value=0;min.value=100;
randomize();
for(i=0;iM;printf(“\n”),i++)
for(j=0;jN;j++)
{printf(“%4d”,a[i][j]=random(100)+1);
if(max.valuea[i][j])
{max.value=a[i][j];max.x=i;max.y=j;}
if(min.valuea[i][j])
{min.value=a[i][j];min.x=i;min.y=j;}
}
printf(“—————–\n”);
i=a[0][N-1];a[0][N-1]=max.value;a[max.x][max.y]=i;
i=a[M-1][0];a[M-1][0]=min.value;a[min.x][min.y]=i;
for(i=0;iM;printf(“\n”),i++)
for(j=0;jN;j++)
printf(“%4d”,a[i][j]);
}
执行结果:
51 53 74 65 30 40
30 26 50 6 61 27
47 16 54 58 76 19
57 74 44 92 71 48
73 57 60 32 73 67
—————–
51 53 74 65 30 92
30 26 50 73 61 27
47 16 54 58 76 19
57 74 44 40 71 48
6 57 60 32 73 67
9. 源程序:
main()
{char s[255];int i,j,b=1;
printf(“Input a string:”);
scanf(“%s”,s);
i=strlen(s);
for(j=1;j=i/2;j++)
b=b(s[j-1]==s[i-j]);
printf(b?”Yes\n”:”No\n”);}
执行结果:
Input a string:level
Yes
10. 源程序:
main()
{char s[255],t,max=0,min=0,l,i;
printf(“Input a string(length4):”);
gets(s);
l=strlen(s);
for(i=0;il;i++)
{if(s[max]s[i])max=i;if(s[min]s[i])min=i;}
t=s[1];s[1]=s[max];s[max]=t;if(min==1)min=max;
t=s[l-2];s[l-2]=s[min];s[min]=t;
printf(“%s\n”,s);}
执行结果:
Input a string(length4):C++Builder
Cu+Beild+r
11. 源程序:
main()
{char m[13][10]={“****”,”January”,”February”,”March”,
“April”,”May”,”June”,”July”,”August”,”September”,
“October”,”November”,”December”};
int i,j,k,a,s,n;
printf(“Please input an integer(100..999):”);
scanf(“%d”,n);
printf(“%d:%d+%d+%d=%d, %d%%13=%d, %s\n”, n,i,j,k,s,s,a,m[a=((s=(i=n/100)+(j=n/10%10)+(k=n%10))%13)]);}
执行结果:
Please input an integer(100..999):539
539:5+3+9=17, 17%13=4, April
11 第8章 函 数
一、单项选择题(第241页)
1-5.BCCAA 6-10.CCDDD 11-15.ACACB
二、填空题(第243页)
1.看不出原题的意图。因为要计算1~n的累加和,n应是一个≥1的正整数。可是题目中却出现了n=0的情况。除非另加规定当n=0时1~n的累加和为0,或者把原题中的计算式改为计算0~n的累加和。据此猜测,原题应填为:①return(0) ②return(n+sum(n-1))
根据题意,如下程序较为合理:
int sum(int n)
{if(n=0)return(-1); /* -1是出错标志 */
else if(n==1)return(1);
else return(n+sum(n-1));}
2. ①return(1) ②return(n*facto(n-1))
三、编程题(第244页)
3.源程序:
main()
{int i,a,b,c;
for(i=100;i999;i++)
if((a=i/100)*a*a+(b=i/10%10)*b*b+(c=i%10)*c*c==i)
printf(“%d\t”,i);}
执行结果:
153 370 371 407
8.源程序(非递归算法):
#define P 13 /* P可以改为其他正整数 */
main()
{int a[P],r,c;
for(r=0;r=P;r++)
{a[r]=1;
for(c=r-1;c=1;a[c–]+=a[c-1]);
printf(“%*d”,(P-r)*3+1,a[0]);
for(c=1;c=r;printf(“%6d”,a[c++]));
printf(“\n”);}
}
执行结果:
(应该排列成一个三角形,是贴吧造成现在这个样子的,不是程序有问题)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
9.源程序(递归算法):
#include “stdio.h”
void printOCT(unsigned long n)
{unsigned long i;
if(i=n3)printOCT(i);
putchar((n7)+48);}
main()
{unsigned long i;
scanf(“%ld”,i);
printOCT(i);}
执行结果:
1234567890
11145401322
本题也可以不用递归算法,源程序请参考第7章第三题4。
12 回复:【C语言】《C语言程序设计教程(第二版)》习题答案
但是不同时间印刷的版本课后题不太一样呢,象我们的是1999年12月第2版,2005年12月第69次印刷的。没有选择填空,应用题和楼主不知道有多少相同的,因为看不到原题。这个比较麻烦呢。
作者:210.77.204.* 2006-5-9 18:38 回复此发言
——————————————————————————–
13 回复:【C语言】《C语言程序设计教程(第二版)》习题答案
你对照一下主编和出版社,看看对吗?(见说明的第一条。)
我不是说叫你有问题另发帖子吗?
14 第9章 指 针
一、单项选择题(第276页)
1-5.DCDAC 6-10.CCABC 11-16.AABBB 16-20.DCDBD
二、填空题(第278页)
1.①int * ②*z
2.*p++
3.①’\0′ ②++
4.①q=p+1 ②qp+10 ③*qmax ④*qmin
三、编程题(第280页)
7.源程序:
main()
{int i=0;char c[20];
do{scanf(“%s”,c);i++;}
while(strcmp(c,”stop”));
printf(“%d\n”,i);}
执行结果:
This car ran form Nanyang
to Luoyang without a stop
10
9.源程序:
main()
{char s[255],c[255]={0};int i;
gets(s);
for(i=0;s[i];c[s[i++]]++);
for(i=0;i255;i++)
if(c[i])printf(“%c=%d\t”,i,c[i]);}
执行结果:
abcedabcdcd
a=2 b=2 c=3 d=3 e=1
c语言程序设计教程课后习题答案 黄复贤的
大学学习资料免费下载网 有
在 其他公共课程 版块
标题:
C语言程序设计第三版谭浩强课后习题答案完整版txt
谭浩强C语言程序设计习题参考解答
C程序设计题解与上机指导 答案
还有很多相关资料:
谭浩强编《C语言程序设计》视频教程(曾怡讲解) 在线 下载
谭浩强编《C语言程序设计》(电子书+纸质书)
……
下载不用积分
如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!
c语言程序设计实训教程答案的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于c语言程序设计实训教程答案详解、c语言程序设计实训教程答案的信息别忘了在本站进行查找喔。