C语言程序设计苏小红版第五六单元答案资料_第1页
C语言程序设计苏小红版第五六单元答案资料_第2页
C语言程序设计苏小红版第五六单元答案资料_第3页
C语言程序设计苏小红版第五六单元答案资料_第4页
C语言程序设计苏小红版第五六单元答案资料_第5页
已阅读5页,还剩32页未读 继续免费阅读

下载本文档

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

文档简介

1、第五章5.1#include<stdio.h>voidmain()(doublex;printf(“请输入一个实数:n");scanf(H%lf",&x);if(x>0)(printf("%fn”,x);)else(printf(,%fnK,-x);)5.2#inelude<stdio.h>main()(inta,b;printf(“请输入一个整数:");scanf("%d",&a);b=a%2;if(b=O)(printf(*'%d为偶数n'a);)else(printf

2、(*'%d为奇数n'a);)5.3#include<stdio.h>#include<math.h>main()(floata,b,c,s,area;printf("请输入三边长:”);scanf(n%f,%f,%f",&a,&b,&c);s=(a+b+c)/2;area=(float)sqrt(s*(s-a)*(s-b)*(s-c);if(a+b>c)&&(a+c>b)&&(b+c>a)(printf("这个三角形的面积为%fn'area);

3、)else(printf("这三条边不能组成三角形n*');)5.4#include<stdio.h>#include<math.h>voidmain()(doublea,b,c5d,e,x1,x2;printf("请输入a,b,c:");scanf("%lf,%lf,%ir,&a,&b,&c);if(a=0)(printf("该方程不是一元二次方程.n");)elseif(b*b-4*a*c>0)(d=sqrt(b*b-4*a*c);x仁(b+d)/(2*a);x2=(-

4、b-d)/(2*a);printf("x1=%f,x2=%fn",x1,x2);)elseif(b*b-4*a*c=0)x1=(-b)/(2*a);printf(Hx1=x2=%fnn,x1);)elseif(b*b-4*a*c<0)(printf("该方程没有实数解.n“);)5.5#include<stdio.h>main()(intyear,flag;printf("输入年份:“);scanf("%dH,&year);if(year%4=0)&&(year%100!=0)|(year%400=0)

5、flag=1;)else(flag=O;)if(flag=1)(printf("d是闰年n",year);)else(printf("d不是闰年nH,year);)6.6#include<stdio.h>voidmain()(intyear,flag;printf("输入年份:“);scanf("%du,&year);flag=(year%4=0)&&(year%100!=0)|(year%400=0)?1:2;if(flag=1)(printf(”d年是闰年n",year);)else(print

6、f(*'%d年不是闰年n",year);)5.7#include<stdio.h>main()(charch;printf("请输入一个字母:");ch=getchar();if(ch>=65)&&(ch<=90)(ch=ch+32;printf(-c,%dn”,ch,ch);)elseif(ch>=97)&&(ch<=122)ch=ch-32;printf(-c,%dn”,ch,ch);)elseprintf(n%cnn5ch);5.8#include<stdio.h>mai

7、n()(charch;printf(“请输入一个字符:");ch=getchar();if(ch>=48)&&(ch<=57)(printf("这是一个数字字符.n");)elseif(ch>=65)&&(ch<=90)(printf("这是一个大写字母.W);)elseif(ch>=97)&&(ch<=122)(printf("这是一个小写字母.n“);)elseif(ch=32)(printf("这是一个空格字符.n“);)else(printf(

8、"这是一个其它字符.W);)5.9#include<stdio.h>main()intscore,mark;printf(HPleaseenterscore:1');scanf(八d”,&score);if(score<0|score>100)(printf("lnputerror!*n*');)elseif(score>=90&&score<=100)printf("d-An”,score);)elseif(score>=80&&score<90)(print

9、f(”%d-Bn”,score);)elseif(score>=70&&score<80)(printf(”%d-Cn”,score);)elseif(score>=60&&score<70)(printf(n%d-Dn",score);else(printf("%d-En”,score);)5.10#include<stdio.h>#include<stdlib.h>main()(intflag,year,month,day,day仁31,day2=30,day3=29,day4=28;pri

10、ntf("请以2012,03这种形式输入某年某月:scanf("%d,%dH,&year,&month);if(month>12|month<1)printf("你输入的年份不在合法范围内n");exit(O);)else(flag=(year%4=0&&year%1OO!=O|year%400=0)?1:0;switch(flag)(case1:prints闰年)break;case0:printf(“平年”);break;)if(month=2&&flag=1)day=day3;elseif

11、(month=2&&flag=O)day=day4;switch(month)(case1:case3:case5:case 7:case 8:case10:case12:printf("%d年,d月拥有的天数是31.nM,year,month);break;case4:case6:case9:case11:printf("%d年,d月拥有的天数是30.n",year,month);break;default:printf(M%d年,d月拥有的天数是%d:nM,year,month,day);本章实验题1#include<stdio.h>

12、;main()doublefaheight,maheight;charsex,sports,diet,F,M,Y,N;printf("请输入父母的身高:n");scanf("%lf,%lf",&faheight,&maheight);printf("请输入孩子是男孩还是女孩(F/M):n“);scanty%c",&sex);printf("请输入孩子是否喜欢锻炼身体(Y/N):n“);scanf(n%cH,&sports);printf("请输入孩子是否有良好的饮食习惯(Y/N):n

13、");scanf(n%cH,&diet);if(sex=77&&sports=89&&diet=89)(printf(“该孩子成人时身高约为:%.2f.n",(faheight+maheight)*0.54*1.02*1.015);)elseif(sex=77&&sports=89&&diet=78)(printf("该孩子成人时身高约为:%.2f.nM,(faheight+maheight)*0.54*1.02);)elseif(sex=77&&sports=78&&

14、amp;diet=89)(printf(“该孩子成人时身高约为:%.2f.nM,(faheight+maheight)*0.54*1.015);)elseif(sex=77&&sports=78&&diet=78)printf("该孩子成人时身高约为:%.2f.nn,(faheight+maheight)*0.54);elseif(sex=70&&sports=89&&diet=89)printf("该孩子成人时身高约为:%.2f.nH,(faheight*0.923+maheight)/2*1.02*1.01

15、5);elseif(sex=70&&sports=89&&diet=78)printf("该孩子成人时身高约为:%.2f.nH,(faheight*0.923+maheight)/2*1.02);)elseif(sex=70&&sports=78&&diet=89)(printf(“该孩子成人时身高约为:%.2f.nH,(faheight*0.923+maheight)/2*1.015);)elseif(sex=70&&sports=78&&diet=78)(printf("该孩

16、子成人时身高约为:%.2f.n",(faheight*0.923+maheight)/2);)本章实验题2#include<stdio.h>main()(doublet,w,h;printf(”请输入体重身高:n”);scanf("%lf,%lf",&w,&h);t=w/(h*h);if(t<18)(printf("低体重.H);)if(t>=18&&t<25)(printf("正常体重.n");)if(t>=25&&t<27)printf(&q

17、uot;超重体重.n");if(t>=27)printf(M肥胖.n”);)弟八早6.1 #include<stdio.h>main()(intij,k;charspace-';for(i=1;iv=4;i+)(for(j=1;jv=i;j+)(printf("%c",space);)for(k=1;k<=6;k+)(prinprintfC'n");)6.1(2)#include<stdio.h>main()(intk=4,n;for(n=0;n<k;n+)(if(n%2=0)continue;k

18、-;)printf("k=%dn,n=%dnu,k,n);6.1(3)#include<stdio.h>main()(intk=4,n;for(n=0;n<k;n+)(if(n%2=0)break;k-;)printf("k=%d,n=%dnH,k,n);)6.2(1)# include<stdio.h>main()(inti,sum=0;for(i=1;i<=101;i=i+2)(sum=sum+i;)printf("sum=%dnn,sum);)6.2(2)# include<stdio.h>main()(lon

19、gi;longterm,sum=0;for(i=1;i<=99;i=i+2)term=i*(i+1)*(i+2);sum=sum+term;)printf(nsum=%ldn",sum);)6.2(3)# include<stdio.h>main()(longterm=0,sum=0;inta,i,n;printf("lnputa,n:n);scanf("%d,%d",&a,&n);for(i=1;i<=n;i+)(term=term*10+a;sum=sum+term;)printf(nsum=%ldnH,sum

20、);)6.2(4)#include<stdio.h>#include<math.h>main()(intn=1;floatterm=1.0,sign=1,sum=0;while(fabs(term)>=1e-4)(term=sign/n;sum=sum+term;sign=-sign;n+;)printf("sum=%fnu);)6.2(5)#include<stdio.h>#include<math.h>main()(intn=15count=1;floatx;doublesum,term;printf('1nputx:

