1、第 3 章 习题解答 第 1 章 1.C 语言程序主要由预处理命令、函数、注释等组成。 2.填空 (1)分号 (2)main (3)stdio.h 3. 源程序: #include main( ) printf(“*n”); printf(“Hello World!n”); printf(“*”); 4. 源程序: #include main( ) int a, b, c; /* 定义变量 */ scanf(“%d”, /* 输入第一个整数 */ scanf(“%d”, /* 输入第二个整数 */ c=a-b; /* 计算差 */ printf(“%d-%d=%d”,a,b,c); /* 输出
2、结果 */ 5. (1) (2)x=10; (3)printf(“s=%dn”,s); 第 2 章 1. (1) c (2) a (3) b g (4) a d e (5) d 2. a. 5 b. 295 c. 4 d. 29 e. 9 3. a.x=4,y=6 b. x=4,y=3 f.x=3,y=6 4. 16 5. #include main() int a,b,c; scanf(“%d%d“, c=a*b; printf(“%d*%d=%d“,a,b,c); 第 3 章 1. (1) b (2) b (3) d (4) a (5) b 2. (1) 4. #include main(
3、) int num1,num2,num3,sum; float average; scanf(“%d%d%d“, sum=num1+num2+num3; average=sum/3.0; printf(“sum=%d,average=%.2fn“,sum,average); 5. #include main() int hour,minute,second,total; /* 定义变量代表时、分、秒和总秒数 */ scanf(“%d“, hour=total/3600; minute=total%3600/60; second=total%3600%60; printf(“%dhours:%d
4、minutes:%dsecondsn“,hour,minute,second); 第 4 章 1. (1) a (2) b (3) b (4) b (5) b 2. 0 3. (1) 6 (2) 4 (3) 1 (4) 1 4. #include main( ) int x,y; scanf(“%d“, if (x-5 /* score 表示成绩,rank 表示级别 */ printf(“Please input score:“); scanf(“%d“, rank=score/10; switch(rank) case 10: case 9: printf(“成绩等级为:An“); brea
5、k; case 8: printf(“成绩等级为:Bn“); break; case 7: printf(“成绩等级为:Cn“); break; case 6: printf(“成绩等级为:Dn“); break; default:printf(“成绩等级为:En“); break; 6. #include void main() int n; printf(“Please input the digit:“); scanf(“%d“, switch(n) case 1:printf(“Jan n“);break; case 2:printf(“Feb n“);break; case 3:pr
6、intf(“Mar n“);break; case 4:printf(“Apr n“);break; case 5:printf(“May n“);break; case 6:printf(“Jun n“);break; case 7:printf(“Jul n“);break; case 8:printf(“Agu n“);break; case 9:printf(“Sep n“);break; case 10:printf(“Oct n“);break; case 11:printf(“Nov n“);break; case 12:printf(“Dec n“);break; 第 5 章
7、1. (1) b (2) a (3) b (4) d (5) d 2. 7,1 3. y=-1 4. m=6 5. 10,14 6. 3 7. 5 8. ABABCDCD 9. # include main ( ) int i; long int sum=0; for (i=2;i main() long int n; int sum = 0; printf(“Please input the nber:“); scanf(“%ld“, while(n != 0) sum += n % 10; n /= 10; printf(“%dn“, sum); 第 6 章 1. (1) d (2) b
8、(3) d (4) c (5) a 2. 11 3. 3 4. 5689 5. 12,6 6. (1) i- (2) n 7. (1) char k; (2) i main( ) float a10; int i; float sum=0,average; for (i=0;i #define N 10 main( ) int aN,i,target,found; for (i=0;i #define N 80 main( ) char strN; int len=0,i=0; printf(“Please input a string:“); gets(str); while (stri+
9、!= 0) len+; printf(“the length of string is %d.n“,len); 第 7 章 1. 21 2. 136 3. 16 4. (1) func (2) m+ 5. 9 6. 1,6,3,2,3 7. 2,5,1,2,3,-2 8. #include int fun(int a,int b,int c); main( ) int a,b,c,max; printf(“Please input three numbers:“); scanf(“%d%d%d“, max=fun(a,b,c); printf(“Max is %d.n“,max); int f
10、un(int a,int b,int c) int max; max= ab?(ac?a:c):(bc?b:c); return max; 9. #include long int sum(int n); main( ) int n; printf(“n=?“); scanf(“%d“, printf(“Sum=%ld.n“,sum(n); long int sum(int n) if (n = = 1 ) return 1; else return sum(n-1)+n; 10. #include void fun(n); main( ) int n; printf(“n=?“); scan
11、f(“%d“, fun(n); void fun(n) if (n = = 0) return; else fun(n/2); printf(“%-2d“,n%2); 第 8 章 1. (1) b (2) d (3) b (4) c (5) c 2. 8 3. 123456789 4. 2 3 4 5 6 5. 345 6. 1 2 3 4 5 6 7. bi 8. bcdefgha 9. p=sum 10. #include #include main() char str80; char *p1, *p2; gets(str); p1=str; p2=str+strlen(str)-1;
12、while (p1 #define SWAP(a,b) int temp;temp=a;a=b;b=temp; main() int x,y; printf(“x=?“); scanf(“%d“, printf(“y=?“); scanf(“%d“, SWAP(x,y) printf(“x=%d,y=%d.n“,x,y); 第 10 章 1. struct student int sno; char sname10; char sex; stu1,stu2; 2. 12 3. 合法的有 a,b,d c.改成 s.u.rectangle.length=25; e.改成 s.u.circle.ra
13、dius=5; f.改成 s.u.circle.radius=5; 4. a,b,c,d (说明:变量 b 有确定的值之后,b+是合法的。) 5. 改为 typedef struct product char name10; float price; PRODUCT; PRODUCT products10; 6. (1) struct employee (2) printemp(emp) 7. 2 4 3 9 8 8. #include struct time_struct int hour; int minute; int second; ; main( ) struct time_stru
14、ct time; printf(“Input time?n(Example 18:28:38)n“); scanf(“%d:%d:%d“, printf(“Time is %d:%d:%dn“,time.hour,time.minute,time.second); 9. #include struct time_struct int hour; int minute; int second; time; void enter_time(); void display_time(); main( ) enter_time(); display_time(); void enter_time() printf(“Enter the time(example 18:28:38)?“); scanf(“%d:%d:%d“, void display_time() printf(“Time is %d:%d:%d.n“,time.hour,time.minute,time.second); 10.