C程序设计第四版谭浩强课后习题答案共11章xin 自动保存的Word格式.docx
《C程序设计第四版谭浩强课后习题答案共11章xin 自动保存的Word格式.docx》由会员分享,可在线阅读,更多相关《C程序设计第四版谭浩强课后习题答案共11章xin 自动保存的Word格式.docx(143页珍藏版)》请在冰豆网上搜索。
p=%f\n"
p);
3-2-1
{floatr5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;
p=1000;
r5=0.0585;
r3=0.054;
r2=0.0468;
r1=0.0414;
r0=0.0072;
p1=p*((1+r5)*5);
//一次存5年期
p2=p*(1+2*r2)*(1+3*r3);
//先存2年期,到期后将本息再存3年期
p3=p*(1+3*r3)*(1+2*r2);
//先存3年期,到期后将本息再存2年期
p4=p*pow(1+r1,5);
//存1年期,到期后将本息存再存1年期,连续存5次
p5=p*pow(1+r0/4,4*5);
//存活期存款。
活期利息每一季度结算一次
p1=%f\n"
p1);
//输出按第1方案得到的本息和
p2=%f\n"
p2);
//输出按第2方案得到的本息和
p3=%f\n"
p3);
//输出按第3方案得到的本息和
p4=%f\n"
p4);
//输出按第4方案得到的本息和
p5=%f\n"
p5);
//输出按第5方案得到的本息和
3-2-2
{doubler5,r3,r2,r1,r0,p,p1,p2,p3,p4,p5;
3-2-3
p1=%10.2f\n"
p2=%10.2f\n"
p3=%10.2f\n"
p4=%10.2f\n"
p5=%10.2f\n"
3-3.
{floatd=300000,p=6000,r=0.01,m;
m=log10(p/(p-d*r))/log10(1+r);
m=%6.2f\n"
m);
3-4
{intc1,c2;
c1=197;
c2=198;
c1=%c,c2=%c\n"
c1,c2);
c1=%d,c2=%d\n"
3-5
{inta,b;
floatx,y;
charc1,c2;
a=%db=%d"
b);
%f%e"
x,&
y);
%c%c"
c1,&
c2);
a=%d,b=%d,x=%f,y=%f,c1=%c,c2=%c\n"
a,b,x,y,c1,c2);
3-6
{charc1='
C'
c2='
h'
c3='
i'
c4='
n'
c5='
a'
;
c1=c1+4;
c2=c2+4;
c3=c3+4;
c4=c4+4;
c5=c5+4;
passworis%c%c%c%c%c\n"
c1,c2,c3,c4,c5);
3-7
intmain()
{floath,r,l,s,sq,vq,vz;
floatpi=3.141526;
请输入圆半径r,圆柱高h∶"
%f,%f"
r,&
h);
//要求输入圆半径r和圆柱高h
l=2*pi*r;
//计算圆周长l
s=r*r*pi;
//计算圆面积s
sq=4*pi*r*r;
//计算圆球表面积sq
vq=3.0/4.0*pi*r*r*r;
//计算圆球体积vq
vz=pi*r*r*h;
//计算圆柱体积vz
圆周长为:
l=%6.2f\n"
l);
圆面积为:
s=%6.2f\n"
s);
圆球表面积为:
sq=%6.2f\n"
sq);
圆球体积为:
v=%6.2f\n"
vq);
圆柱体积为:
vz=%6.2f\n"
vz);
}
3-8-1
{
intc1,c2;
//整型定义
请输入两个整数c1,c2:
"
%d,%d"
按字符输出结果:
%c,%c\n"
按ASCII码输出结果为:
%d,%d\n"
3-8-2
//定义字符型变量
inti1,i2;
//定义整型变量
请输入两个字符c1,c2:
%c,%c"
i1=c1;
//赋值给整型变量
i2=c2;
i1,i2);
按整数输出结果:
3-8-3
//定义为字符型
//定义为整型
请输入两个整数i1,i2:
i1,&
i2);
c1=i1;
//将整数赋值给字符变量
c2=i2;
3-8
charc1,c2;
printf("
c1=getchar();
c2=getchar();
用putchar语句输出结果为:
putchar(c1);
putchar(c2);
用printf语句输出结果为:
%c%c\n"
return0;
习题111
4-4-1
inta,b,c;
请输入三个整数:
if(a<
if(b<
max=%d\n"
c);
else
b);
elseif(a<
a);
4-4-2
{inta,b,c,temp,max;
temp=(a>
b)?
a:
b;
/*将a和b中的大者存入temp中*/
max=(temp>
c)?
temp:
c;
/*将a和b中的大者与c比较,取最大者*/
三个整数的最大数是%d\n"
4-5-2
#defineM1000
inti,k;
请输入一个小于%d的整数i:
M);
%d"
i);
while(i>
M)
{printf("
输入的数不符合要求,请重新输入一个小于%d的整数i:
k=sqrt(i);
%d的平方根的整数部分是:
%d\n"
i,k);
4-5
if(i>
4-6.
{intx,y;
输入x:
x);
if(x<
1)/*x<
1*/
{y=x;
x=%3d,y=x=%d\n"
x,y);
elseif(x<
10)/*1=<
x<
10*/
{y=2*x-1;
x=%d,y=2*x-1=%d\n"
x,y);
else/*x>
=10*/
{y=3*x-11;
x=%d,y=3*x-11=%d\n"
4-7-1
intx,y;
enterx:
y=-1;
if(x!
=0)
if(x>
0)
y=1;
y=0;
x=%d,y=%d\n"
4-7-2
pleaseenterx:
0)y=1;
elsey=-1;
4-8
{floatscore;
chargrade;
请输入学生成绩:
%f"
score);
while(score>
100||score<
\n输入有误,请重输"
switch((int)(score/10))
{case10:
case9:
grade='
A'
break;
case8:
B'
case7:
case6:
D'
case5:
case4:
case3:
case2:
case1:
case0:
E'
成绩是%5.1f,相应的等级是%c\n"
score,grade);
4-9
intnum,indiv,ten,hundred,thousand,ten_thousand,place;
//分别代表个位,十位,百位,千位,万位和位数
请输入一个整数(0-99999):
num);
if(num>
9999)
place=5;
elseif(num>
999)
place=4;
99)
place=3;
9)
place=2;
elseplace=1;
位数:
place);
每位数字为:
ten_thousand=num/10000;
thousand=(int)(num-ten_thousand*10000)/1000;
hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;
ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;
indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);
switch(place)
{case5:
%d,%d,%d,%d,%d"
ten_thousand,thousand,hundred,ten,indiv);
\n反序数字为:
%d%d%d%d%d\n"
indiv,ten,hundred,thousand,ten_thousand);
break;
%d,%d,%d,%d"
thousand,hundred,ten,indiv);
%d%d%d%d\n"
indiv,ten,hundred,thousand);
hundred,ten,indiv);
%d%d%d\n"
indiv,ten,hundred);
ten,indiv);
%d%d\n"
indiv,ten);
indiv);
4-10-1
inti;
doublebonus,bon1,bon2,bon4,bon6,bon10;
bon1=100000*0.1;
bon2=bon1+100000*0.075;
bon4=bon2+100000*0.05;
bon6=bon4+100000*0.03;
bon10=bon6+400000*0.015;
请输入利润i:
if(i<
=100000)
bonus=i*0.1;
elseif(i<
=200000)
bonus=bon1+(i-100000)*0.075;
=400000)
bonus=bon2+(i-200000)*0.05;
=600000)
bonus=bon4+(i-400000)*0.03;
=1000000)
bonus=bon6+(i-600000)*0.015;
bonus=bon10+(i-1000000)*0.01;
奖金是:
%10.2f\n"
bonus);
4-10-2
intbranch;
bon4=bon2+200000*0.05;
bon6=bon4+200000*0.03;
branch=i/100000;
if(branch>
10)branch=10;
switch(branch)
{case0:
bonus=i*0.1;
bonus=bon1+(i-100000)*0.075;
case10:
奖金是%10.2f\n"
4-11
{intt,a,b,c,d;
请输入四个数:
c,&
d);
a=%d,b=%d,c=%d,d=%d\n"
a,b,c,d);
if(a>
{t=a;
a=b;
b=t;
a=c;
c=t;
d)
a=d;
d=t;
if(b>
{t=b;
b=c;
b=d;
if(c>
{t=c;
c=d;
排序结果如下:
\n"
%d%d%d%d\n"
a,b,c,d);
_
4-12
{
in