c语言程序设计课后习题解答_第1页
c语言程序设计课后习题解答_第2页
c语言程序设计课后习题解答_第3页
c语言程序设计课后习题解答_第4页
c语言程序设计课后习题解答_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第 3 章 习题解答第1章语言程序主要由预处理命令、函数、注释等组成。2. 填空(1) 分号(2) main(3)3.源程序:#includemain( ) printf( printf( printf( 4. 源程序: #include main( ) int a, b, c; /* scanf( “ %d” scanf( “ %d” c=a-b; /* printf( 5.Hello World!n”);n”);定义变量 */, &a); /*输入第一个整数, &b); /*输入第二个整数计算差 */%d-%d=%”d ,a,b,c); /*);*/*/输出结果 */(1) (2) x=1

2、0;(3) printf( “ s=%d n” ,s);第2章1.(1) c(2) a(3) b g(4) a d e(5) d2. a. 5 b. 295 c. 4 d. 29 e. 93. =4,y=6b. x=4,y=3=3,y=64. 165.#includemain()int a,b,c;scanf(%d%d,&a,&b);c=a*b;printf(%d*%d=%d,a,b,c);第3章1.(1) b(2) b(3) d(4) a(5) b2.(1) &a,&b(2) l,s3. printf( “x=%.2f,y=%.2f n” ,x,y);4.#includemain()int

3、num1,num2,num3,sum;float average;scanf(%d%d%d,&num1,&num2,&num3);sum=num1+num2+num3;average=sum/;printf(sum=%d,average=%.2fn,sum,average);5.#includemain()int hour,minute,second,total; /* 定义变量代表时、分、秒和总秒数 */ scanf(%d,&total);hour=total/3600;minute=total%3600/60;second=total%3600%60;printf(%dhours:%dmi

4、nutes:%dsecondsn,hour,minute,second); 第4章1.(1) a(2) b(3) b(4) b(5) b2. 03.(1) 6(2) 4(3) 1(4) 14. #include main( )int x,y; scanf(%d,&x); if (x-5 & x0 & x10) y=2*x+1;printf(x=%d,y=%dn,x,y);5.#include main( ) int score,rank; /* score表示成绩 ,rank 表示级别 */printf(Please input score:);scanf(%d,&score); rank=s

