您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > C语言程序设计苏小红版第五六单元答案
第五章5.1#includestdio.hvoidmain(){doublex;printf(请输入一个实数:\n);scanf(%lf,&x);if(x0){printf(%f\n,x);}else{printf(%f\n,-x);}}5.2#includestdio.hmain(){inta,b;printf(请输入一个整数:);scanf(%d,&a);b=a%2;if(b==0){printf(%d为偶数\n,a);}else{printf(%d为奇数\n,a);}}5.3#includestdio.h#includemath.hmain(){floata,b,c,s,area;printf(请输入三边长:);scanf(%f,%f,%f,&a,&b,&c);s=(a+b+c)/2;area=(float)sqrt(s*(s-a)*(s-b)*(s-c));if((a+bc)&&(a+cb)&&(b+ca)){printf(这个三角形的面积为%f\n,area);}else{printf(这三条边不能组成三角形\n);}}5.4#includestdio.h#includemath.hvoidmain(){doublea,b,c,d,e,x1,x2;printf(请输入a,b,c:);scanf(%lf,%lf,%lf,&a,&b,&c);if(a==0){printf(该方程不是一元二次方程.\n);}elseif(b*b-4*a*c0){d=sqrt(b*b-4*a*c);x1=(-b+d)/(2*a);x2=(-b-d)/(2*a);printf(x1=%f,x2=%f\n,x1,x2);}elseif(b*b-4*a*c==0){x1=(-b)/(2*a);printf(x1=x2=%f\n,x1);}elseif(b*b-4*a*c0){printf(该方程没有实数解.\n);}}5.5#includestdio.hmain(){intyear,flag;printf(输入年份:);scanf(%d,&year);if(((year%4==0)&&(year%100!==0))||(year%400==0)){flag=1;}else{flag=0;}if(flag==1){printf(%d是闰年\n,year);}else{printf(%d不是闰年\n,year);}}5.6#includestdio.hvoidmain(){intyear,flag;printf(输入年份:);scanf(%d,&year);flag=(((year%4==0)&&(year%100!=0))||(year%400==0))?1:2;if(flag==1){printf(%d年是闰年\n,year);}else{printf(%d年不是闰年\n,year);}}5.7#includestdio.hmain(){charch;printf(请输入一个字母:);ch=getchar();if((ch=65)&&(ch=90)){ch=ch+32;printf(%c,%d\n,ch,ch);}elseif((ch=97)&&(ch=122)){ch=ch-32;printf(%c,%d\n,ch,ch);}else{printf(%c\n,ch);}}5.8#includestdio.hmain(){charch;printf(请输入一个字符:);ch=getchar();if((ch=48)&&(ch=57)){printf(这是一个数字字符.\n);}elseif((ch=65)&&(ch=90)){printf(这是一个大写字母.\n);}elseif((ch=97)&&(ch=122)){printf(这是一个小写字母.\n);}elseif(ch==32){printf(这是一个空格字符.\n);}else{printf(这是一个其它字符.\n);}}5.9#includestdio.hmain(){intscore,mark;printf(Pleaseenterscore:);scanf(%d,&score);if(score0||score100){printf(Inputerror!\n);}elseif(score=90&&score=100){printf(%d--A\n,score);}elseif(score=80&&score90){printf(%d--B\n,score);}elseif(score=70&&score80){printf(%d--C\n,score);}elseif(score=60&&score70){printf(%d--D\n,score);}else{printf(%d--E\n,score);}}5.10#includestdio.h#includestdlib.hmain(){intflag,year,month,day,day1=31,day2=30,day3=29,day4=28;printf(请以2012,03这种形式输入某年某月:);scanf(%d,%d,&year,&month);if(month12||month1){printf(你输入的年份不在合法范围内\n);exit(0);}else{flag=(year%4==0&&year%100!=0||year%400==0)?1:0;switch(flag){case1:printf(闰年);break;case0:printf(平年);break;}if(month==2&&flag==1)day=day3;elseif(month==2&&flag==0)day=day4;switch(month){case1:case3:case5:case7:case8:case10:case12:printf(%d年,%d月拥有的天数是31.\n,year,month);break;case4:case6:case9:case11:printf(%d年,%d月拥有的天数是30.\n,year,month);break;default:printf(%d年,%d月拥有的天数是%d:\n,year,month,day);}}}本章实验题1#includestdio.hmain(){doublefaheight,maheight;charsex,sports,diet,F,M,Y,N;printf(请输入父母的身高:\n);scanf(%lf,%lf,&faheight,&maheight);printf(请输入孩子是男孩还是女孩(F/M):\n);scanf(%c,&sex);printf(请输入孩子是否喜欢锻炼身体(Y/N):\n);scanf(%c,&sports);printf(请输入孩子是否有良好的饮食习惯(Y/N):\n);scanf(%c,&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.\n,(faheight+maheight)*0.54*1.02);}elseif(sex==77&&sports==78&&diet==89){printf(该孩子成人时身高约为:%.2f.\n,(faheight+maheight)*0.54*1.015);}elseif(sex==77&&sports==78&&diet==78){printf(该孩子成人时身高约为:%.2f.\n,(faheight+maheight)*0.54);}elseif(sex==70&&sports==89&&diet==89){printf(该孩子成人时身高约为:%.2f.\n,(faheight*0.923+maheight)/2*1.02*1.015);}elseif(sex==70&&sports==89&&diet==78){printf(该孩子成人时身高约为:%.2f.\n,(faheight*0.923+maheight)/2*1.02);}elseif(sex==70&&sports==78&&diet==89){printf(该孩子成人时身高约为:%.2f.\n,(faheight*0.923+maheight)/2*1.015);}elseif(sex==70&&sports==78&&diet==78){printf(该孩子成人时身高约为:%.2f.\n,(faheight*0.923+maheight)/2);}}本章实验题2#includestdio.hmain(){doublet,w,h;printf(请输入体重身高:\n);scanf(%lf,%lf,&w,&h);t=w/(h*h);if(t18){printf(低体重.\n);}if(t=18&&t25){printf(正常体重.\n);}if(t=25&&t27){printf(超重体重.\n);}if(t=27){printf(肥胖.\n);}}第六章6.1(1)#includestdio.hmain(){inti,j,k;charspace='';for(i=1;i=4;i++){for(j=1;j=i;j++){printf(%c,space);}for(k=1;k=6;k++){printf(*);}printf(\n);}}6.1(2)#includestdio.hmain(){intk=4,n;for(n=0;nk;n++){if(n%2==0)continue;k--;}printf(k=%d\n,n=%d\n,k,n);}6.1(3)#includestdio.hmain(){intk=4,n;for(n=0;nk;n++){if(n%2==0)break;k--;}printf(k=%d,n=%d\n,k,n);}6.2(1)#includestdio.hmain(){inti,sum=0;for(i=1;i=101;i=i+2){sum=sum+i;}printf(sum=%d\n,sum);}6.2(2)#includestdio.hmain(){longi;longterm,sum=0;for(i=1;i=99;i=i+2){term=i*(i+1)*(i+2);sum=sum+term;}printf(sum=%ld\n,sum);}6.2(3)#includestdio.hmain(){longterm=0,sum=0;inta,i,n;printf(Inputa,n:);scanf(%d,%d,&a,&n);for(i=1;i=n;i++){term=term*10+a;sum=sum+term;}printf(sum=%ld\n,sum);}6.2(4)#includestdio.h#includemath.hmain(){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=%f\n);}6.2(5)#includestdio.h#includemath.hmain(){intn=1,count=1;floatx;doublesum,term;printf(Inputx:);scanf(%f,&x);sum=x;term=x;do{term=-term*x*x/((n+1)
本文标题:C语言程序设计苏小红版第五六单元答案
链接地址:https://www.777doc.com/doc-2908314 .html