何钦铭版C语言第3章答案,何钦铭版C语言第3章答案

何钦铭版C语言第3章答案

45 改变改变else 和和 if 的配对的配对 例例3-12 改写下列改写下列 if 语句,使语句,使 else 和第和第1个个 if 配对。配对。 if (x 2) if (x 1) y = x + 1; else y = x + 2; if (x 2) if (x 1) y = x + 1; else y = x + 2; if (x 2) if (x 1) y = x + 1; else; else y = x + 2; 每条语句的执行条件每条语句的执行条件? 若要改变 else 正常的配对规则,一般采用 两种方法: 采用,构造一个复合语句。 增加空的 else 语句; 46 本章总结本章总结 分支结构:分支结构: if-else语句语句 else if 分支结构:分支结构: switch语句语句 case后为常量表达式后为常量表达式 break的使用的使用 数据类型:数据类型:char型型 运算符与表达式运算符与表达式 逻辑运算符、关系运算符逻辑运算符、关系运算符 逻辑表达式逻辑表达式 分支结构程序的综合设计分支结构程序的综合设计 正确理解正确理解if语句和语句和switch语句 的执行机制; 语句 的执行机制; 掌握各类关系表达式、逻辑 表达式的运用; 掌握各类关系表达式、逻辑 表达式的运用; 能合理运用分支语句熟练编写 分支结构类的程序; 能合理运用分支语句熟练编写 分支结构类的程序; 正确理解正确理解if语句和语句和switch语句 的执行机制; 语句 的执行机制; 掌握各类关系表达式、逻辑 表达式的运用; 掌握各类关系表达式、逻辑 表达式的运用; 能合理运用分支语句熟练编写 分支结构类的程序; 能合理运用分支语句熟练编写 分支结构类的程序; 归纳总结本章的各个重要知识点。 3.3 练习与习题参考答案练习与习题参考答案 3.3.1 练习参考答案练习参考答案 练习 3-1 例 3-4 中使用 else-if 语句求解多分段函数,为了检查 else-if 语句的三个分支是否 正确,已经设计了三组测试用例,请问还需要增加测试用例吗?为什么?如果要增加,请给 出具体的测试用例并运行程序。 解答: 最好再增加两组测试用例,因为尚未对分段函数参数的边界值进行测试。可再给出 x=0 和 x=15 时的两种情况。 练习 3-2 输入一个整数 x,计算并输出下列分段函数 sign(x) 的值。 01 00 01 )( x x x xsigny , , , 解答: #include int main(void) int x, y; printf(Enter x: ); scanf(%d, if(x0) y=1; else if (x=0) y=0; else y=-1; printf(sign( %d ) = %dn,x,y); return 0; 练习 3-3 输入一个正整数 n,再输入 n 个学生的成绩,计算平均成绩,并统计所有及格学生 的人数。 解答: # include int main(void) int count, i, n; double grade, total; printf(Enter n: ); scanf(%d, total = 0; count = 0; for(i = 1; i = 60) count+; printf(Grade average = %.2fn, total / n); printf(Number of pass = %dn, count); return 0; 练习 3-4 输入 15 个字符,统计其中英文字母、空格或回车、数字字符和其他字符的个数。 解答: #include int main(void) int i; int blank, digit, letter, other; char c; blank = digit = letter = other = 0; for(i=1;i=a else if(c= |c=n) blank+; else other+; printf(letter=%d,blank=%d,digit=%d,other=%dn, letter, blank, digit, other); return 0; 练习 3-5 输入一个年份 year,判断该年是否为闰年。判断闰年的条件是:能被 4 整除但 不能被 100 整除,或者能被 400 整除。 解答: # include int main(void) int year; printf(Enter year: ); scanf(%d, if( (year % 4 = 0 else printf(It is not a leap yearn ); return 0; 练习 3-6 在例 3-8 程序中,如果把 switch 语句中所有的 break 都去掉,运行结果会改变 吗?如果有变化,输出什么?为什么? 解答: 如果去掉所有的 break 语句,运行结果会改变,输出 price = 0.0 ,因为不管 choice 值与 其中某个常量表达式相等,当去掉 break 语句时,其后的所有语句段都将运行,故每次都将 执行到 price=0.0 语句为止。 练习 3-7 输入五级制成绩 (AE) , 输出相应的百分制成绩 (0100) 区间, 要求使用 switch 语句。例如,输入 A,输出 90100。五级制成绩对应的百分制成绩区间为:A(90-100) 、 B(80-89) 、C(70-79) 、D(60-69)和 E(0-59) 。 解答: #include int main(void) char ch; printf(Input Grade: ); ch = getchar(); switch(ch) case A: printf(%c 对应的百分制区间是 90-100n,ch); break; case B: printf(%c 对应的百分制区间是 80-89n,ch); break; case C: printf(%c 对应的百分制区间是 70-79n,ch); break; case D: printf(%c 对应的百分制区间是 60-69n,ch); break; case E: printf(%c 对应的百分制区间是 0-59n,ch); break; default: printf(Invalid inputn); return 0; 练习 3-8 查询水果的单价。有 4 种水果,苹果(apples) 、梨(pears) 、桔子(oranges)和葡 萄(grapes) ,单价分别是 3.00 元/公斤,2.50 元/公斤,4.10 元/公斤和 10.20 元/公斤。在屏 幕上显示以下菜单(编号和选项) ,用户可以连续查询水果的单价,当查询次数超过 5 次时, 自动退出查询;不到 5 次时,用户可以选择退出。当用户输入编号 14,显示相应水果的 单价(保留两位小数) ;输入 0,退出查询;输入其他编号,显示价格为 0。 1 apples 2 pears 3 oranges 4 grapes 0 Exit 解答: #include int main(void) int ri; int choice; float price; for(ri=1; ri=5; ri+) printf(1 applesn); printf(2 pearsn); printf(3 orangesn); printf(4 grapesn); printf(0 Exitn); scanf(%d, if(choice=0) break; else switch(choice) case 1: price=3.00; break; case 2: price=2.50; break; case 3: price=4.10; break; case 4: price=10.20; break; default: price=0; printf(price=%0.2fn, price); return 0; 练习 3-9 请读者重新编写例 3-4 的程序,要求使用嵌套的 if - else 语句,并上机运行。 解答: #include int main(void) double x, y; scanf(%lf, if (x 15) y = 2.5 * x - 10.5; else if(x0) y=0; else y=4*x/3; printf(f(%.2f)=%.2fn, x, y); return 0; 练习 3-10 在例 3-12 中, 改写 if 语句前, y= x + 1;和 y= x + 2; 两条语句的执行条件是什么? 改写后呢? 解答: 改写前:y=x+1 的执行条件是 x1; y=x+2 的执行条件是 1=x2。 改写后:y=x+1 的执行条件是 x1; y=x+2 的执行条件是 2= 10 or a = 0 | a = 10 By=0; else if(x = 0) y = 0; if(x 0) y = 1; else y = -1; else if(x = 0) if(x = 0); if(x 0) y = 1; if(x 0) y = 1 ; else y = 0; else y = -1; else y = -1; 5下列程序段的输出结果是 C 。 int main(void) int a = 2, b = -1,c = 2; if(a b) if(b 0) s = s + 1; if(a b) t = s + t; else if(a = b) t = 5; else t = 2 * s; printf(t=%dn,t); return 0; Aab Bab0 C0aab 二填空题 1表示条件:10x100 或者 x10 scanf(%d, if(a 50) printf(%d,a); if(a 40) printf(%d,a); if(a 30) printf(%d,a); return 0; 5下列程序运行的输出结果是 9 。 int main(void) char c = b; int k = 4; switch(c) case a: k = k + 1;break; case b: k = k + 2; case c: k = k + 3; printf(%dn,k); return 0; 三程序设计题 1输入三角形的 3 条边 a, b, c,如果能构成一个三角形,输出面积 area 和周长 perimeter(保 留 2 位小数);否则,输出These sides do not correspond to a valid triangle。 在一个三角形中,任意两边之和大于第三边。三角形面积计算公式: )()(csbsassarea ,其中 s = (a+b+c)/2 解答: #include #include int main(void) int a,b,c; double area,s,perimeter; scanf(%d%d%d, if(a+bc) s=(a+b+c)*1.0/2; area=sqrt(s*(s-a)*(s-b)*(s-c); printf(area=%.2f,perimeter=%.2fn,area,perimeter); else printf(These sides do not correspond to a valid trianglen); return 0; 22011 年开始实行新个人所得税法,要求输入月薪 salary,输出应交的个人所得税 tax。新 税法方案如下: tax = rate * (salary-3500)- deduction 当 salary 3500 时,rate = 0、deduction = 0 当 3500 salary 5000 时,rate = 3%、deduction = 0 当 5000 salary 8000 时,rate = 10%、deduction = 105 当 8000 salary 12500 时,rate = 20%、deduction = 555 当 12500 salary 38500 时,rate = 25%、deduction = 1005 当 38500 salary 58500 时,rate = 30%、deduction = 2755 当 58500 salary 83500 时,rate = 35%、deduction = 5505 当 83500 salary 时,rate = 45%、deduction = 13505 解答: #include int main(void) int decution; double rate,salary,tax; printf(Enter the salary: ); scanf(%lf, if(salary=3500) rate=0;decution=0; else if(salary=5000) rate=0.03;decution=0; else if(salary=8000) rate=0.1;decution=105; else if(salary=12500) rate=0.2;decution=555; else if(salary=38500) rate=0.25;decution=1005; else if(salary=58500) rate=0.3;decution=2755; else if(salary=83500) rate=0.35;decution=5505; else rate=0.45;decution=13505; tax=rate*(salary-3500)-decution; printf(tax = %.0fn,tax); return 0; 3某城市普通出租车收费标准如下: “起步里程 3 公里,起步费 10 元;超起步里程后 10 公里内,每公里租费 2 元,超过 10 公里以上的部分加收 50%的回空补贴费,即每公里租费 3 元。营运过程中,因路阻及乘客要求临时停车的,每 5 分钟按 1 公里租费计收。运价计费 尾数四舍五入,保留到元。 ” 。编写程序,输入行驶里程(公里)与等待时间(分钟),计算并输 出乘客应支付的车费(元)。 解答: #include int main(void) int mile,time,cost; scanf(%d%d, cost=0; mile=mile+time/5; if(mile=3) cost=10; else if(mile=13) cost=10+(mile-3)*2; else cost=10+10*2+(mile-13)*3; printf(cost=%dn,cost); return 0; 4输入一个正整数 n,再输入 n 个学生的成绩,计算平均分,并统计各等级成绩的个数。 成绩分为 5 个等级,分别为 A(90100)、B(8089)、C(7079)、D(6069)、E(059)。 #include int main(void) int mark, n, i, sum; double average; int counta, countb, countc, countd, counte; printf(Enter n: ); scanf(%d, counta= countb = countc = countd = counte =sum=0; for(i=1; i=90) counta+; else if (mark=80) countb+; else if(mark=70) countc+; else if(mark=60) countd+; else counte+; average=sum*1.0/n; printf(average=%.1fn,average); printf(A: %d, B: %d, C: %d, D: %d, E: %dn,counta, countb, countc, countd, counte); return 0; 5输出 21 世纪所有的闰年。判断闰年的条件是:能被 4 整除但不能被 100 整除,或者能 被 400 整除。 解答: #include int main(void) int year; for(year=2000; year=2099; year+) if( (year%4=0 return 0; 3.4 实验指导教材参考答案实验指导教材参考答案 一、调试示例:略 二、改错题 改正下列程序中的错误,输入三角形的 3 条边 a, b, c,如果能构成一个三角形,输出 面积 area 和周长 perimeter(保留 2 位小数) ;否则,输出“These sides do not correspond to a valid triangle” 。 (源程序 error03_2.cpp) 在一个三角形中,任意两边之和大于第 3 边。三角形面积计算公式: area=)()(csbsass 其中 s = (a+b+c)/2 输入输出示例(运行 2 次) 第一次运行: Enter 3 sides of the triangle: 5 5 3 area = 7.15; perimeter = 13.00 第二次运行: Enter 3 sides of the triangle: 1 4 1 These sides do not correspond to a valid triangle 源程序(有错误的程序) 1 #include 2 #include 3 int main(void) 4 5 double a,b,c; 6 double area,perimeter,s; 7 8 printf(Enter 3 sides of the triangle: ); 9 scanf( %lf%lf%lf, 10 11 if( a + b c | b + c a | a + c b) 12 s = ( a + b + c ) / 2; 13 area = sqrt( s * ( s - a ) * ( s - b ) * ( s - c ); 14 perimeter = a + b + c; 15 printf(area = %.2f; perimeter = %.2fn,area,perimeter); 16 17 else 18 printf(These sides do not correspond to a valid trianglen); 19 20 return 0; 21 改错汇总:改错汇总: 错误行号: 11 正确语句: if( a + b c 错误行号: 16 正确语句: 三、编程题 1输入 x,计算并输出下列分段函数 sign(x) 的值。 10 ( )00 10 x ysign xx x 输入输出示例(运行 3 次) 第一次运行: Enter x: 10 sign( 10 ) = 1 第二次运行: Enter x: 0 sign( 0 ) = 0 第三次运行: Enter x: -98 sign( -98 ) = -1 解答:参见 3.3.1 节中的练习 3-2 2输入一个整数 x,判断 x 能否被 3、5、7 整除,并输出以下信息之一: (1)能同时被 3、5、7 整数; (2)能被其中两数整除; (3)能被其中一个数整除; (4)不能被 3、5、7 任一个数整除; 输入输出示例(运行 4 次) 第一次运行: Enter x: 15 能被其中两数整除 第二次运行: Enter x: 14 能被其中一个数整除 第三次运行: Enter x: 105 能同时被 3、5、7 整数 第四次运行: Enter x: 17 不能被 3、5、7 任一个数整除 解答: #include int main(void) int x; printf(Enter x: ); scanf(%d, if(x%3=0 else if (x%3=0 else if(x%3=0|x%5=0|x%7=0); printf(能被其中一个数整除n); else printf(不能被 3、5、7 任一个数整除n); return 0; 3输入五级制成绩(A E) ,输出相应的百分制成绩(0 100)区间,要求使用 switch 语 句。五级制成绩对应的百分制成绩区间为:A(90 100) 、B(80 89) 、C(70 79) 、D(60 69)和 E(0 59) 。 输入输出示例 Input Grade: B B 对应的百分制成绩区间是 80 89 提示:程序应运行 6 次,每次测试一种情况,即分别输入 A、B、C、D、E 和其他字符。 解答:参见 3.3.1 节中的练习 3-7 4查询水果的单价。有 4 种水果,苹果(apple) 、梨(pear) 、橘子(orange)和葡萄(grape) , 单价分别是 3.00 元/千克,2.50 元/千克,4.10 元/千克和 10.20 元/千克。在屏幕上显示以下 菜单(编号和选项) ,用户可以连续查询水果的单价,当查询次数超过 5 次时,自动退出查 询;不到 5 次时,用户可以选择退出。当用户输入编号 1 4,显示相应水果的单价(保留 1 位小数) ;输入 0,退出查询;输入 0 4 之外的其他编号,显示价格为 0。 1 apple 2 pear 3 orange 4 grape 0 exit 解答:参见 3.3.1 节中的练习 3-8 52011 年开始实行新个人所得税法,要求输入月薪 salary,输出应交的个人所得税 tax。新 税法方案如下: tax = rate * (salary-3500)- deduction 当 salary 3500 时,rate = 0、deduction = 0 当 3500 salary 5000 时,rate = 3%、deduction = 0 当 5000 salary 8000 时,rate = 10%、deduction = 105 当 8000 salary 12500 时,rate = 20%、deduction = 555 当 12500 salary 38500 时,rate = 25%、deduction = 1005 当 38500 salary 58500 时,rate = 30%、deduction = 2755 当 58500 salary 83500 时,rate = 35%、deduction = 5505 当 83500 salary 时,rate = 45%、deduction = 13505 输入输出示例(运行 5 次) 第一次运行: Enter the salary: 3050.5 tax = 0 第二次运行: Enter the salary: 4238.9 tax = 22 第三次运行: Enter the salary: 7328.6 tax = 278 第四次运行: Enter the salary: 52547 tax = 11959 第五次运行: Enter the salary: 89000 tax = 24970 解答:参见 3.3.2 节中程序设计题第 2 题 6输入一个正整数 n,再输入 n 个学生的百分制成绩,统计各等级成绩的个数。成绩等级 分为五级,分别为 A(90 100) 、B(80 89) 、C(70 79) 、D(60 69)和 E(0 59) 。 输入输出示例 Enter n: 5 Enter grade 1: 77 Enter grade 2: 54 Enter grade 3: 92 Enter grade 4: 73 Enter grade 5: 60 The number of A(90 100):1 The number of B(80 89):0 The number of C(70 79):2 The number of D(60 69):1 The number of E(0 59):1 解答:参见 3.3.2 程序设计题第 4 题 7 (选作)油价居高不下对有车族来说真是个大负担!现在 90 号汽油 6.95 元/升、93 号汽油 7.44 元/升、97 号汽油 7.93 元/升。为吸引顾客,某自动加油站推出了“自助服务” 和“协助服务”两个服务等级,分别可得到 5%和 3%的折扣。请编写程序,输入顾客的加油 量 a,汽油品种 b(90、93 或 97)和服务类型 c(m自助,e 协助) ,计算并输出应付 款(保留小数点后 2 位) 。 解答: int main(void) int a,b; char c ; double price,money ; printf(输入加油量: ); scanf(%d, printf(输入加油品种: ); scanf(%d, getchar(); printf(输入服务方式: ); c=getchar(); if(b=90) price=6.95; else if(b=93) price=7.44; else price=7.93; if(c=m) money=a*price*0.95; else money=a*price*0.97; printf(应付款: %.2fn,money); return 0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值