版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、。实验三选择结构程序设计一、实验目的1、 掌握建立和执行M文件的方法。2、 掌握利用if语句实现选择结构的方法。3、 掌握利用switch语句实现多分支选择结构的方法。4、 掌握 try语句的使用。二、实验内容1、 求分段函数的值。用if语句实现,分别输出x=-5.0,3.0,1.0,2.0,2.5,3.0,5.0时的 y值。 x=input('please input the value of x'); if x<0&x=-3y=x*x+x-6;elseif x>=0&x<5&x=2&x=3 y=x*x-5*x+6;else
2、y=x*x-x-1;endy please input the value of x-5.0y =14>> aaaaaplease input the value of x-3.0y =。1。11>> aaaaaplease input the value of x1.0y =2>> aaaaaplease input the value of x2.0y =1>> aaaaaplease input the value of x2.5y =-0.2500>> aaaaaplease input the value of x3.0y
3、=5。2。>> aaaaaplease input the value of x5.0y =192、 输入一个百分制成绩,要求输出成绩等级A、 B、C、 D、 E。其中90 分 100 分为 A, 80分89 分为 B, 70 分79 分为 C, 6069 分为 D,60 分以下为 E。要求:( 1) 分别用 if 语句和 switch 语句实现。( 2) 输入百分制成绩后要判断该成绩的合理性,对不合理性的成绩应输出出错信息。If语句 s=input('please input the score:'); if s>=90&s<=100rank=
4、'A'elseif s>=80&s<=89rank='B'elseif s>=70&s<=79rank='C'elseif s>=60&s<=69rank='D'elseif s>0&s<=59rank='E'elserank='wrong socre'endrank >>>> bbbplease input the score:94。3。rank =A>> bbbplease inp
5、ut the score:75rank =C>> bbbplease input the score:-3rank =wrong socre>> bbbplease input the score:456rank =wrong socre>>Switch 语句 score=input('please input the score:'); switch floor(score/10)case9,10。4。rank='A'case8rank='B'case7rank='C'case6rank=
6、39;D'case num2cell(0:5)rank='E'otherwiserank='wrong score'endrank=rank>> cccplease input the score:-3rank =wrong score>> cccplease input the score:456rank =wrong score>> cccplease input the score:94。5。rank =A>> cccplease input the score:45rank =E3、 硅谷公司员工的
7、工资计算方法如下:( 1)、工作时数超过 120 小时者,超过部分加发 15%。( 2)、工作时数低于 60 小时者,扣发 700 元。( 3)、其余按每小时 84 元计发。试编程按输入的工号和该号员工的工时数,计算应发工资。 number=input('please input work number:'); h=input('please input work hours:');if h>120 wage=120*84+(h-120)*84*1.15;elseif h<60wage=h*84-700;elsewage=h*84;endwage &
8、gt;> dddplease input work number:01。6。please input work hours:74wage =6216>> dddplease input work number:02please input work hours:53wage =3752>> dddplease input work number:03please input work hours:135wage =11529>>4、 设计程序,完成两位数的加、减、乘、除四则运算,即产生两个两位随机整数,再输入一个运算符号,做相应的运算,并显示相应的结果
9、。 x=input('please input a sign:','s'); x1=round(rand(1)*90+10); x2=round(rand(1)*90+10);if x='+' answer=x1+x2;。7。elseif x='-'answer=x1-x2;elseif x='*'answer=x1*x2;elseif x='/'answer=x1/x2;endx1x2answer > eeeplease input a sign:+x1 =83x2 =11answer =9
10、4>> eeeplease input a sign:-。8。x1 =23x2 =28answer =-5>>5、 建立 5× 6 矩阵,要求输出矩阵第n 行元素。当n 值超过矩阵的行数时,自动转为输出矩阵的最后一行元素,并给出出错信息。 x=rand(5,6); n=input('please input n:'); if n>0&n<=5y=x( n,:);elseif n>5;y=x(5,:);disp('wrong n');elseif n<0;y=( 'wrong n');endy。9。>> eeeplease input n:4y =0.93830.27890.47870.92220.92380.5945>> eeeplease
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026临沂职业学院招聘教师和教辅人员22人考试参考题库及答案解析
- 消费类公司管理制度(3篇)
- 全聚德生日活动策划方案(3篇)
- 2026年浙江兴海控股集团有限公司下属企业招聘3人参考考试题库及答案解析
- 陵水打井施工方案(3篇)
- 铝合金销售管理制度范本(3篇)
- 内江二幼招聘编外教师备考考试试题及答案解析
- 2026上海黄浦区中意工程创新学院教务岗位招聘1人备考考试试题及答案解析
- 动量定理在高考中的应用
- 2026年宁德师范学院附属小学招聘教师2人备考考试题库及答案解析
- GB/T 46210-2025项目成本管理指南
- 快手直播内容分发标准
- 高新技术企业专项审计操作手册
- 2025湖南湘能多经产业(集团)有限公司高校毕业生招聘(第三批)模拟试卷及完整答案详解1套
- 六化安全生产培训内容课件
- 辐射安全培训自主培训课件
- 2025年国家能源局公务员面试模拟题及解析
- 2025外研社小学英语三年级下册单词表(带音标)
- 维保约赔偿方案(3篇)
- 农机消防安全知识培训课件
- 行政事务处理员高级工工勤技师迎考测试题及答案-行政事务人员
评论
0/150
提交评论