C语言课后习题答案_第1页
C语言课后习题答案_第2页
C语言课后习题答案_第3页
C语言课后习题答案_第4页
C语言课后习题答案_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

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

文档简介

1、第4章后练习10. ch4_lx10.c #include void main() int m=1,n=1,s=0,k=1; while(n=10) while(m=n) printf(%d,m); m+; printf(n); n+; m=1; ch4_lx12.c 输入一行字符串,统计有多少字母. #include void main() int m=0;char ch;while( (ch=getchar() )!=n) if(ch=a&ch=a&ch=z) m+; printf(%dn,m);ch4_lx13.c 猜字游戏#include#include#include#include

2、void main( ) int magic ; int guess; srand( (unsigned) time(null) ); magic=rand(); printf(enter your guess:);scanf(%d,&guess); while( guess!=magic) if(guessmagic) printf( * too highn*); else printf( * too lown*); printf(enter your guess:); scanf(%d,&guess); if(guess =magic) printf(*right*n); break; 1

3、1. 输入一自然数,输出它的因子。解1. #includeint main()int w,i=2; printf(请输入一个整数w: n); scanf(%d,&w); printf(%d的因子是:,w);while(w) while(w) if(w%i=0) printf( %d ,i); w=w/i; else break; i+; return 0; ch5_lx14.c 求 1*3*5*7*n#includeint main()int i,x,s=1;printf(请输入一个奇数:n);scanf(%d,&x);for(i=1;imagic) printf(“ * too highn*

4、”); else printf(“ * too low *n”); main( ) 猜字游戏(二) int magic =123; int guess; printf(enter your guess:); scanf(%d,&guess); while(guess!=magic) if(guess =magic) printf(*right*n); else printf( * wrong*n); if(guessmagic) printf( * too highn*); else printf( * too lown*); printf(enter your guess:); scanf(

5、%d,&guess); 例:一球从100米高度自由落下,每次落地后都弹回原高度一半落下,求它在第10次落地时, 共经过多少米?第10次反弹多高?main() float sn=100,hn=sn/2; int n; for(n=2;n0) x1=(x2+1)*2; x2=x1; day-; printf(“total=%dn”,x1); 12. #include void main() int m=0;char ch;while( (ch=getchar() )!=n) if(ch=a&ch=a&ch=z) m+; printf(%dn,m);第5章后练习把100200之间的不能被3整除的数输

6、出。 main( ) int n; for(n=100; n=200; n+) if(n%3 =0) continue; printf(“%5d” , n); 例: 问程序输出的结果? main() int i=1,a=0; for(; i=5; i+) do i+; a+; while(i3); i+; printf(“a=%d,i=%dn”,a,i); 输出:a=3 ,i=8例: 找出所有的三位数,它和它的各位数字的立方和正好相 等. 例如:153=13+53+33. 设所求的三位数,其百位数字是i,十位数字是j,个位数字 是k. 显然应满足如下表达式: i*i*i+j*j*j +k*k*

7、k = 100*i+10*j+kmain( ) int i,j,k; for(i=1; i=9; i+) for(j=0; j=9; j+) for(k=0; k=9; k+) if(i*i*i+j*j*j +k*k*k = =100*i+10*j+k ) printf(“%d%d%dn”, i,j, k); 第6章后练习示例: 输入2个数,如果它们的值不相等,则交换并输出它们的值;否则,输出“equal”。 交换a和b值的方法: temp=a; a=b; b=temp;#include void main( ) int a, b, temp; printf(input a, b: ); sc

8、anf(%d%d, &a, &b); if(a != b) temp = a; a = b; b = temp; printf(a = %d, b = %dn, a, b); else printf(equaln);示例: 输入3个整数,输出其中的最大值#include void main( ) int a, b, c, max; printf(input a, b, c: ); scanf(%d%d%d, &a, &b, &c); max = a; if(max b) max = b; if(max c) max = c; printf(max is %dn, max);例: 输入x,计算下

