C课后习题参考答案

2020-03-27 04:49

习题参考答案

习 题 参 考 答 案

习 题 1

一、选择题

1、B 2、C 3、B 4、D 5、A 6、B 7、C 二、填空题

1、源程序文件 c 2、obj 3、可执行文件 exe 4、机器语言 汇编语言 高级语言 三、解答题 略 四、编程题 1、 main() {

printf(“Hello! Welcome to China!”);

} 2、 main() {int x;

scanf(“%d”,&x);

if(x>=20&&x<1000) printf(“x=%d”,x); else printf(“Input error!”); }

习 题 2

一、选择题

1、C 2、A 3、B 4、D 5、 A 6、D 7、B 8、B 9、B 10、D 11、D 12、B 13、A 14、B 15、C 二、填空题

1、整型、实型、字符型 2、用户标识符、关键字标识符 3、存储单元、符号地址、内存地址 4、十、十六、八 5、double(双精度型) 6、 8 7、5.500000 8、a=-32768

295

习题参考答案

9、+0017,021,0x11 三、写程序运行结果

3257 32 57

7.88, -345.12,7.8765,-345.1230 7.87654e+00, -3.5e+02 a,97,141,61 1234,2322,4d2 CHINESE, CHI 四、scanf函数的使用

a=3 b=7 8.5 71.82 A a

五、用scanf函数输入数据

10

20Aa1.5-3.75 123.45,67.8

注意,其中123.45可以是任意实数,因为该值将被跳过,不用于赋值。

习 题 3

一、选择题 1. C 2. B 3. D 4. D 二、填空题 基本概念题 1. 2 2. 2 3. 1

阅读程序写出运行结果题 4. 1.00 5. 1,0,1

6. 9,11,9,10

三、写出下面表达式运算后a的值,设原来a=12。 (1)24 (2)10 (3)60 (4)0

(5)0 (6)0

习 题 4

一、选择题

1、B 2、D 3、B 4、D 5、A 6、C 二、填空题

1、1,0,1 2、1,2,3

3、ch1>=′A′&&ch1<=′Z′ ch1=ch1-32; 三、编程题

296

习题参考答案

1、从键盘输入三个数,然后按照由小到大的顺序输出。要求,设三个数放在变量a、b、中,最后仍然按照a、b、c的顺序输出。

#include main() {int a,b,c,t;

scanf(“%d,%d,%d”,&a,&b,&c); if(a>b) {t=a; a=b; b=t;} if(a>c) {t=a; a=c; c=t;} if(b>c) {t=b; b=c; c=t;} printf(“%d,%d,%d\\n”,a,b,c); }

2、编写程序根据以下的函数关系,对输入的x值输出相应的y值。

x y 2

#include main() {float x,y; scanf(“%f”,&x); if(x<=-1) y=x-1; else if(x<=2) y=2*x; else if(x<=10) y=x*(x+2); else printf(“Error!\\n”); printf(“y=%f\\n”,y); }

3、求一元二次方程ax2

+bx+c=0的解。 #include main()

{float a,b,c,d,disc,x1,x2,realpart,imagpart; scanf(“%f,%f,%f”,&a,&b,&c); if(fabs(a)<=1e-6)

Printf(is not a quadratic); else

297

c习题参考答案

{disc=b*b-4*a*c; if(fabs(disc)<=1e-6)

printf(“has two equal roots:%8.4\\n”,-b/(2*a)); else if(disc>1e-6)

{x1=(-b+sqrt(disc))/(2*a); x2=(-b-sqrt(disc))/(2*a);

printf(“has distinct real roots:%8.4f and %8.4f\\n”,x1,x2); } else

{realpart=-b/(2*a);

imagpart=sqrt(-disc)/(2*a); printf(“has complex roots:\\n”);

printf(“%8.4f+%8.4fi\\n”, realpart,imagpart); printf(“%8.4f-%8.4fi\\n”, realpart,imagpart); } } }

4、假设工资税率如下,其中s代表工资,r代表税率:

s<500 r=0% 500<=s<1000 r=5% 1000<=s<2000 r=8% 2000<=s<3000 r=10% 3000<=s r=15%

编一程序实现从键盘输入一个工资数,输出实发工资数。要求使用switch语句。

main()

{int salarly,r,g; scanf(“%d”,&salarly); if(salary>=3000) r=0.15; g=salary/500; switch(g) {case 1: r=0.05; case 2: case 3: r=0.08; case 4: case 5: r=0.10; }

salary=salary*(1-r); printf(“%d\\n”,salary);

298

习题参考答案

习 题 5

一、选择题

1、A 2、C 3、C 4、D 5、A 6、A 7、A 8、B 9、C 10、B 11、B 12、D 二、填空题 1、continue

2、[1]n<=999或n<1000 [2]n

3、[1]x>=0或x>=0.0 [2]x

6、[1]n [2]flag=1 [3]n— 7、[1]j

#include main()

{int m,n,p,r,temp;

printf(“Please input m,n:”); do

{scanf(“%d%d”,&m,&n); }while(m<=0||n<=0); if(n

{ temp=n; n=m; m=temp; } /*确保大数放到n中*/

p=n*m; /*保留n和m的乘积到p中,以便求最小公倍数*/ while(m!=0) /*求n和m的最大公约数*/ {r=n%m; n=m; m=r; }

printf(“最大公约数为:%d\\n”,n); printf(“最小公倍数为:%d\\n”,p/n); } 2、

#include main() {char c;

int letter=0,space=0,digit=0,other=0;

printf(“Please input a line character:”); while((c=getchar())!=’\\n’)

{if(c>=’a’&&c<=’z’||c>=’A’&&c<=’Z’) letter++; else if(c==‘ ’) space++;

else if(c>=’0’&&c<=’9’) digit++; else other++; }

printf(“Letter is %d,Space is %d,Digit is %d,Other

299


C课后习题参考答案.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: