C语言程序设计 苏小红版 第五六单元 答案.docx
《C语言程序设计 苏小红版 第五六单元 答案.docx》由会员分享,可在线阅读,更多相关《C语言程序设计 苏小红版 第五六单元 答案.docx(32页珍藏版)》请在冰豆网上搜索。
![C语言程序设计 苏小红版 第五六单元 答案.docx](https://file1.bdocx.com/fileroot1/2023-2/21/044516fa-39fa-491f-8d4c-61333787b54a/044516fa-39fa-491f-8d4c-61333787b54a1.gif)
C语言程序设计苏小红版第五六单元答案
第五章
5.1
#include
voidmain()
{
doublex;
printf("请输入一个实数:
\n");
scanf("%lf",&x);
if(x>0)
{
printf("%f\n",x);
}
else
{
printf("%f\n",-x);
}
}
5.2
#include
main()
{
inta,b;
printf("请输入一个整数:
");
scanf("%d",&a);
b=a%2;
if(b==0)
{
printf("%d为偶数\n",a);
}
else
{
printf("%d为奇数\n",a);
}
}
5.3
#include
#include
main()
{
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+b>c)&&(a+c>b)&&(b+c>a))
{
printf("这个三角形的面积为%f\n",area);
}
else
{
printf("这三条边不能组成三角形\n");
}
}
5.4
#include
#include
voidmain()
{
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*c>0)
{
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*c<0)
{
printf("该方程没有实数解.\n");
}
}
5.5
#include
main()
{
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
#include
voidmain()
{
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
#include
main()
{
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
#include
main()
{
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
#include
main()
{
intscore,mark;
printf("Pleaseenterscore:
");
scanf("%d",&score);
if(score<0||score>100)
{
printf("Inputerror!
\n");
}
elseif(score>=90&&score<=100)
{
printf("%d--A\n",score);
}
elseif(score>=80&&score<90)
{
printf("%d--B\n",score);
}
elseif(score>=70&&score<80)
{
printf("%d--C\n",score);
}
elseif(score>=60&&score<70)
{
printf("%d--D\n",score);
}
else
{
printf("%d--E\n",score);
}
}
5.10
#include
#include
main()
{
intflag,year,month,day,day1=31,day2=30,day3=29,day4=28;
printf("请以2012,03这种形式输入某年某月:
");
scanf("%d,%d",&year,&month);
if(month>12||month<1)
{
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
#include
main()
{
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
#include
main()
{
doublet,w,h;
printf("请输入体重身高:
\n");
scanf("%lf,%lf",&w,&h);
t=w/(h*h);
if(t<18)
{
printf("低体重.\n");
}
if(t>=18&&t<25)
{
printf("正常体重.\n");
}
if(t>=25&&t<27)
{
printf("超重体重.\n");
}
if(t>=27)
{
printf("肥胖.\n");
}
}
第六章
6.1
(1)
#include
main()
{
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)
#include
main()
{
intk=4,n;
for(n=0;n{
if(n%2==0)continue;
k--;
}
printf("k=%d\n,n=%d\n",k,n);
}
6.1(3)
#include
main()
{
intk=4,n;
for(n=0;n{
if(n%2==0)break;
k--;
}
printf("k=%d,n=%d\n",k,n);
}
6.2
(1)
#include
main()
{
inti,sum=0;
for(i=1;i<=101;i=i+2)
{
sum=sum+i;
}
printf("sum=%d\n",sum);
}
6.2
(2)
#include
main()
{
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)
#include
main()
{
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)
#include
#include
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=%f\n");
}
6.2(5)
#include
#include
main()
{
intn=1,count=1;
floatx;
doublesum,term;
printf("Inputx:
");
scanf("%f",&x);
sum=x;
term=x;
do{
term=-term*x*x/((n+1)*(n+2));
sum=sum+term;
n=n+2;
count++;
}while(fabs(term)>=1e-5);
printf("sin(x)=%f,coumt=%d\n",sum,count);
}
6.3
#include
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;
}
x++;
}
printf("x=%d\n",x-1);
}
6.4
#include
main()
{
inti,n;
longp,j;
printf("Pleaseentern:
");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p=i*i;
j=i*i*i;
printf("%d*%d=%ld\n",i,i,p);
printf("%d*%d*%d=%ld\n",i,i,i,j);
}
}
6.5
#include
main()
{
doubleC,F,i;
for(F=-40;F<=110;F=F+10)
{
C=(F-32)*5/9;
printf("F=%fC=%f\n",F,C);
}
}
6.6
#include
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
main()
{
doublecount=0,
c,d,sum=100;
printf("Inputc:
");
scanf("%lf",&c);
d=c/100.0;
do{
sum=sum*(1+d);
count++;
}while(sum<=200);
printf("count=%f\n",count);
}
6.8
#include
#include
main()
{
intcount=1;
doublesum=1.0,term,n=3.0,i=1.0;
do{
term=-i/n;
sum=sum+term;
n=n+2;
i=-i;
count++;
}while(fabs(term)>=1e-5);
printf("∏=%lf,count=%d.\n",4*sum,count);
}
6.9
#include
#include
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("e=%lf,count=%d.\n",sum,count);
}
6.10
#include
main()
{
inti,a,b,c,d;
for(i=100;i<=999;i++)
{
a=i/100;
c=i%10;
b=(i-a*100-c)/10;
d=a*a*a+b*b*b+c*c*c;
if(i==d)
{
printf("i=%d\n",i);
}
}
}
6.11
#include
main()
{
longm=2,n,
term=1,sum=1;
printf("Inputn:
");
scanf("%ld",&n);
do{
term=term*m;
sum=sum+term;
m++;
}while(sum<=n);
printf("m=%d.\n",m-2);
}
6.12
#include
main()
{
intnum=1,sum=0,count=0;
while(num>0)
{
printf("Inputnum:
");
scanf("%d",&num);
sum=sum+num;
count++;
}
printf("sum=%d,count=%d.\n",sum,count-1);
}
6.13
#include
main()
{
intterm,sum=0,count=0;
do{
begin:
printf("Inputterm:
");
scanf("%d",&term);
if(term>0)
{
sum=sum+term;
count++;
}
elseif(term<0)
gotobegin;
else
gotoend;
}while(term!
=0);
end:
printf("sum=%d.count=%d.\n",sum,count);
}
6.14
#include
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,女=%d,孩子=%d.\n",a,b,c);
}
}
6.15
#include
main()
{
inta,b;
for(a=0;a<=98;a++)
{
b=98-a;
if(a*2+b*4==386)
{
printf("鸡有%d只,免有%d只.\n",a,b);
}
}
}
6.16
#include
main()
{
intx,y,z;
for(x=0;x<=20;x++)
{
for(y=0;y<=33;y++)
{
z=100-x-y;
if(5*x+3*y+z/3.0==100)
{
printf