习题参考答案
习 题 参 考 答 案
习 题 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
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 3、求一元二次方程ax2 +bx+c=0的解。 #include {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 {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 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