21、u);scan&x);sum=x;term=x;doterm=-term*x*x/(n+1)*(n+2);sum=sum+term;n=n+2;count+;Jwhile(fabs(term)>=1e-5);printf("sin(x)=%f,coumt=%dn",sum,count);)6.3#include<stdio.h>main()(intx=1,find=0;while(!find)(if(x%2=1&&x%3=2&&x%5=4&&x%6=5&&x%7=0)(find=1;)

22、x+;)printf("x=%dnn,x-1);)6.4#include<stdio.h>main()(inti,n;longpj;printf("Pleaseentern:");scanf(n%d",&n);for(i=1;i<=n;i+)(P=i*i;j=i*i*i;printf("%d*%d=%ldn",i,i,p);printf(H%d*%d*%d=%ldn,',i,i,i,j);6.5#include<stdio.h>main()(doubleC,F,i;for(F=-40;F&

23、lt;=110;F=F+10)C=%fn"EC);C=(F-32)*5/9;printf(T=%f)6.6#include<stdio.h>main()(doublep=1+0.01875*12,x4,x3,x2,benjin;x4=1000/p;x3=(x4+1000)/p;x2=(x3+1000)/p;benjin=(x2+1000)/p;printf("本金为:%f",benjin);)6.7#include<stdio.h>main()(doublecount=0,c,d5sum=100;printf('1nputcscan

24、f("%lf",&c);d=c/100.0;dosum=sum*(1+d);count+;while(sum<=200);printf("count=%fnn,count);6.8#include<stdio.h>#include<math.h>main()(intcount=1;doublesum=1.0,term,n=3.0J=1.0;doterm=-i/n;sum=sum+term;n=n+2;I=-I;count+;while(fabs(term)>=1e-5);printf(Hn=%lf,count=%d.n&

25、quot;,4*sum,count);6.9# include<stdio.h># inelude<math.h>main()(intcount=1;doublesum=1.0,term=1.0,n=1.0;while(fabs(term)>=1e-5)(term=term/n;sum=sum+term;n+;count+;)printf(He=%lf,count=%d.nfl,sum5count);)6.10# include<stdio.h>main()(inti,a,b,c5d;for(i=100;i<=999;i+)(a=i/100;c=

26、i%10;b=(i-a*100-c)/10;d=a*a*a+b*b*b+c*c*c;if(i=d)(printf('1=%dn",i);)6.11#include<stdio.h>main()longm=2,n,term=1,sum=1;printf(Hlnputn:H);scanf("%ld",&n);doterm=term*m;sum=sum+term;m+;while(sum<=n);printf("m=%d.n”,m-2);)6.12#include<stdio.h>main()(intnum=1,s

27、um=0,count=0;while(num>0)(printf('1nputnum:");scanf(n%d",&num);sum=sum+num;count+;)printf("sum=%d,count=%d.nH,sum,count-1);6.13# include<stdio.h>main()(intterm,sum=0,count=0;dobegin:printf('1nputterm:");scant(n%d",&term);if(term>0)(sum=sum+term;co

28、unt+;)elseif(term<0)gotobegin;elsegotoend;Jwhile(term!=0);end:printf(Hsum=%d.count=%d.nlsum,count);)6.14# include<stdio.h>main()(inta,b,c;for(a=0;a<17;a+)for(b=0;b<=25;b+)(c=30-a-b;if(a*3+b*2+c=50)printf("男=%"女=%"孩子=%d.n",a,b,c);)6.15# include<stdio.h>main()(

29、inta,b;for(a=0;a<=98;a+)b=98-a;67孑蛊丹*ncludAsfdp-hv6-8ma-n()一三XXNfor(XH一八Xfor(yHy二(XAIo八X+A20八y+10+y5+zHuI00)-f(apz.nff*4Hu386)n由血QdJn云-66*ncludAsfd-O.hvmain。-一NFXMNfor(XHoXAH2oX+)-for(yHol<AH33P+)-zJoo-x-y八二(5*x+3*y+z、30HH-oo一-Dz.nff(=XH%dlyH%dN上:#include<stdio.h>main()(inti,j;for(i=1;i&

30、lt;=9;i+)(for(j=1;jv=9;j+)(printf(',%-5d',i*j);)printf("n");)左:#include<stdio.h>main()(inti,j;for(i=1;i<=9;i+)(for(j=1;j<=i;j+)(printf(',%-5d',i*j);)printf("n");)右:#include<stdio.h>main()(inti,j,a=1,k;charb='*;for(i=1;i<=9;i+)for(k=1;k<

31、=i;k+)for(j=i;j<=9;j+)(a=j*i;printf(n%5d",a);)printf("n");)6.19#include<stdio.h>main()(longi,summo;doublesumfu=0,term=0.01;summo=100000*30;for(i=1;i<=30;i+)(term=term*2.0;sumfu=sumfu+term;)printf("陌生人付ld,富翁付%lf.nH,summo,sumfu);)本章实验题2.1#include<stdio.h>#include<stdlib.h>#include<time.h>main()(intx1,x2,c,answer;begin:s

温馨提示

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

评论

0/150

提交评论