9、列分段函数f(x). 1/x x0 f(x)= 0 x=0#include void main()float x,y;printf(input x: n);scanf(%f,&x);if ( x != 0 ) y = 1/x;else y = 0;printf(f(%f) = %.2fn, x, y);例: 输入x,计算下列分段函数f(x). x+1 x 1 f(x)= x+2 1x2 x+3 x 2#includevoid main( ) double x,y; printf(请输入x:); scanf(%lf,&x); if(x1) y=x+1; else if(x2) y=x+2;els

10、e y=x+3; printf(f(%.2f)=%.2fn,x,y); 7. 将大写字符转换为小写. #includevoid main() char ch; while(ch=getchar()!=n) if(ch=a&ch=z) ch+=32; printf(%c,ch); else printf(%c,ch); printf(n);8. 统计字符#include #include int main() char ch; int x=0,y=0,z=0,a=0,b=0; printf(请输入:n);while(ch=getchar()!=n) if(ch=a&ch=a&ch=0&ch=9)

11、 z+; else if(ch= |ch=n) a+; else b+; printf(小写字母%d个n,x); printf(大写字母%d个n,y); printf(数字字符%d个n,z); printf(空格字符%d个n,a); printf(其它字符%d个n,b); return 0;9计算分段函数#includevoid main() float x,y; printf(请输入x:n); scanf(%f,&x); if(x1&x=10 ) y=2*x; else y=(3/(x-10);printf(x=%f y=%fn,x,y);10.五分制与百分制的转换#includevoid

12、main() int x,i; printf(该程序功能为五分制与百分制的转换!n); printf(1 进入, 2 退出!n); scanf(%d,&i);while(i=1) printf(请输入:n); scanf(%d,&x); if(x=90 & x=80 & x=70&x=60&x70) x=2; else x=1;printf(五分制成绩为%d分n,x); printf(“1 继续 2 退出!n); scanf(%d,&i); 第7章后练习1百分制转换成五分制. #include int main() int x;printf(请输入学生百分制成绩:n输入负数结束程序:n);sc

13、anf(%d,&x);while(x0) switch(x/10) case 10: case 9: printf(五分制为:n5nn);break; case 8: printf(五分制为:n4nn);break;case 7: printf(五分制为:n3nn);break;case 6: printf(五分制为:n2nn);break; default: printf(五分制为:n1nn);break; printf(请输入学生百分制成绩:n输入负数结束程序:n); scanf(%d,&x); printf(谢谢使用,再见:n);return 0;2计算sin(),cos(),tan()

14、.#include #includeint main()double x,s; int y;printf(input a number:n);scanf(%lf,&x);while(1) printf(您希望进行哪种运算?n); printf(1.计算sin(x)n); printf(2.计算cos(x)n); printf(3.计算tan(x)n); scanf(%d,&y); switch(y) case 1: s=sin(x); printf(sin(%f)=%fn,x,s);break; case 2: s=cos(x); printf(cos(%f)=%fn,x,s);break;

15、case 3: s=tan(x); printf(tan(%f)=%fn,x,s);break; default: printf(“输入有误,请输入 1 - 3之间的数字!n); if(y=1|y=2|y=3) break; printf(谢谢使用:n); return 0;acknowledgements my deepest gratitude goes first and foremost to professor aaa , my supervisor, for her constant encouragement and guidance. she has walked me thr

16、ough all the stages of the writing of this thesis. without her consistent and illuminating instruction, this thesis could not havereached its present form. second, i would like to express my heartfelt gratitude to professor aaa, who led me into the world of translation. i am also greatly indebted to

17、 the professors and teachers at the department of english: professor dddd, professor ssss, who have instructed and helped me a lot in the past two years. last my thanks would go to my beloved family for their loving considerations and great confidence in me all through these years. i also owe my sin

18、cere gratitude to my friends and my fellow classmates who gave me their help and time in listening to me and helping me work out my problems during the difficult course of the thesis. my deepest gratitude goes first and foremost to professor aaa , my supervisor, for her constant encouragement and guidance. she has walked me through all the stages of the writing of this thesis. without her consistent and illuminating instruction, this thesis could not havereached its present form. second

温馨提示

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

评论

0/150

提交评论