1、全国计算机等级考试二级教程-C语言程序设计课后习题答案第一章 1.1 EXE 1.2 C OBJ EXE 1.3 顺序 选择 循环 第二章 一. 选择题 2.1 B 2.2 D 2.3 B 2.4 A 2.5 C 2.6 A 2.7 B 2.8 B 2.9 D 2.10 C 2.11 B 2.12 B 2.13 A 二. 填空题 2.14 11 12 2.15 4.2 4.2 2.16 定义 执行语句 2.17 关键字 用户标记符 2.18 int float double 2.19 float a1=1; float a2=1; 2.20 存储单元 2.21 3.5 2.22 (a*b)/c
2、 a*b/c a/c*b 2.23 把常量10赋给变量s 2.24 位 1或0 2.25 8 127 0111111 -128 10000000 2.26 32767 -32768 0000 2.27 10 8 16 三. 上机改错题 2.28 #include stdio.h; 删除行尾的; main(); / * main function * / 删除)后的;,注释中的*要紧靠“/”,即应为“/*”和“*/” 函数开始处遗失了一个“” float r,s ; /*/*r is radius*/,/* s is area of circuilar*/*/ 注释符号不可嵌套使用 r = 5.
3、0 ; s = 3.14159 * r * r ; printf(%fn,s) 行尾遗失了“;” 函数结束处遗失了一个“” 2.29 #include stdio.h main /* main function */ main后遗失了“()” float a,b,c,v; /*a,b,c are sides, v is volume of cube */ a=2.0; b=3.0; c=4.0 行尾遗失了“;” v=a*b*c; printf(%fn, v) 行尾遗失了“;” 第三章 一. 选择题 3.1 C 3.2 C 3.3 D 3.4 C 3.5 D 3.6 B 3.7 C 3.8 D
4、3.9 A 3.10 B 3.11 C 3.12 D 3.13 D 3.14 A 3.15 C 3.16 C 3.17 C 3.18 无答案 3.19 C 3.20 B 二. 填空题 3.21 (1)-2023500(2)i=-200,j=2500 (3)i=-200 j=2500 3.22 12 0 0 3.23 一条语句 ; 3.24 ; 3.25 100,25.81,1.89234 100 25.81 1.89234 100 25.81 1.89234 3.26 x=127,x= 127,x= 177,x= 7f,x= 127 3.27 x=127,x=127 ,x=$127 ,x=$0
5、00127,x=%06d 3.28 a=513.789215,a= 513.79,a= 513.78921500,a= 513.78921500 三. 编程题和改错题 3.29 修改后的程序如下: main() double a,b,c,s,v; printf(input a,b,c:); scanf(%lf%lf%lf,&a,&b,&c); s =a*b; v=a*b*c; printf(a=%f,b=%f,c=%fn, a,b,c); printf(s=%f,v=%fn,s,v); 3.30 #include main() int a=560,b=60; printf(560 minute
6、 is %d hour and %d minute.n,a/b,a%b); 3.31 #include main() int a,b; a=1500;b=350; printf(a div b is : %dn,a/b); printf(a mod b is : %dn,a%b); 3.32 #include main() double a,b,c,ave; printf (input 3 double number : n); scanf (%lf%lf%lf,&a,&b,&c); printf (%.1fn,(a+b+c)/3); 3.33 #include void main() int
7、 a,b,c,t; printf(请依次输入整数a,b,c:); scanf(%d%d%d,&a,&b,&c); printf(n你输入的值是: a=%d,b=%d,c=%dn,a,b,c); t=b;b=a;a=c;c=t; printf(互换之后的值是:a=%d,b=%d,c=%dn,a,b,c); 第四章 一. 选择题 4.1 A 4.2 A 4.3 A 4.4 D 4.5 C 4.6 A 4.7 B 4.8 C 4.9 D 4.10 C 二. 填空题 4.11 非0 0 4.12 = =同级 = !=同级 4.13 ! & | 4.15 ! 4.16 a = b | a 4 | x -
8、4 4.17 1 4.18 x 0 4.19 3 2 2 4.20 *# 三. 编程题 4.21 略 4.22 #include /* 检查日期的合法性 */ int checkdate(int year, int month, int day) if(year 2023) printf(输入的年份无效!n); return 0; else if(month 12) printf(输入的月份无效!n); return 0; else if(day 31) printf(输入的日期无效!n); return 0; else switch(month) case 4: case 6: case 9
9、: case 11: if(day 30) printf(输入的日期无效!n); return 0; break; case 2: if(year%4 = 0 & year%100 != 0) | year%400 = 0) if(day 29) printf(输入的日期无效!n); return 0; else if(day 28) printf(输入的出生日期无效!n); return 0; break; /* end of switch(m0)*/ return 1; void main() int y0, m0, d0; /* 生日 */ int y1, m1, d1; /* 当前日期
10、 */ int years, months, days; /* 实足年龄*/ printf(请输入学生的生日:); scanf(%d%d%d, &y0,&m0,&d0); if(checkdate(y0, m0, d0) printf(请输入当前日期:); scanf(%d%d%d, &y1,&m1,&d1); /*当前日期合法性检查*/ if(!checkdate(y1, m1, d1) return; else if(y0 y1) printf(出生年份比当前年份晚!n); return; else if(y0 = y1) if(m0 m1) printf(出生年月比当前年月晚!n); r
11、eturn; else if(m0 = m1) if(d0 d1) printf(出生年月日比当前年月日晚!n); return; /* 计算实足年龄 */ years = y1 - y0; months = m1 - m0; days = d1 - d0; /* 修正实足年龄天数*/ if(days 0) months-; switch(m1) case 1: case 5: case 7: case 10: case 12: days += 30; break; case 2: case 4: case 6: case 8: case 9: case 11: days += 31; bre
12、ak; case 3: if(y1%4 = 0 & y1%100 != 0) | y1%400 = 0) days += 29; else days += 28; break; /* end of switch(m1) */ /* end of if(days 0) */ /* 修正实足年龄月数*/ if(months 0) months += 12; years-; /* end of if(months b)? a:b; max=(tempc)? temp:c; printf (n); printf (你输入的数中最大的是 %d.n,max); 4.25 (1)不嵌套的if语句 #incl
13、ude void main() int x,y; printf(input x :); scanf(%d,&x); if ( x-5 & x0 & x=10 | x=-5) printf(errorn); (2)嵌套的if语句 #include void main() int x,y; printf(input x :); scanf(%d,&x); printf(n); if(x -5) printf(y is %d.n,y=x); else printf(error!n); if(0 = x) printf(y is %d.n,y=x-1); if(x 0) if(x -5 & x0 &
14、x10 ) printf(y is %d.n,y=x+1); else printf(error!n); (4)switch语句 #include void main() int x,y; printf(input x : ); scanf(%d,&x); switch (x) case -4: case -3: case -2: case -1: printf(y is %d.n,y=x); break; case 0: printf(y is %d.n,y=x-1); break; case 1: case 2: case 3: case 4: case 5: case 6: case 7
15、: case 8: case 9: printf(y is %d.n,y=x+1); break; default: printf(error!n); 第五章 一. 选择题 5.1 D 5.2 C 5.3 B 5.4 C 5.5 C 5.6 B 5.7 D 5.8 A 5.9 D 5.10 D 二. 填空题 5.11 5 4 6 5.12 死循环 5.13 -1 5.14 11 5.15 d=1.0 k+ k=0 x 三. 编程题 5.17 #include void main() int i; int sig = 1; int sum = 0; for(i=1; i=101; i+,i+)
16、sum += sig*i; sig *= -1; printf(sum=%dn, sum); 5.18 (1) #include void main() int i; double m=1.0; double e = 1.0; for(i=1; i= 0.0004) m *= i; e += 1/m; i+; printf(e=%fn,e); 5.19 #include void main() int year; int col = 0; for(year=1600; year=2023; year+) if(year%4 = 0 & year%100 != 0) | year%400 = 0
17、) printf(%dt, year); col+; if(col%5 = 0) printf(n); printf(n); 5.20 #include #define N 7 void main() int i; int j; int m; int k = N/2; for(i=0; i m = i-k; if(m 0) m *= -1; for(j=0; j printf( ); for(j=0; j= 0 & striLoop = 9) num = 10*num + (striLoop - 0); iLoop+; printf(%dn,num); 6.22 #include #inclu
18、de #define N 80 void main() char strN; int num = -1; do gets(str); num+; while(strcmp(str, EOF); printf(您输入了%d行字符!n,num); 6.23 #include #define N 80 void main() char strN; int iLoop = 0; int num = 0; gets(str); while(striLoop & iLoop = a & striLoop = z) num+; iLoop+; printf(您输入了字符中有%d个小写字母!n,num); 6
19、.24 #include void main() int line; int iLoop1; int iLoop2; printf(请输入图案的行数(不大于26):); scanf(%d, &line); for(iLoop1 = 0; iLoop1 line; iLoop1+) for(iLoop2 = 0; iLoop2 line - iLoop1; iLoop2+) printf( ); for(iLoop2 = 0; iLoop2 2*iLoop1+1; iLoop2+) printf(%c,iLoop1 + A); printf(n); 第七章 一. 选择题 7.1 C 7.2 C
20、7.3 B 7.4 C 7.5 A 7.6 D 7.7 A 二. 填空题 7.8 12 7.9 9.000000 7.10 4 7.11 n=1 s 7.12 =y z*x 7.13 1 s*i 0 f(k) 三. 程序调试和编程题 7.14 fun(int n) int k,yes; for(k=2; k=n/2; k+) if(n%k = 0) yes = 0; break; else yes = 1; return yes; 7.15 int mymod(int a, int b) return a%b; 7.16 double fun(int n) double sum = 0; in
21、t iLoop; int sig = -1; for(iLoop=1; iLoop=n; iLoop+) sig *= -1; sum += sig*1.0/iLoop; return sum; 7.17 double fun(int n) double t = 1.0; int iLoop; long tmp; for(iLoop=2; iLoop=n; iLoop+) tmp = iLoop*iLoop; t -= 1.0/tmp; return t; 7.18 #include #include double fun(double x) return x*x + 5*x + 4; voi
22、d main() int x = 2; printf(y1=%fn, fun(x); printf(y2=%fn, fun(x+15); printf(y3=%fn, fun(sin(x); 第八章 一. 选择题 8.1 A 8.2 B 8.3 B 8.4 C 8.5 B 8.6 B 8.7 C 8.8 D 8.9 B 8.10 C 8.11 C 8.12 C 二. 填空题 8.13 110 8.14 7 1 8.15 (1)char *p=&ch; (2) p=&ch; (3)scanf(%c,p); (4)*p=A; (5)printf(%c,*p); 8.16 (1)s=p+3; (2)
23、s=s-2 (3)50 (4)*(s+1) (5)2 (6)10 20 30 40 50 三. 编程题 8.17 void fun(double x, double y, double *sum, double *div) *sum = x + y; *div = x - y; return; 8.18 void fun(double x, double y, double z, double *max, double *min) *max = x; *min = x; if(*max y) *max = y; if(*max y) *min = y; if(*min z) *min = z;
24、 return; 第九章 一. 选择题 9.1 D 9.2 A 9.3 A 9.4 C 9.5 C 9.6 A 9.7 B 9.8 D 9.9 C 9.10 C 9.11 C 9.12 D 9.13 D 9.14 A 9.15 A 9.16 A 9.17 C 9.18 C 二. 填空题 9.19 9 0 9.20 6 9.21 12 9.22 3 9.23 2721 9.24 -850,2,0 9.25 k=p k 9.26 (c=getchar() c-A 三. 编程题 9.27 #include #define N 81 int main() int counter10 = 0; int
25、iLoop = 0; char strN; gets(str); while(striLoop) if(striLoop = 0 & striLoop = 9) counterstriLoop - 0+; iLoop+; for(iLoop=0; iLoop 10; iLoop+) printf(%d - %dn, iLoop, counteriLoop); return 0; 9.28 void fun(int array, int arraysize, int start) int iLoop; if(start arraysize-1) if(start =0) start = 1; f
26、or(iLoop = start; iLoop arraysize; iLoop+) arrayiLoop-1 = arrayiLoop; for(iLoop = 0; iLoop arraysize; iLoop+) printf(No.%d = %dn, iLoop, arrayiLoop); 9.29 int fun(int arry1, int arry2, int arrysize) int iLoop; int counter = 0; for(iLoop = 0; iLoop arrysize; iLoop+) if(arry1iLoop % 2) arry2counter+ =
27、 arry1iLoop; return counter; 9.30 void fun(char array, int arraysize) int iLoop1; int iLoop2; char temp; /* 冒泡排序 */ for(iLoop1 = 0; iLoop1 arraysize - 1; iLoop1+) for(iLoop2 = 0; iLoop2 arraysize - 1 - iLoop1; iLoop2+) if(arrayiLoop2 arrayiLoop2 + 1) temp = arrayiLoop2; arrayiLoop2 = arrayiLoop2 + 1
28、; arrayiLoop2 + 1 = temp; 9.31 #include void fun(int array, int arraysize, int inertNumber) int iLoop; int iLoop2; if(array0 arrayarraysize-1) for(iLoop = 0; iLoop inertNumber) for(iLoop2 = arraysize - 1; iLoop2 = iLoop; iLoop2-) arrayiLoop2 + 1 = arrayiLoop2; arrayiLoop = inertNumber; break; if(iLo
29、op = arraysize) arrayarraysize = inertNumber; else for(iLoop = 0; iLoop arraysize; iLoop+) if(arrayiLoop = iLoop; iLoop2-) arrayiLoop2 + 1 = arrayiLoop2; arrayiLoop = inertNumber; break; if(iLoop = arraysize) arrayarraysize = inertNumber; int main() int iLoop; int a20 = 7,6,5,3,2,1; for(iLoop = 0; i
30、Loop 6; iLoop+) printf(%d , aiLoop); printf(n); fun(a, 6, 0); for(iLoop = 0; iLoop 7; iLoop+) printf(%d , aiLoop); printf(n); fun(a, 7, 4); for(iLoop = 0; iLoop 8; iLoop+) printf(%d , aiLoop); printf(n); fun(a, 8, 8); for(iLoop = 0; iLoop 9; iLoop+) printf(%d , aiLoop); printf(n); return 0; 9.32 int
31、 fun(int number, int array) int iLoop = 0; int iLoop2; int binLen; int midNumber; int div; int remain; midNumber = number; do div = midNumber/2; remain = midNumber%2; midNumber = div; arrayiLoop+ = remain; while(midNumber); binLen = iLoop; for(iLoop2 = 0, iLoop = binLen - 1; iLoop2 iLoop; iLoop2+, i
32、Loop-) midNumber = arrayiLoop2; arrayiLoop2 = arrayiLoop; arrayiLoop = midNumber; return binLen; 9.33 #include #include #define N 15 void fun(int array, int arraysize) int x; int iLoop; int iLoop2; for(iLoop = 0; iLoop 0) x = rand()%20; iLoop2 = 0; iLoop2+; while(iLoop2 iLoop); arrayiLoop = x; int m
33、ain() int aN; int iLoop; fun(a, N); for(iLoop = 0; iLoop N; iLoop+) printf(%dn, aiLoop); return 0; 第十章 一. 选择题 10.1 C 10.2 B 10.3 C 10.4 B 10.5 C 10.6 A 10.7 C 10.8 A 10.9 C 10.10 C 二. 填空题 10.11 GFEDCB 10.12 XYZ 10.13 SO 10.14 10 10.15 Itis 10.16 strlen(str)-1 j- 10.17 3 10.18 goodgood! 三. 编程题 10.19 char* mygets(char *str) int iLoop = 0; char ch; while(ch=getchar() != n) striLoop+ = ch; striLoop = 0; return str; char * myputs(char *