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(max<B)
max=b
if(max<C)
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=3b=4c=5
x=1.2y=2.4z=-3.6
u=51274n=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(a<B)
if(b<C)
printf("max=%d\n",c)
else
printf("max=%d\n",b)
else if(a<C)
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=(a>b)?a:b
max=(temp>c)? temp:c
printf(" A,B,C中最大数是%d,",max)
}
5.5 main()
{int x,y
printf("输入x:")
scanf("%d",&x)
if(x<1)
{y=x
printf("X-%d,Y=X=%d \n",x,y)
}
else if(x<10)
{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(s>100||s<0)
{
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)
}
http://818pp.com/
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(num>9999)
place=5
else if(num>999)
place=4
else if(num>99)
place=3
else if(num>9)
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(branch>10)branch=10
switch(branch)
{case 0:bonus=i*0.1break
case 1:bonus=bon1+(i-100000)*0.075break
case 2:
case 3:bonus=bon2+(i-200000)*0.05break
case 4:
case 5:bonus=bon4+(i-400000)*0.03break
case 6:
case 7
case 8:
case 9:bonus=bon6+(i-600000)*0.015break
case 10:bonus=bon10+(i-1000000)*0.01
}
printf("bonus=%10.2f",bonus)
} http://818pp.com/
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(a>b)
{t=aa=bb=t}
if(a>c)
{t=aa=cc=t}
if(a>d)
{t=aa=dd=t}
if(b>c)
{t=bb=cc=t}
if(b>d)
{t=bb=dd=t}
if(c>d)
{t=cc=dd=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(d1>1 &&d2>1 &&d3>1 &&d4>1)
h=0
printf("该点高度为%d",h)
}
c语言程序设计第三版指导答案
附录F 课后题参考答案
习 题 1
1.1 填空题
1.函数
2.主函数main();主函数main() 3.主函数main() 4.函数首部;函数体 5.{;}
6.顺序结构;选择结构;循环结构 7..c;.obj;.exe
1.2 思考题
1.答:结构化程序设计是指,为使程序具有一个合理的结构以保证程序正确性而规定的一套如何进行程序设计的原则。顺序结构,选择结构,循环结构
2.答:算法是对具体问题求解步骤的一种描述。计算机算法的表达工具通常采用以下几种方法:①用自然语言表示算法;②用流程图表示算法;③用伪代码表示算法;④用程序设计语言表示算法。
3.略 4.略 5.略 1.3 编程题 1.答:
#include "stdio.h" main()
{ float a=10, b=20, h=5, s s=(a+b)*h/2
printf("s=%f " , s ) }
2.答:
#include "stdio.h"
main()
{ printf("******************************") printf("* hello world *") printf("******************************") }
习 题 2
2.1 单选题
DBDCA DCABB CA
2.2 填空题
1.2.000000
2.1;0.500000
3.9;2 4.6 5.100;d 6.(1)20
(2)0
(3)60 7.(1)10;6;4
(2)6;9;15
(3)3;60;83
8.55或 '7'
9.x=4;y=6;z=3;m=463
2.3 改错题(略)
习 题 3
3.1 单选题
BDABC ADCAC BBA
3.2 填空题
1.3 2.261 3.10
4.2, 1;互换a,b的值 5.6.6 6.003 7.7
8.5.0,4,c=3<Enter>
9.i=10,j=20<Enter>
10. (1)65 (2)65,A (3)3.14,123.46
(4)3.141600e+000,1.234560e+002 (5)8765.432100 (6)8.765432e+003
11.a=2b=5x=8.8y=76.34c1=65c2=97 12.%d/%d;%d/%d=%.2f\n
3.3 改错题(略) 3.4 编程题
1.答:
#include "stdio.h" main() {
int x,y
scanf("%d%d",&x,&y) printf("\t\tx\ty\n")
printf("十进制数\t%d\t%d\n",x,y) printf("八进制数\t%o\t%o\n",x,y) printf("十六进制数\t%X\t%x\n",x,y) }
2.答:
#include "stdio.h" main() {
char ch
printf("请输入一个大写英文字母") scanf("%c",&ch)
printf("大写英文字母是%c\n",ch) printf("它的前导字符是%c\n",ch-1) printf("它的后续字符是%c\n",ch+1) }
3.答:
#include "stdio.h" main() {
int x,a,b,c,y
printf("请输入一个三位整数\n") scanf("%d",&x) a=x/100
b=(x-a*100)/10 c=x%10
y=c*100+b*10+a
printf("反向输出该整数:%d\n",y) } }
4.答:
#include "stdio.h" main()
{ int hour
double salary, salaryday
scanf("%d,%lf", &hour, &salaryday )
salary=hour*salaryday- hour*salaryday*0.1
printf("%8.2lf\n", salary) }
5.答:
#include "stdio.h" main() {
int a,b,c,t
printf("请输入三个整数\n") scanf("%d%d%d",&a,&b,&c)
printf("交换前a=%d,b=%d,c=%d\n",a,b,c) t=aa=cc=bb=t
printf("交换后a=%d,b=%d,c=%d\n",a,b,c) }
习 题 4
4.1 单选题
BADDD ACBBB BA
4.2 填空题
1.1
2.(1)a>0 || b>0
(2)x>0 && x<=10 (3)a==1.5 && b==1.5 && c==1.5
(4)p<a || p<b || p<c
3.(1)0 (2)1 (3)1 (4)0 (5)1
4.c=1 5.-4 6.1 7.5, 0, 3 8.5 9.123
10.( cvb= ='y'||cvb= ='Y')&&(work>=3||college=='y'|| college=='Y')&&age<=35
4.3 改错题(略) 4.4 编程题
1.答
#include "stdio.h"
#include "math.h" main() {
double a,b,c,p,area
scanf("%lf%lf%lf",&a,&b,&c)
printf("三角形的三边为:%.llf,%.1lf,%.1lf\n",a,b,c) if (a+b>c&&a+c>b&&b+c>a) {p=(a+b+c)/2
area=sqrt(p*(p-a)*(p-b)*(p-c))
printf("三角形的面积为%.2lf\n",area) } else
printf("不能构成三角形\n") }
2.答:
#include "stdio.h" main()
{ int x,y
scanf("%d,%d",&x,&y) if(x*x+y*y>1000)
printf("%d\n",(x*x+y*y)/100) else
printf("%d\n",x+y) }
3.答:
#include "stdio.h" #include "math.h" main()
{ double x,y
scanf("%lf",&x) if(x<-2) y=x*x-sin(x) else if (x<=2) y=pow(2,x)+x else y=sqrt(x*x+x+1)
printf("x=%.2lf y=%.2lf\n",x,y) }
4.答:
#include "stdio.h" main( )
{ long ge,shi,qian,wan,x scanf("%ld",&x) wan=x/10000
qian=x%10000/1000 shi=x%100/10
ge=x%10
if (ge==wan&&shi==qian) /*个位等于万位并且十位等于千位*/ printf("this number is a huiwen\n") else
printf("this number is not a huiwen\n")
}
5.答:
#include "stdio.h" main()
{ float p,w,s,d,f
scanf("%f,%,%f",p,s,w) if (s>3000) d=0.15 else if( s>=2000) d=0.1 else if(s>=1000) d=0.08 else if(s>=500) d=0.05 else if(s>=250) d=0.02 else d=0 f=p*w*s*(1-d) printf("%f",f) }
6.答:
#include "stdio.h" main()
{ int year,money char x
printf("是否是本公司产品(y/n):") scanf("%c",&x)
if(x=='y'||x=='Y')
{printf("产品使用的年限:") scanf("%d",&year)
if(year<=1) money=0
else if(year<8) money=50 else money=100
printf("产品保修额是:%d\n",money)
}
else
{ money=200
printf("不是本公司产品,产品保修额是:%d\n",money) } }
7.答:
#include "stdio.h" main()
{ int money,num1,num2
printf("请输入取款额(≤2000):") scanf("%d",&money)
if(money>2000) printf("请输入取款额数≤2000!\n") else if(money%50==0) { num1=money/100 num2=(money-num1*100)/50 printf("需支付100元:%d张\n",num1) printf("需支付50元:%d张\n",num2) } else printf("输入钱数必须是50的倍数!\n") }
习 题 5
5.1 单选题
CDABA ABDDB DBCB
5.2 填空题
1.2 0 2.333
3.(1)i<10 或 i<=9 (2)j%3!=0
4.(1)flag*(float)k/(k+1) 或1.0*flag*k/(k+1) (2)flag=-flag 5.(1)max=x
(2)x!=-1 (3)scanf("%d", &x)
6.(1)x<=9或x<10
(2)y=9-x
5.3 改错题(略) 5.4 编程题
1.答:
(1)for循环,其他略
#include "stdio.h"
main()
{ int i,s=0
for(i=1i<=100i++) s+=i*i
printf("%d\n",s) }
(2)for循环,其他略
#include "stdio.h" main()
{ int i=1,p=1 double s=1 do {
s+=1.0/p p*=++i
}while(1.0/p>1e-6) printf("%lf",s) }
2.答:
#include "stdio.h" main()
{ int m,n,t,a,b
scanf("%d,%d" ,&m,&n) if (m<n)
{ t=m m=n n=t } a=m b=n t=m%n while(t)
{ m=n n=t t=m%n} printf("%d",n) }
3.答:
#include "stdio.h" main()
{ int x,y,s=1
scanf("%d,%d",&x,&y) for( y>0 y--)s*=x
printf("%d,%d,%d\n ",s%10,s/10%10,s/100%10) }
4.答:
#include "stdio.h" main()
{ int x,y,z
for( x=1 x<20 x++) for( y=1 y<33 y++) { z=100-x-y
if ((z%3)==0 &&(5*x+3*y+z/3)==100) printf("x=%d,y=%d,z=%d\n",x,y,z) } }
5.答: (a)
#include "stdio.h" main()
{ int j,k
for( j=1 j<=4 j++)
{ for(k=1k<=4-jk++)printf(" ") printf("****") printf("\n") } }
(b)
#include "stdio.h" main()
{ int j,k
for( j=1 j<=4 j++)
{for(k=1k<=4-jk++)printf(" ") for(k=1 k<=2*j-1 k++) printf("*") printf("\n") } }
6.答:
程序分析:利用for循环控制在100~999之间,对每个数分解出个位、十位、百位。
#include <stdio.h> main() { int i,j,k,n printf("water flower'number is:") for(n=100n<1000n++) { i=n/100/*分解出百位*/ j=n/10%10/*分解出十位*/ k=n%10/*分解出个位*/ if(n==i*i*i+j*j*j+k*k*k) { printf("%-5d",n) } } printf("\n") }
7.答:
#include <stdio.h> main() { int x for(x=1000x>=3x--) if(x%3==1&&x%5==2&&x%7==3) {
printf("该校的学生人数是:%d人\n",x) break } }
8.答:
#include <stdio.h> main() { int x=12,i=1 while(1)
{ if((x+20+i)==2*(x+i)) break i++ } printf("小明母亲在%d年后比小明的年龄大一倍\n",i) printf("那时小明年龄是:%d岁,小明母亲年龄是:%d岁\n",x+i,x+20+i) }
习 题 6
6.1 单选题
DBCCB BDC
C语言程序设计教程(第3版)
278
6.2 填空题
1.c 2.60 3.1000 10 4.16
6.3 编程题
1.答:
#include "stdio.h" #include "math.h"
#define F(a) a*a+ sqrt(3*a*a+2*a+1) main()
{ float x, f
scanf("%f", &x )
f=4.5/F(exp(x))+F(cos(x))+F(sqrt(x))/F(x*x) printf("%f\n", f) }
习 题 7
7.1 单选题
BCADA CCCDA BCBDB
7.2 填空题
1.(1)2 3 4 5 (2)10010 (3)QuickC
(4)10000 01000 00100 00010 00001 (5)Language
(6)Language Programming 2.(1)j+=2 (2)a[i]>a[j] 3.(1)i=1 (2)x[i-1]
7.3 改错题(略) 7.4 编程题
1.答:
#define N 10
#include "stdio.h" main()
{ int a[N]={1,2,3,4,5,6,7,8,9,10,osum=0, qsum=0,j for(j=0j<10j++)
if( j%2) qsum+=a[j]
else osum+=a[j]
printf("osum=%d,qsum=%d\n", osum,qsum) }
2.答:
#define N 10
#include "stdio.h" main()
{ int a[N]={10,20,30,40,50,60,70,80,90}, j, k, x scanf("%d",&x) for(j=0j<Nj++)
if (x<a[j]) break if(j==N) a[N-1]=x else
{for(k=N-1 k>j k--) a[k]=a[k-1] a[j]=x}
for(j=0j<Nj++)
printf("%d ",a[j]) }
3.答:
#define M 3
#include "stdio.h" main()
{ int a[M][M]={{1,2,3},{2,4,5},{3,5,6}},j,k,flag=1 for( j=0j<Mj++)
for(k=0k<Mk++) if (a[j][k]!=a[k][j]) { flag=0 break} if (flag) printf("ok") else printf("NO") }
4.答:
#include "stdio.h" #include "string.h" main()
{ char c1[10],c2[10],j gets(c1) gets(c2)
for(j=0 (c1[j]==c2[j]) && c1[j] && c2[j] j++) if (c1[j]>c2[j]) printf("%d\n",1) if (c1[j]<c2[j]) printf("%d\n",-1) if (c1[j]==c2[j]) printf("%d\n",0) }
5.答:
#include "stdio.h" #include "string.h" #define M 3 #define N 80 main()
{ char a[M][N],j,k,n[5]={0} for(j=0j<Mj++) gets(a[j])
for(j=0j<Mj++)
for(k=0a[j][k]k++)
if( a[j][k]>='A' && a[j][k]<='Z') n[0]++
else if (a[j][k]>='a' && a[j][k]<='z') n[1]++ else if (a[j][k]>='0' && a[j][k]<='9') n[2]++ else if (a[j][k]==' ' ) n[3]++ else n[4]++
for(j=0j<5j++) printf("%4d", n[j]) }
习 题 8
8.1 单选题
DBDAC BACCC
8.2 填空题
1.(1)2, 1 (2)10#30# (3)FOUR, P (4)60
2.(1)49
(2)2
(3)2
(4)
(5)
8.3 改错题(略) 8.4 编程题
1.答:
#include "stdio.h"
main()
{ int n1,n2,n3,t int *p1,*p2,*p3
printf("please input 3 number:n1,n2,n3:") scanf("%d,%d,%d",&n1,&n2,&n3) p1=&n1
p2=&n2 p3=&n3
if(*p1>* p2) { t=*p1*p1=*p2*p2=t}
if(*p1>*p3) { t=*p1*p1=*p3*p3=t} if(*p2>*p3) { t=*p2*p2=*p3*p3=t}
printf("the sorted numbers are:%d,%d,%d\n",n1,n2,n3) }
2.答:
#include "stdio.h" #define N 3 main()
{ int a[N],*p=a for(p-a<N p++) scanf("%d",p) p=a+N-1
for(p-a>=0 p--) printf("%d ",*p) }
3.答:
#include "stdio.h" main()
{ int a[10]
int j,minl=0,maxl=0 for(j=0j<10j++)
scanf("%d", a+j) for(j=0j<10j++)
{ if(a[maxl]<*(a+j)) maxl=j if(a[minl]>*(a+j)) minl=j }
j=a[0] a[0]=a[minl]a[minl]=j j=a[9]a[9]=a[maxl]a[maxl]=j for(j=0j<10j++) printf("%d ", *(a+j)) }
4.答:
输入阵列如下: 1 2 3 4 5 6 7 8 9 10 11 12 输出阵列如下:
12 11 10 9 8 7 6 5 4 3 2 1
#define M 3
#define N 4
#include "stdio.h" main()
{ int a[M][N]={1,2,3,4,5,6,7,8,9,10,11,12},k,j,*p=&a[0][0],t for(k=0,j=M*N-1k<jk++,j--)
{ t=*(p+k) *(p+k)=*(p+j) *(p+j)=t} for (k=0 k<M k++) { for(j=0 j<N j++)
printf("%4d ",a[k][j]) printf("\n")
} }
5.答:
#include "stdio.h" main() {
int len
char str[20],*p=str
printf("please input a string:\n") scanf("%s",str) len=0
while(*p!='\0') {
len++ p++ }
printf("the string has %d characters.\n",len) }
6.答:
#include "string.h" #include "stdio.h" main() {
char *str1[5],ch[5][20],k,t,j,*c void sort(char **) for(k=0k<5k++) {str1[k]=ch[k] gets(str1[k])} for(k=0k<7k++)
{ t=k
for(j=k+1j<5j++) if(strcmp(*(str1+t),*(str1+j))>0) t=j c=*(str1+t)
*(str1+t)=*(str1+k) *(str1+k)=c }
for(k=0k<5k++) puts(str1[k]) }
习 题 9
9.1 单选题
CBBAD DBCCD DCABC BCCBA DCDAB
9.2 填空题
1.120 2.x 3.3,2,2,3 4.fac /i 5.8,17 6.9 7.1.0/(i*i) 8.
fun-in:30,20,10 fun-end:1015,35,1050 10,20,30 9.012345 10.93636 11.(1)r+b[k] (2)*x
12.7 5 3 1 9 13.15
14.(1)*x (2)t 15.(1)'\0' (2)n++ 16.024
9.3 改错题(略) 9.4 编程题
1.答:
void zhuan( )
{ char ch
while((ch=getchar())!='\n')
{ if(ch>='a' && ch<='z') ch=ch-32 putchar(ch) } }
2.答:
double expp(int n) { int k, fac=1 double sum=1
for(k=1 k<=n k++) { fac*=k
sum+=1.0/fac }
return(sum) }
3.答:
int xy3( int x, int y)
{ int k, num=1
for(k=1k<=y k++) num*=x
num=num%1000 return num }
4.答:
int age( int n) { int c
if(n==1) c=10
else c=age(n-1)+2 return c }
5.答:
#include "stdio.h"
main()
{ int a,b,c,d
void fun(int a,int b,int *c, int *d) scanf("%d%d",&a,&b) fun(a,b,&c,&d)
printf("%d %d",c,d)
}
void fun(int a,int b,int *c, int *d) { if (b)
{ *c=a/b *d=a%b} }
6.答:
#include "stdio.h"
main(int argc,char *argv[]) { int k
printf("argc=%d\n",argc) for (k=1k<argc k++) printf("%s\n",argv[k]) }
习 题 10
10.1 单选题
CDBBB BBBAD CCBDC
10.2 填空题
1.所有结构体成员所占存储空间的总和 2.与占用存储空间最大的那个成员相等
附录F 课后题参考答案
285
3.(1)struct satype (2)3 (3)sa.a (4)9 (5)psa=&sa 4.80 5.struct node 6.0
10.3 编程题
1.答:
#include "stdio.h"
struct student {
long num;
char name[20]; char sex; float score; }; main()
{ struct student s[20], temp; int j,k, man=0, woman=0
float summan=0,sumwoman=0, aveman, avewoman; for(k=0 k<20 k++)
{ scanf("%ld %s %c%f",&s[k].num,s[k].name,&s[k].sex,&s[k].score); if(s[k].sex=='m')
{ summan+=s[k].score man++} else
{ sumwoman+=s[k].score;woman++ } }
aveman=summan/man;
avewoman=sumwoman/woman
printf("%d\t%f\t%d\t%f\n",man,aveman,woman,avewoman); for(k=0 k<19 k++)
for(j=0;j<20-k;j++)
if(s[j].score<s[j+1].score)
{ temp=s[j];s[j]=s[j+1];s[j+1]=temp;} printf("the sorted numbers:\n") for(k=0;k<20;k++)
printf("%ld\t%s\t%c\t%5.1f\n",s[k].num,s[k].name,s[k].sex,s[k].score); }
习 题 11
11.1 单选题
BADD
11.2 填空题
1.3d3d330 2.(1)28
(2)20 (3)0 (4)--9
3.(1)251
(2)42
(3)209
(4)–295 (5)848
习 题 12
12.1 单选题
BCDCA ADA
12.2 填空题
1.rewind(文件指针) 2."d1.dat","rb" 3.stdin
4.文本文件;二进制文件 5.(1)"w"
(2)str[i]--32
(3)"r"
6.fopen 7.Hello 8.(1)"r"
(2)fgetc(fp)
(3)time++
C语言程序设计实验与习题指导课后程序设计答案
P18
(1)
#include<stdio.h> int main(void) { intcelsius,fahr fahr=150 celsius=5*fahr/9-5*32/9 printf("fahr=%d,celsius=%d\n",fahr,celsius) return 0 }
(2)
#include<stdio.h> int main(void) { intcelsius,fahr celsius=26 fahr=9*celsius/5+32 printf("celsius=%d,fahr=%d\n",celsius,fahr) return 0 }
(3)
#include<stdio.h> int main(void) { intaverage,math,eng,comp math=87 eng=72 comp=93 average=(math+eng+comp)/3 printf("math=%d,eng=%d,comp=%d,average=%d\n",math,eng,comp,average) return 0 }
(4)
#include<stdio.h> int main(void) { intn,a,b,c n=152
c=n%10
b=(n/10)%10 a=n/100
printf("整数%d的个位数字是%d,十位数字是%d,百位数字是%d\n",n,c,b,a) return 0
}
P27
(1)
#include<stdio.h> #include<math.h> int main(void) { intcelsius,fahr printf("Enter celsius:") scanf("%d",&celsius) fahr=9*celsius/5+32 printf("fahr%d\n",fahr) return 0 }
(2)
#include<stdio.h> #include<math.h> int main(void) { intmoney,year doublerate,interest printf("Enter money,year,rate:") scanf("%d%d%lf",&money,&year,&rate) interest=money*pow(1+rate,year)-money printf("interest=%.2f\n",interest) return 0 }
(3)
#include<stdio.h> #include<math.h> int main(void) { doublex,y printf("Enter x:") scanf("%lf",&x)
if(x<0){ y=pow(x+1,2)+2*x+1/x } else{ y=sqrt(x) }
printf("y=f(%f)=%.2f\n",x,y) return 0
}
(4)
#include<stdio.h> int main(void) { intx,y printf("Enter num1:") scanf("%d",&x) printf("Enter num2:") scanf("%d",&y) printf("%d+%d=%d\n",x,y,x+y) printf("%d-%d=%d\n",x,y,x-y) printf("%d*%d=%d\n",x,y,x*y) printf("%d/%d=%d\n",x,y,x/y) printf("%d%%%d=%d\n",x,y,x%y) return 0 }
10的阶乘
#include<stdio.h> int main(void) { inti,n,product printf("Enter n:") scanf("%d",&n) product=1 for(i=1i<=ni++){ product=product*i } printf("product=%d\n",product) return 0 }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)