好文档 - 专业文书写作范文服务资料分享网站

(完整word版)《C语言程序设计》课后习题答案(第四版)谭浩强

天下 分享 时间: 加入收藏 我要投稿 点赞

4-8

#include int main()

{ float score; char grade;

printf(\请输入学生成绩:\ scanf(\

while (score>100||score<0) {printf(\输入有误,请重输\ scanf(\ }

switch((int)(score/10)) {case 10: case 9: grade='A';break; case 8: grade='B';break; case 7: grade='C';break; case 6: grade='D';break; case 5: case 4: case 3: case 2: case 1: case 0: grade='E'; }

printf(\成绩是 %5.1f,相应的等级是%c\\n \ return 0; } 4-9

#include #include int main() {

int num,indiv,ten,hundred,thousand,ten_thousand,place; 位,万位和位数

printf(\请输入一个整数(0-99999):\ scanf(\ if (num>9999) place=5; else if (num>999) place=4; else if (num>99)

//分别代表个位,十位,百位,千 place=3; else if (num>9) place=2; else place=1;

printf(\位数:%d\\n\ printf(\每位数字为:\ 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)

{case 5:printf(\ printf(\反序数字为:\ printf(\ break;

case 4:printf(\ printf(\反序数字为:\ printf(\ break;

case 3:printf(\ printf(\反序数字为:\ printf(\ break;

case 2:printf(\ printf(\反序数字为:\ printf(\ break;

case 1:printf(\ printf(\反序数字为:\ printf(\ break; } return 0; }

4-10-1

#include int main() {

int i;

double bonus,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; printf(\请输入利润i:\ scanf(\ if (i<=100000) bonus=i*0.1; else if (i<=200000)

bonus=bon1+(i-100000)*0.075; else if (i<=400000)

bonus=bon2+(i-200000)*0.05; else if (i<=600000)

bonus=bon4+(i-400000)*0.03; else if (i<=1000000)

bonus=bon6+(i-600000)*0.015; else

bonus=bon10+(i-1000000)*0.01; printf(\奖金是: .2f\\n\ return 0; }

4-10-2

#include int main() {

int i;

double bonus,bon1,bon2,bon4,bon6,bon10; int branch;

bon1=100000*0.1;

bon2=bon1+100000*0.075; bon4=bon2+200000*0.05; bon6=bon4+200000*0.03; bon10=bon6+400000*0.015; printf(\请输入利润i:\ scanf(\ branch=i/100000;

if (branch>10) branch=10; switch(branch)

{ case 0:bonus=i*0.1;break;

case 1:bonus=bon1+(i-100000)*0.075;break; case 2:

case 3: bonus=bon2+(i-200000)*0.05;break;

case 4:

case 5: bonus=bon4+(i-400000)*0.03;break; case 6: case 7: case 8:

case 9: bonus=bon6+(i-600000)*0.015;break; case 10: bonus=bon10+(i-1000000)*0.01; }

printf(\奖金是 .2f\\n\ return 0; } 4-11

#include int main()

{int t,a,b,c,d;

printf(\请输入四个数:\

scanf(\ printf(\ if (a>b)

{ t=a;a=b;b=t;} if (a>c)

{ t=a;a=c;c=t;} if (a>d)

{ t=a;a=d;d=t;} if (b>c)

{ t=b;b=c;c=t;} if (b>d)

{ t=b;b=d;d=t;} if (c>d)

{ t=c;c=d;d=t;}

printf(\排序结果如下: \\n\

printf(\ %d %d %d \\n\ ,a,b,c,d); return 0; } 4-12

#include int main() {

int h=10;

float x1=2,y1=2,x2=-2,y2=2,x3=-2,y3=-2,x4=2,y4=-2,x,y,d1,d2,d3,d4;

printf(\请输入一个点(x,y):\ scanf(\

d1=(x-x4)*(x-x4)+(y-y4)*(y-y4); /*求该点到各中心点距离*/ d2=(x-x1)*(x-x1)+(y-y1)*(y-y1); d3=(x-x2)*(x-x2)+(y-y2)*(y-y2); d4=(x-x3)*(x-x3)+(y-y3)*(y-y3);

if (d1>1 && d2>1 && d3>1 && d4>1) h=0; /*判断该点是否在塔外*/ printf(\该点高度为 %d\\n\ return 0; }

第5章循环结构程序设计114 5.1为什么需要循环控制114 5.2用while语句实现循环115 5.3用do…while语句实现循环117 5.4用for 语句实现循环120 5.5循环的嵌套124 5.6几种循环的比较125 5.7改变循环执行的状态125

5.7.1用break语句提前终止循环126

5.7.2用continue语句提前结束本次循环127 5.7.3break语句和continue语句的区别128 5.8循环程序举例131 习题140 5-2

#include

#include // 程序中用到数学函数fabs,应包含头文件math.n int main() {

int sign=1,count=0; // sign用来表示数值的符号,count用来统计循环次数 double pi=0.0,n=1.0,term=1.0; // pi开始代表多项式的值,最后代表π的值, n代表分母,term代表当前项的值

while(fabs(term)>=1e-8) // 检查当前项term的绝对值是否大于或等于10的(-6)次方 {

pi=pi+term; // 把当前项term累加到pi中 n=n+2; // n+2是下一项的分母

sign=-sign; // sign代表符号,下一项的符号与上一项符号相反 term=sign/n; // 求出下一项的值term count++; // count累加1 }

pi=pi*4; // 多项式的和pi乘以4,才是π的近似值 printf(\ // 输出π的近似值 printf(\ // 输出循环次数

(完整word版)《C语言程序设计》课后习题答案(第四版)谭浩强

4-8#includeintmain(){floatscore;chargrade;printf(\请输入学生成绩:\scanf(\while(score>100||score<0){printf(\输入有误,请重输\scanf(\}
推荐度:
点击下载文档文档为doc格式
5ibh21ih7y8mpoj7ocb09o8y29wt5t00z3w
领取福利

微信扫码领取福利

微信扫码分享