matlab选择结构程序设计答案.docx

上传人:b****1 文档编号:485187 上传时间:2023-04-29 格式:DOCX 页数:9 大小:16.25KB
下载 相关 举报
matlab选择结构程序设计答案.docx_第1页
第1页 / 共9页
matlab选择结构程序设计答案.docx_第2页
第2页 / 共9页
matlab选择结构程序设计答案.docx_第3页
第3页 / 共9页
matlab选择结构程序设计答案.docx_第4页
第4页 / 共9页
matlab选择结构程序设计答案.docx_第5页
第5页 / 共9页
matlab选择结构程序设计答案.docx_第6页
第6页 / 共9页
matlab选择结构程序设计答案.docx_第7页
第7页 / 共9页
matlab选择结构程序设计答案.docx_第8页
第8页 / 共9页
matlab选择结构程序设计答案.docx_第9页
第9页 / 共9页
亲,该文档总共9页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

matlab选择结构程序设计答案.docx

《matlab选择结构程序设计答案.docx》由会员分享,可在线阅读,更多相关《matlab选择结构程序设计答案.docx(9页珍藏版)》请在冰点文库上搜索。

matlab选择结构程序设计答案.docx

matlab选择结构程序设计答案

实验三选择结构程序设计

一、实验目的

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('pleaseinputthevalueofx');

ifx<0&x~=-3

y=x*x+x-6;

elseifx>=0&x<5&x~=2&x~=3

y=x*x-5*x+6;

else

y=x*x-x-1;

end

y

②pleaseinputthevalueofx-5.0

y=

14

>>aaaaa

pleaseinputthevalueofx-3.0

y=

11

>>aaaaa

pleaseinputthevalueofx1.0

y=

2

>>aaaaa

pleaseinputthevalueofx2.0

y=

1

>>aaaaa

pleaseinputthevalueofx2.5

y=

-0.2500

>>aaaaa

pleaseinputthevalueofx3.0

y=

5

>>aaaaa

pleaseinputthevalueofx5.0

y=

19

2、输入一个百分制成绩,要求输出成绩等级A、B、C、D、E。

其中90分~100分为A,80分~89分为B,70分~79分为C,60~69分为D,60分以下为E。

要求:

(1)分别用if语句和switch语句实现。

(2)输入百分制成绩后要判断该成绩的合理性,对不合理性的成绩应输出出错信息。

If语句

①s=input('pleaseinputthescore:

');

ifs>=90&s<=100

rank='A';

elseifs>=80&s<=89

rank='B';

elseifs>=70&s<=79

rank='C';

elseifs>=60&s<=69

rank='D';

elseifs>0&s<=59

rank='E';

else

rank='wrongsocre'

end

rank

②>>

>>bbb

pleaseinputthescore:

94

rank=

A

>>bbb

pleaseinputthescore:

75

rank=

C

>>bbb

pleaseinputthescore:

-3

rank=

wrongsocre

>>bbb

pleaseinputthescore:

456

rank=

wrongsocre

>>

Switch语句

①score=input('pleaseinputthescore:

');

switchfloor(score/10)

case{9,10}

rank='A';

case{8}

rank='B';

case{7}

rank='C';

case{6}

rank='D';

casenum2cell(0:

5)

rank='E';

otherwise

rank='wrongscore';

end

rank=rank

>>ccc

pleaseinputthescore:

-3

rank=

wrongscore

>>ccc

pleaseinputthescore:

456

rank=

wrongscore

>>ccc

pleaseinputthescore:

94

rank=

A

>>ccc

pleaseinputthescore:

45

rank=

E

 

3、硅谷公司员工的工资计算方法如下:

(1)、工作时数超过120小时者,超过部分加发15%。

(2)、工作时数低于60小时者,扣发700元。

(3)、其余按每小时84元计发。

试编程按输入的工号和该号员工的工时数,计算应发工资。

①number=input('pleaseinputworknumber:

');

h=input('pleaseinputworkhours:

');

ifh>120

wage=120*84+(h-120)*84*1.15;

elseifh<60

wage=h*84-700;

else

wage=h*84;

end

wage

②>>ddd

pleaseinputworknumber:

01

pleaseinputworkhours:

74

wage=

6216

>>ddd

pleaseinputworknumber:

02

pleaseinputworkhours:

53

wage=

3752

>>ddd

pleaseinputworknumber:

03

pleaseinputworkhours:

135

wage=

11529

>>

4、设计程序,完成两位数的加、减、乘、除四则运算,即产生两个两位随机整数,再输入一个运算符号,做相应的运算,并显示相应的结果。

①x=input('pleaseinputasign:

','s');

x1=round(rand

(1)*90+10);

x2=round(rand

(1)*90+10);

ifx=='+'

answer=x1+x2;

elseifx=='-'

answer=x1-x2;

elseifx=='*'

answer=x1*x2;

elseifx=='/'

answer=x1/x2;

end

x1

x2

answer

②>eee

pleaseinputasign:

+

x1=

83

 

x2=

11

 

answer=

94

>>eee

pleaseinputasign:

-

x1=

23

 

x2=

28

 

answer=

-5

>>

 

5、建立5×6矩阵,要求输出矩阵第n行元素。

当n值超过矩阵的行数时,自动转为输出矩阵的最后一行元素,并给出出错信息。

①x=rand(5,6);

n=input('pleaseinputn:

');

ifn>0&n<=5

y=x(n,:

);

elseifn>5;

y=x(5,:

);

disp('wrongn');

elseifn<0;

y=('wrongn');

end

y

>>eee

pleaseinputn:

4

y=

0.93830.27890.47870.92220.92380.5945

>>eee

pleaseinputn:

7

wrongn

y=

0.43970.11210.94420.57440.14430.3099

>>eee

pleaseinputn:

-3

y=

wrongn

>>

 

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2