5、core/10;switch(rank)case 10:case 9: printf( 成绩等级为: An); break;case 8: printf( 成绩等级为: Bn); break;Cn); break;Dn); break;En); break;case 7: printf(成绩等级为:case 6: printf(成绩等级为:default:printf(成绩等级为: 6.#includevoid main()int n;printf(Please input the digit:); scanf(%d,&n);switch(n)case 1:printf(Jan n);brea

6、k;case 2:printf(Feb n);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 10:printf(Oct n);break;case 11:printf(Nov n);break;case 12:printf(Dec n);brea

7、k;第5章1.(1) b(2) a(3) b(4) d(5) d2. 7,13. y=-14. m=65. 10,146. 37. 58. ABABCDCD9.# include main ( )int i;long int sum=0;for (i=2;i=200;i+=2)sum+=i;printf(2+4+6+.+200=%ld,sum); 10.#include main()long int n;int sum = 0;printf(Please input the nber:); scanf(%ld,&n);while(n != 0)sum += n % 10;n /= 10;pri

8、ntf(%dn, sum);第6章1.(1) d(2) b(3) d(4) c(5) a2. 113. 34. 56895. 12,66. (1) i- (2) n7. (1) char k; (2) ij8.#include main( )float a10;int i;float sum=0,average;for (i=0;i10;i+) printf(a%d=?,i); scanf(%f,&ai); sum+=ai; average=sum/10;printf(average is %.2fn,average);9.#include #define N 10main( )int aN,

9、i,target,found;for (i=0;iN;i+)scanf(%d,&ai); printf(Please input a number:); scanf(%d,&target);i=0;while(iN & target != ai)i+;found = iN?i:-1; printf(%dn,found);10.#include #define N 80main( )char strN;int len=0,i=0;printf(Please input a string:); gets(str);while (stri+ != 0) len+;printf(the length

10、of string is %d.n,len); 第7章1. 212. 1363. 164. (1) func (2) m+5. 96. 1,6,3,2,37. 2,5,1,2,3,-28.#include int fun(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= ab?(ac?a:c

11、):(bc?b:c);return max;9.#include 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 void fun(n);main( )int n;printf(n=?);scanf(%d,&n);fun(n);void fun(n)if (n = = 0) return;elsefun(n/2); printf

12、(%-2d,n%2);第8章1.(1) b(2) d(3) b(4) c(5) c2. 83. 94. 2 3 4 5 65. 3456. 1 2 3 4 5 67. bi8. bcdefgha9. p=sum10. #include #include main() char str80; char *p1, *p2; gets(str); p1=str;p2=str+strlen(str)-1;while (p1p2 & *p1+ = *p2- ) ;puts( p1p2 ? 不是回文 : 是回文 ); 第9章1.a. #define F(x) (x)*(x)*(x)b. #define F

13、(x) (x)%4c. #define F(x,y) (x)*(y)100?1:02.a. 4b. 4c. #define DOUBLE(x) 2*(x)3. d4. -205. N is undefined6. 7.57. y=68.#include #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 studentint s

14、no;char sname10;char sex;stu1,stu2;2. 123. 合法的有 a,b,dc. 改成e. 改成f. 改成b+是合法的。 )4. a,b,c,d ( 说明:变量 b 有确定的值之后,5. 改为typedef struct productchar name10;float price;PRODUCT;PRODUCT products10;6.(1) struct employee(2) printemp(emp)7. 2 4 3 9 88.#include struct time_struct int hour; int minute; int second; ma

15、in( )struct time_struct time;printf(Input time?n(Example 18:28:38)n); scanf(%d:%d:%d,&,&,&;printf(Time is %d:%d:%dn,;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(exa

16、mple 18:28:38)?); scanf(%d:%d:%d,&,&,&;void display_time()printf(Time is %d:%d:%d.n,;10.#include #define N 3struct hotelchar name31; /* 旅馆名称 */char address31;/* 旅馆地址 */int grade; /* 旅馆级别 */float average_charge; /*平均房价 */int number; /* 房间数量 */hN=h1, 上海路 ,5,80,h2, 北京大街 ,5,70,h3, 南京大街 ,3,100;main( )int

17、 grade,i;printf( 请输入级别 (3-5)?);scanf(%d,&grade);for (i=0;iN;i+)if (hi.grade=grade)printf( 名称:%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) b2. Basican3. fgetc(fp)4. “”, “w”5.#include #include #include typedef struct E

18、mployeeint id;char name20;char gender20; int age;char address20;Employee;int main(void)FILE *fp;char another,choice;Employee emp; long int recsize; fp=fopen(,rb+);if(fp=NULL)fp=fopen( ,wb+); if(fp=NULL)printf(Cant Open File); exit(0); recsize=sizeof(emp);while(1)printf(1. 添加记录 2. 显示男员工 3. 退出 n); pri

19、ntf(Enter your choice(1-3):);fflush(stdin); scanf(%c,&choice);switch(choice)case1: fseek(fp,0,SEEK_END); another=Y;while(another=Y| another=y)printf( 输入信息 (id 姓名 性别 年龄 住址 ):n); scanf(%d %s %s %d%s,&,&,&,&,&; fwrite(&emp,recsize,1,fp); printf( 是否继续 (Y/N): );fflush(stdin); another=getchar(); break;cas

20、e 2:printf( 学号 t 姓名 t 性别 t 年龄 t 住址 n); rewind(fp);while(fread(&emp,recsize,1,fp)=1)if (strcmp,男 )=0)printf(%dt%st%st%dt%sn,break;case 3: fclose(fp);exit(0);6.#include#include#define M 2#define stu struct studentstuint 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=0;iM;i+)scanf(%d%s%f%f%f,&sti.num,s

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论