




已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C语言程序设计 实验教学(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; printf(max is %dn,max);main()int a,b,c,max; scanf(%d,%d,%d,&a,&b,&c); if(ab) max=a; else max=b; if(cmax) max=c; printf(max is %dn,max);main()int a,b,c,max; scanf(%d,%d,%d,&a,&b,&c); if(ab) if(ac) max=a; else max=c; else if(bc) max=b; else max=c; printf(max is %dn,max);4、 编写程序,实现从键盘上输入4个整数,并按从大到小顺序输出。main()int a,b,c,d,t; scanf(%d,%d,%d,%d,&a,&b,&c,&d); if(ab) t=a;a=b;b=t; if(ac) t=a;a=c;c=t; if(ad) t=a;a=d;d=t; if(bc) t=b;b=c;c=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,&x); if(x10) y=x*x+x+1; else y=x+1; printf(x=%d,y=%dn,x,y);方法二(用if if 结构表达)#include #include main()int x,y; clrscr(); printf(Please input value of x:n); scanf(%d,&x); if(x=10) if(x0) y=1; else y=x+1; else y=x*x+x+1; printf(x=%d,y=%dn,x,y);6、 编写一个程序,判断从键盘上输入的一个年份是否为闰年?方法一:#include #include main()int year; clrscr(); printf(Please input a year:n); scanf(%d,&year); if(year%4=0 & year%100!=0 | year%400=0) printf(%d is a leap yearn,year); else printf(%d is not a leap yearn,year);方法二:(用if if 结构表达)#include #include main()int year,leap; clrscr(); printf(Please input a year:n); scanf(%d,&year); if(year%4=0) if(year%100=0) if(year%400=0) leap=1; else leap=0; else leap=1; else leap=0; if(leap=1) printf(%d is a leap yearn,year); else printf(%d is not a leap yearn,year);方法三:(用 if else if 结构表达)#include #include main()int year,leap; clrscr(); printf(Please input a year:n); scanf(%d,&year); if(year%400=0) leap=1; else if(year%100=0) leap=0; else if(year%4=0) leap=1; else leap=0; if(leap=1) printf(%d is a leap yearn,year); else printf(%d is not a leap yearn,year);7、 输入一个三位自然数num,判断其是否为水仙花数? 注:一个3位自然数,各个数位的数字立方和等于该数本身,即称为水仙花数。如: 15313+53+33 。 #include #include main()int m,a,b,c; clrscr(); printf(Please input a num(100=num=999):n); scanf(%d,&m); a=m/100; b=m/10%10; c=m%10; if(a*a*a+b*b*b+c*c*c=m) printf(%d is sxh shu.n,m); else printf(%d is not sxh shu.n,m);8、 编写一个程序,实现对给出的一百分制成绩,输出相应的成绩等级。即:90分以上为A,8089分为B,7079分为C,6069分为D,60分以下为E。方法一:(用if 结构)#include #include main()float score; char grade; clrscr(); printf(Please input a score(0=score=90) grade=A; else if(score=80) grade=B; else if(score=70) grade=C; else if(score=60) grade=D; else grade=E; printf(score is %5.1f, grade is %c.n,score,grade);方法二:(用switch 结构)#include #include main()float score; char grade; clrscr(); printf(Please input a score(0=score=100):n); scanf(%f,&score); 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(score is %5.1f, grade is %c.n,score,grade);补充作业:9、 完成下列分段函数的计算:分别使用ifelse ifelse结构和if-if嵌套结构来实现。#include #include main()float x,y; clrscr(); printf(Please input value of x:n); scanf(%f,&x); if(x=1600) y=0; else if(x=2100) y=(x-1600)*0.05; else if(x=3100) y=(x-1600)*0.1-25; else y=(x-1600)*0.15-125; printf(x=%8.2f, y=%8.2fn,x,y);10、 完成下列分段函数的计算:分别使用ifelse ifelse结构和if-if嵌套结构来实现。#include #include #include main()float x,y; clrscr(); printf(Please input value of x:n); scanf(%f,&x); if(x=0) y=(x+2)*exp(x); else y=(x+2)*log(2*x); printf(x=%8.2f, f(x)=%8.2fn,x,y); 11、完成下列分段函数的计算:分别使用ifelse ifelse结构和if-if嵌套结构来实现。#include #include #include main()float x,y; clrscr(); printf(Please input value of x:n); scanf(%f,&x); if(x=0) y=0; else y=(fabs(x)+3.2)/(sin(x)+2); printf(x=%8.2f, f(x)=%8.2fn,x,y);12、完成下列函数的计算:#include #include #include main()float x,y; clrscr(); printf(Please input value of x:n); scanf(%f,&x); 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(); printf(Please input value of x:n); scanf(%f,&x); 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 main()int c,t,d1,d2,m; clrscr(); printf(Please input c and t:n); scanf(%d,%d,&c,&t); if(c=t) d1=t;d2=c-t; if(d1=50) m=d1*80+d2*60; else m=d1*90+d2*60; else d1=c;d2=t-c; if(d1=50) m=d1*80+d2*45; else m=d1*90+d2*45; printf(c=%d, t=%d, m=%dn,c,t,m);15、某邮局对邮寄包裹有如下规定:若包裹的长宽高任一尺寸超过1米或重量超过30千克,不予邮寄;对可以邮寄的包裹每件收手续费0.2元,再加上根据下表按重量wei计算的邮资:重量(千克)收费标准(元/千克)wei100.8010wei=200.7520wei=300.70#include #include main()float c,k,g,w,m; clrscr(); printf(Please input c,k,g and w:n); scanf(%f,%f,%f,%f,&c,&k,&g,&w); if(c100|k100|g100|w30) printf(can not to be mailed); else if(w10) m=0.2+w*0.8; else if(w=20) m=0.2+w*0.75; else m=0.2+w*0.7; printf(c=%5.1f,k=%5.1f,g=%5.1f,w=%5.1f,m=%6.2fn,c,k,g,w,m); 16、请编写程序pro17.c。从键盘输入当月利润I,求应发奖金总数。根据利润进行奖金提成:利润I低于或等于10万元的,奖金可提10%;利润高于10万元,低于20万元(100000I200000)时,低于10万元部分按10%提成,高于100000元的部分,提成7.5%;200000I400000时,低于20万元部分仍按上述办法提成(下同),高于20万元的部分,按5%提成;400000I600000时,高于40万元的部分,按3%提成;600000I1000000时,高于60万元部分,按1.5%提成;I1000000时,超过100万元的部分,按1%提成;方法一:用if语句编程序#include main() long i; float 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+200000*0.03; /*利润为60万元时的奖金*/ bon10=bon6+400000*0.015; /*利润为100万元时的奖金*/ printf(Please input i:n); scanf(%ld,&i); if(i=100000) bonus=i*0.1; /*利润在10万元以内按0.1提成奖金*/ else if(i=200000) bonus=bon1+(i-100000)*0.075; /*利润在10万至20万元时的奖金*/ else if(i=400000) bonus=bon2+(i-200000)*0.05; /*利润在20万至40万元时的奖金*/ else if(i=600000) bonus=bon4+(i-400000)*0.03; /*利润在40万至60万元时的奖金*/ else if(i=1000000) bonus=bon6+(i-600000)*0.015; /*利润在60万至100万元时的奖金*/ else bonus=bon10+(i-1000000)*0.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 地下车库土地租赁及车位销售合同
- 2025公务员妆容面试题及答案
- 电子商务平台与高校人才输送合作协议范本
- 企业可持续发展合理化建议合作合同
- 军官专业面试题目及答案
- 专业心态测试题及答案
- 测序成本下降策略-洞察及研究
- 2025至2030医药级甘氨酸行业发展趋势分析与未来投资战略咨询研究报告
- 消防安全核查培训内容课件
- 消防安全月培训简讯课件
- 储能电站项目进度控制与质量管理方案
- 2025年水发集团有限公司招聘(216人)考试模拟试题及答案解析
- 3.1 生活在新型民主国家(教学课件) 2025-2026学年度道德与法治 九年级上册
- 2025年安徽省政府采购评审专家考试真题库(带答案)
- 急性白血病课件
- GB/T 46142-2025智慧城市基础设施智慧交通快速响应矩阵码应用指南
- 场景速写课件讲解
- 2025广东惠州惠城区招聘社区工作站工作人员66人笔试备考题库及答案解析
- 餐饮四个人合伙合同协议
- 人体十二经络系统解析
- 影像科培训课件
评论
0/150
提交评论