文档库 最新最全的文档下载
当前位置:文档库 › matlab选择结构程序设计答案

matlab选择结构程序设计答案

matlab选择结构程序设计答案
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('please input the value of x');

if x<0&x~=-3

y=x*x+x-6;

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

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

else

y=x*x-x-1;

end

y

②please input the value of x-5.0

y =

14

>> aaaaa

please input the value of x-3.0

y =

11

>> aaaaa

please input the value of x1.0 y =

2

>> aaaaa

please input the value of x2.0 y =

1

>> aaaaa

please input the value of x2.5 y =

-0.2500

>> aaaaa

please input the value of x3.0 y =

5

>> aaaaa

please input the value of x5.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('please input the score:');

if s>=90&s<=100

rank='A';

elseif s>=80&s<=89

rank='B';

elseif s>=70&s<=79

rank='C';

elseif s>=60&s<=69

rank='D';

elseif s>0&s<=59

rank='E';

else

rank='wrong socre'

end

rank

②>>

>> bbb

please input the score:94

rank =

A

>> bbb

please input the score:75

rank =

C

>> bbb

please input the score:-3

rank =

wrong socre

>> bbb

please input the score:456

rank =

wrong socre

>>

Switch语句

①score=input('please input the score:'); switch floor(score/10)

case{9,10}

rank='A';

case{8}

rank='B';

case{7}

rank='C';

case{6}

rank='D';

case num2cell(0:5)

rank='E';

otherwise

rank='wrong score'; end

rank=rank

>> ccc

please input the score:-3 rank =

wrong score

>> ccc

please input the score:456 rank =

wrong score

>> ccc

please input the score:94

rank =

A

>> ccc

please input the score:45

rank =

E

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

(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<60

wage=h*84-700;

else

wage=h*84;

end

wage

②>> ddd

please input work number:01

please input work hours:74

相关文档