实验(4)-选择结构-参考答案

上传人:mg****85 文档编号:34052924 上传时间:2018-02-20 格式:DOC 页数:15 大小:99.50KB
返回 下载 相关 举报
实验(4)-选择结构-参考答案_第1页
第1页 / 共15页
实验(4)-选择结构-参考答案_第2页
第2页 / 共15页
实验(4)-选择结构-参考答案_第3页
第3页 / 共15页
实验(4)-选择结构-参考答案_第4页
第4页 / 共15页
实验(4)-选择结构-参考答案_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《实验(4)-选择结构-参考答案》由会员分享,可在线阅读,更多相关《实验(4)-选择结构-参考答案(15页珍藏版)》请在金锄头文库上搜索。

1、1C 语言程序设计 实验教学(4)【实验目的】通过程序设计实现,掌握 if 结构和 switch 结构的正确应用。【实验要求】使用 if 结构和 switch 结构实现多种条件的选择结构程序,熟练二者的应用范围和注意事项。练习条件表达式的书写。【实验课时】4.0【实验内容】1、 运行下列程序,分析结果。main( ) int a=1,b=2,m=0,n=0,k;k=(n=ba)|(m=ab?a:b;max=maxc?max:c;2printf(max is %dn,max);main()int a,b,c,max;scanf(%d,%d,%d,if(ab) max=a;else max=b;i

2、f(cmax) max=c;printf(max is %dn,max);main()int a,b,c,max;scanf(%d,%d,%d,if(ab) if(ac) max=a;else max=c;else if(bc)max=b;elsemax=c;printf(max is %dn,max);4、 编写程序,实现从键盘上输入 4 个整数,并按从大到小顺序输出。main()int a,b,c,d,t;scanf(%d,%d,%d,%d,if(ab) t=a;a=b;b=t;3if(ac) t=a;a=c;c=t;if(ad) t=a;a=d;d=t;if(bc) t=b;b=c;c=

3、t;if(bd) t=b;b=d;d=t;if(cd) t=c;c=d;d=t;printf(sorted by small to large: %d,%d,%d,%dn,a,b,c,d);5、 完成下列分段函数的计算:分别使用 ifelse if 和 if-if 的嵌套结构来实现。方法一(用 if else if 结构表达)#include #include main()int x,y;clrscr();printf(Please input value of x:n);scanf(%d,if(x10)y=x*x+x+1;elsey=x+1;printf(x=%d,y=%dn,x,y);方法

4、二(用 if if 结构表达)y 0=24#include #include main()int x,y;clrscr();printf(Please input value of x:n);scanf(%d,if(x#include main()int year;clrscr();printf(Please input a year:n);scanf(%d,if(year%4=0 & year%100!=0 | year%400=0)printf(%d is a leap yearn,year);elseprintf(%d is not a leap yearn,year);方法二:(用 i

5、f if 结构表达)5#include #include main()int year,leap;clrscr();printf(Please input a year:n);scanf(%d,if(year%4=0)if(year%100=0)if(year%400=0)leap=1;elseleap=0;elseleap=1;elseleap=0;if(leap=1) printf(%d is a leap yearn,year);elseprintf(%d is not a leap yearn,year);方法三:(用 if else if 结构表达)#include #include

6、 main()int year,leap;clrscr();printf(Please input a year:n);scanf(%d,if(year%400=0)6leap=1;elseif(year%100=0)leap=0;elseif(year%4=0)leap=1;elseleap=0;if(leap=1) printf(%d is a leap yearn,year);elseprintf(%d is not a leap yearn,year);7、 输入一个三位自然数 num,判断其是否为水仙花数?注:一个 3 位自然数,各个数位的数字立方和等于该数本身,即称为水仙花数。如:

7、 1531 3+53+33 。 #include #include main()int m,a,b,c;clrscr();printf(Please input a num(100#include main()float score; char grade;clrscr();printf(Please input a score(0=90)grade=A;elseif(score=80)grade=B;elseif(score=70)grade=C;elseif(score=60)grade=D;elsegrade=E;printf(score is %5.1f, grade is %c.n,

8、score,grade);方法二:(用 switch 结构)#include #include main()float score; char grade;clrscr();8printf(Please input a score(0#include main()float x,y;clrscr();9printf(Please input value of x:n);scanf(%f,if(x#include #include main()float x,y;clrscr();printf(Please input value of x:n);scanf(%f,if(x#include #i

9、nclude main()float x,y;clrscr();printf(Please input value of x:n);scanf(%f,if(x#include #include 11main()float x,y;clrscr();printf(Please input value of x:n);scanf(%f,y=(exp(x)+fabs(x-6)/(x+1.3);printf(x=%8.2f, fun1(x)=%8.2fn,x,y);13、完成下列函数的计算:#include #include #include main()float x,y;clrscr();prin

10、tf(Please input value of x:n);scanf(%f,y=(1+sin(x)+exp(x)/(x+1);printf(x=%8.2f, fun1(x)=%8.2fn,x,y);14、某服装店经营套服,也单件出售。若买的不少于 50 套,每套 80 元;不足 50 套的每套 90 元;只买上衣每件 60 元;只买裤子每条 45。以下程序的功能是读入所买上衣 c 和裤子 t 的件数,计算应付款 m。#include #include 12main()int c,t,d1,d2,m;clrscr();printf(Please input c and t:n);scanf(%

11、d,%d,if(c=t)d1=t;d2=c-t;if(d1=50)m=d1*80+d2*60;else m=d1*90+d2*60;elsed1=c;d2=t-c;if(d1=50)m=d1*80+d2*45;elsem=d1*90+d2*45;printf(c=%d, t=%d, m=%dn,c,t,m);15、某邮局对邮寄包裹有如下规定:若包裹的长宽高任一尺寸超过 1 米或重量超过 30 千克,不予邮寄;对可以邮寄的包裹每件收手续费 0.2 元,再加上根据下表按重量 wei 计算的邮资:重量(千克) 收费标准(元/千克)wei#include main()13float c,k,g,w,m

12、;clrscr();printf(Please input c,k,g and w:n);scanf(%f,%f,%f,%f,if(c100|k100|g100|w30)printf(can not to be mailed);elseif(wmain() long i;14float bonus,bon1,bon2,bon4,bon6,bon10;bon1=100000*0.1; /*利润为 10 万元时的奖金*/bon2=bon1+100000*0.075; /*利润为 20 万元时的奖金*/bon4=bon2+200000*0.05; /*利润为 40 万元时的奖金*/bon6=bon4

13、+200000*0.03; /*利润为 60 万元时的奖金*/bon10=bon6+400000*0.015; /*利润为 100 万元时的奖金*/printf(Please input i:n);scanf(%ld,if(imain() long i;float bonus, bon1, bon2, bon4, bon6, bon10;int c;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(Please inp

14、ut i:n);scanf(%ld,c=i/100000;if(c10) c=10;15switch(c) 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(bonus is:%10.2f,bonus);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号