




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第3章 习题解答 第1章 1.C 语言程序主要由预处理命令、函数、注释等组成。 2.填空 (1)分号 (2)main (3)stdio.h 3. 源程序: #include<stdio.h> main( ) printf(“*n”); printf(“Hello World!n”); printf(“*”); 4. 源程序: #include <stdio.h> main( ) int a, b, c; /* 定义变量 */ scanf(“%d”, &a); /* 输入第一个整数 */ scanf(“%d”, &b); /* 输入第二个整数 */ c=a
2、-b; /* 计算差 */ printf(“%d-%d=%d”,a,b,c); /* 输出结果 */ 5. (1)<stdio.h> (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<stdio.h> main() int a,b,c; scanf("%d%d",&a,&b)
3、; c=a*b; printf("%d*%d=%d",a,b,c); 第3章 1. (1) b (2) b (3) d (4) a (5) b 2. (1)&a,&b (2)l,s 3. printf(“x=%.2f,y=%.2fn”,x,y); 4. #include<stdio.h> main() int num1,num2,num3,sum; float average; scanf("%d%d%d",&num1,&num2,&num3); sum=num1+num2+num3; average=
4、sum/3.0; printf("sum=%d,average=%.2fn",sum,average); 5. #include<stdio.h> main() int hour,minute,second,total; /* 定义变量代表时、分、秒和总秒数 */ scanf("%d",&total); hour=total/3600; minute=total%3600/60; second=total%3600%60; printf("%dhours:%dminutes:%dsecondsn",hour,min
5、ute,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 <stdio.h> main( ) int x,y; scanf("%d",&x); if (x>-5 && x<0) y=5*x; if (x = 0) y=-1; if (x>0 && x<10) y=2*x+1; printf("x=%d,y=%dn",x,y); 5. #include
6、<stdio.h> main( ) int score,rank; /* score 表示成绩,rank 表示级别 */ printf("Please input score:"); scanf("%d",&score); rank=score/10; switch(rank) case 10: case 9: printf("成绩等级为:An"); break; case 8: printf("成绩等级为:Bn"); break; case 7: printf("成绩等级为:Cn&q
7、uot;); break; case 6: printf("成绩等级为:Dn"); break; default:printf("成绩等级为:En"); break; 6. #include<stdio.h> void main() int n; printf("Please input the digit:"); scanf("%d",&n); switch(n) case 1:printf("Jan n");break; case 2:printf("Feb n
8、");break; case 3:printf("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
9、10:printf("Oct n");break; case 11:printf("Nov n");break; case 12:printf("Dec n");break; 第5章 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 <stdio.h> main ( ) int i; long int sum=0; for (i=2;i<=200;i+=2) sum+=i
10、; printf("2+4+6+.+200=%ld",sum); 10. #include <stdio.h> main() long int n; int sum = 0; printf("Please input the nber:"); scanf("%ld",&n); while(n != 0) sum += n % 10; n /= 10; printf("%dn", sum); 第6章 1. (1) d (2) b (3) d (4) c (5) a 2. 11 3. 3 4. 56
11、89 5. 12,6 6. (1) i- (2) n 7. (1) char k; (2) i<j 8. #include <stdio.h> main( ) float a10; int i; float sum=0,average; for (i=0;i<10;i+) printf("a%d=?",i); scanf("%f",&ai); sum+=ai; average=sum/10; printf("average is %.2fn",average); 9. #include <stdi
12、o.h> #define N 10 main( ) int aN,i,target,found; for (i=0;i<N;i+) scanf("%d",&ai); printf("Please input a number:"); scanf("%d",&target); i=0; while(i<N && target != ai) i+; found = i<N?i:-1; printf("%dn",found); 10. #include <st
13、dio.h> #define N 80 main( ) char strN; int len=0,i=0; printf("Please input a string:"); gets(str); while (stri+ != 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 <stdio.h> int f
14、un(int a,int b,int c); main( ) int a,b,c,max; printf("Please input three numbers:"); scanf("%d%d%d",&a,&b,&c); max=fun(a,b,c); printf("Max is %d.n",max); int fun(int a,int b,int c) int max; max= a>b?(a>c?a:c):(b>c?b:c); return max; 9. #include <s
15、tdio.h> long int sum(int n); main( ) int n; printf("n=?"); scanf("%d",&n); printf("Sum=%ld.n",sum(n); long int sum(int n) if (n = = 1 ) return 1; else return sum(n-1)+n; 10. #include <stdio.h> void fun(n); main( ) int n; printf("n=?"); scanf("
16、;%d",&n); 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 <stdio.h> #include <string.h> main() char str80; char
17、 *p1, *p2; gets(str); p1=str; p2=str+strlen(str)-1; while (p1<p2 && *p1+ = *p2- ) ; puts( p1<p2 ? "不是回文" : "是回文" ); 第9章 1. a. #define F(x) (x)*(x)*(x) b. #define F(x) (x)%4 c. #define F(x,y) (x)*(y)<100?1:0 2. a. 4 b. 4 c. #define DOUBLE(x) 2*(x) 3. d 4. -20 5. N
18、 is undefined 6. 7.5 7. y=6 8. #include <stdio.h> #define SWAP(a,b) int temp;temp=a;a=b;b=temp; main() int x,y; printf("x=?"); scanf("%d",&x); printf("y=?"); scanf("%d",&y); SWAP(x,y) printf("x=%d,y=%d.n",x,y); 第10章 1. struct student in
19、t sno; char sname10; char sex; stu1,stu2; 2. 12 3. 合法的有a,b,d c.改成 s.u.rectangle.length=25; e.改成 s.u.circle.radius=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(
20、emp) 7. 2 4 3 9 8 8. #include <stdio.h> struct time_struct int hour; int minute; int second; ; main( ) struct time_struct time; printf("Input time?n(Example 18:28:38)n"); scanf("%d:%d:%d",&time.hour,&time.minute,&time.second); printf("Time is %d:%d:%dn"
21、;,time.hour,time.minute,time.second); 9. #include <stdio.h> 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&quo
22、t;,&time.hour,&time.minute,&time.second); void display_time() printf("Time is %d:%d:%d.n",time.hour,time.minute,time.second); 10. #include <stdio.h> #define N 3 struct hotel char name31; /*旅馆名称*/ char address31;/*旅馆地址*/ int grade; /*旅馆级别*/ float average_charge; /*平均房价*/ i
23、nt number; /*房间数量*/ hN= "h1","上海路",5,500.00,80, "h2","北京大街",5,480.00,70, "h3","南京大街",3,300.50,100 ; main( ) int grade,i; printf("请输入级别(3-5)?"); scanf("%d",&grade); for (i=0;i<N;i+) if (hi.grade=grade) printf("
24、;名称:%sn 地址:%sn 级别:%dn平均房价%.2fn 房间数量:%dn", ,hi.address,hi.grade,hi.average_charge,hi.number); 第11章 1. (1) c (2) d (3) b (4) b (5) b 2. Basican 3. fgetc(fp) 4. “record.dat”, “w” 5. #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct Employee int id;
25、char name20; char gender20; int age; char address20; Employee; int main(void) FILE *fp; char another,choice; Employee emp; long int recsize; fp=fopen("employee.dat","rb+"); if(fp=NULL) fp=fopen( "employee.dat","wb+"); if(fp=NULL) printf("Can't Open Fi
26、le"); exit(0); recsize=sizeof(emp); while(1) printf("1.添加记录 2.显示男员工 3.退出n"); printf("Enter your choice(1-3):"); fflush(stdin); scanf("%c",&choice); switch(choice) case'1': fseek(fp,0,SEEK_END); another='Y' while(another='Y'| another='
27、;y') printf("输入信息(id 姓名 性别 年龄 住址):n"); scanf("%d %s %s %d %s",&emp.id,&,&emp.gender,&emp.age,&emp.address); fwrite(&emp,recsize,1,fp); printf("是否继续 (Y/N): "); fflush(stdin); another=getchar(); break; case '2': printf("学号t
28、 姓名t性别t 年龄t住址n"); rewind(fp); while(fread(&emp,recsize,1,fp)=1) if (strcmp(emp.gender,"男")=0) printf("%dt%st%st%dt%sn", emp.id,,emp.gender,emp.age,emp.address); break; case '3': fclose(fp); exit(0); 6. #include"stdio.h" #include"stdlib.h" #define M 2 #define stu struct student stu int num; char name20; float s1; float s2; float s3; float avg; ; main() stu stM; FILE *fp; int i; printf("请输入 5 名同学生的成绩,按照学号,姓名,成绩 1,成绩2,成绩 3 的顺序,中间用空格隔开:n"); for(i
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年入党积极分子党校培训考试题库及答案(共740题)
- 2025年带式输送机操作工技能认定理论考试题(附答案)
- 关于余秋雨励志经典语录
- 2024年计算机二级考试知识要点归纳试题及答案
- 湖南省岳阳市湘阴县城南区2024-2025学年九年级下学期3月月考语文试题(原卷版+解析版)
- 家庭家教家风活动总结
- 工厂电工个人总结
- 2025超高层商务楼群不锈钢门窗施工合同
- 2025年度城市轨道交通建设合同(最终版)
- 企业借给个人借款合同范本
- 铁路涵洞及框架桥接长施工方案及方法
- 《管子》的智慧课件
- 物品交接确认书
- 水利工程质量管理讲义课件
- 张利《新营销》的完整版
- 四川省存量房买卖合同
- 2X型真空泵说明书
- 中考化学备考复习策略【最新实用精品】课件
- 药品说明书和标签管理规定(培训)课件
- YYT 0681.18-2020 无菌医疗器械包装试验方法 第18部分:用真空衰减法无损检验包装泄漏
- 三下健康成长教案
评论
0/150
提交评论