實驗三選擇結構程式設計
一、 實驗目的
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;
endy
②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'
endrank
②>>>> 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
rank='a';
case
rank='b';
case
rank='c';
case
rank='d';
case num2cell(0:5)
rank='e';
otherwise
rank='wrong score';
endrank=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 =
e3、 矽谷公司員工的工資計算方法如下:
(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;
endwage
②>> ddd
please input work number:01
please input work hours:74
wage =
6216
>> ddd
please input work number:02
please input work hours:53
wage =
3752
>> ddd
please input work number:03
please input work hours:135
wage =
11529
>>4、 設計程式,完成兩位數的加、減、乘、除四則運算,即產生兩個兩位隨機整數,再輸入乙個運算符號,做相應的運算,並顯示相應的結果。
①x=input('please input a sign:','s');
x1=round(rand(1)*90+10);
x2=round(rand(1)*90+10);
if x=='+'
answer=x1+x2;
elseif x=='-'
answer=x1-x2;
elseif x=='*'
answer=x1*x2;
elseif x=='/'
answer=x1/x2;
endx1
x2answer
②> eee
please input a sign:+
x1 =
83x2 =
11answer =
94>> eee
please input a sign:-
x1 =
23x2 =
28answer =
-5>>5、 建立5×6矩陣,要求輸出矩陣第n行元素。當n值超過矩陣的行數時,自動轉為輸出矩陣的最後一行元素,並給出出錯資訊。
①x=rand(5,6);
n=input('please input n:');
if n>0&n<=5
y=x( n,:);
elseif n>5;
y=x(5,:);
disp('wrong n');
elseif n<0;
y=( 'wrong n');
endy
②>> eee
please input n:4
y = 0.9383 0.2789 0.4787 0.9222 0.9238 0.5945
>> eee
please input n:7
wrong n
y = 0.4397 0.1121 0.9442 0.5744 0.1443 0.3099
>> eee
please input n:-3
y =wrong n
>>
實驗三選擇結構程式設計答案
1.求分段函式的值。用if語句實現,分別輸出時的y值。x input x if x 0 x 3 y x 2 x 6 elseif x 0 x 5 x 2 x 3 y x 2 5 x 6 else y x 2 x 1 endy 2.輸入乙個百分制成績,要求輸出成等級a b c d e。其中90分 10...
選擇結構程式設計
四川師範大學物理與電子工程學院2014級2班張文鵬 1 實驗目的 1 了解c語句表示邏輯量的方法 以0代表 假 以1代表 真 2 學會正確使用邏輯運算子和邏輯表示式 3 熟練掌握if語句和switch語句。2 實驗原理 在邏輯運算中先判斷該邏輯運算值得真假,如果為真應該執行什麼程式,為假應該執行什麼...
選擇結構程式設計,迴圈結構程式設計習題
習題4選擇結構程式設計 一 單選題 1 有如下巢狀的 語句 if a if a else k c if b else k c 以下選項中與上述 語句等價的語句是 k a k a k a k a2.以下選項中與if a 1 a b else a 語句功能不同的switch語句是 switch case...