matlab選擇結(jié)構(gòu)程序的設(shè)計答案_第1頁
matlab選擇結(jié)構(gòu)程序的設(shè)計答案_第2頁
matlab選擇結(jié)構(gòu)程序的設(shè)計答案_第3頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、實驗三 選擇結(jié)構(gòu)程序設(shè)計一、實驗?zāi)康?、掌握建立和執(zhí)行 M文件的方法。2、掌握利用 if 語句實現(xiàn)選擇結(jié)構(gòu)的方法。3、掌握利用 switch 語句實現(xiàn)多分支選擇結(jié)構(gòu)的方法。4、掌握 try 語句的使用。二、實驗內(nèi)容時的 y1 、 求分段函數(shù)的值。用 if 語句實現(xiàn),分別輸出 x=-5.0,3.0,1.0,2.0,2.5,3.0,5.0 值。 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=3y=x*x-5*x+6;

2、elsey=x*x-x-1;endy please input the value of x-5.0y =14>> aaaaaplease input the value of x-3.011>> aaaaaplease input the value of x1.0 y =2>> aaaaaplease input the value of x2.0y =1>> aaaaaplease input the value of x2.5 y =-0.2500>> aaaaaplease input the value of x3.0y

3、=>> aaaaa please input the value of x5.0192、輸入一個百分制成績,要求輸出成績等級A、B、C D E。其中90分100分為A, 80分89分為B, 70分79分為C, 6069分為D, 60分以下為 E。要求:( 1 ) 分別用 if 語句和 switch 語句實現(xiàn)。(2) 輸入百分制成績后要判斷該成績的合理性,對不合理性的成績應(yīng)輸出出錯信息。If 語句 s=input('please input the score:');if s>=90&s<=100rank='A'elseif s&g

4、t;=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:94rank =A>> bbbplease input the score:75rank =C>

5、;> 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,10rank='A'case8rank='B'case7rank='C'case6rank='D'case num2cell(0:5)r

6、ank='E'otherwiserank='wrong score'endrank=rank>> cccplease input the score:-3rank = wrong score>> cccplease input the score:456rank =wrong score>> cccplease input the score:94rank =A>> cccplease input the score:45rank =E3、硅谷公司員工的工資計算方法如下:(1)、工作時數(shù)超過 120 小時者,超過部

7、分加發(fā)15%。(2)、工作時數(shù)低于 60 小時者,扣發(fā) 700 元。( 3)、其余按每小時 84 元計發(fā)。 試編程按輸入的工號和該號員工的工時數(shù),計算應(yīng)發(fā)工資。 number=input('please input work number:'); h=input('please input work hours:');if h>120wage=120*84+(h-120)*84*1.15;elseif h<60wage=h*84-700;elsewage=h*84;endwage >> dddplease input work numbe

8、r:01please input work hours:74 wage =6216>> dddplease input work number:02please input work hours:53 wage =3752>> dddplease input work number:03please input work hours:135 wage =11529>>4、設(shè)計程序,完成兩位數(shù)的加、減、乘、除四則運算,即產(chǎn)生兩個兩位隨機整數(shù),再輸入 一個運算符號,做相應(yīng)的運算,并顯示相應(yīng)的結(jié)果。 x=input('please input a sign

9、:','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;endx1x2answer > eee please input a sign:+ x1 =83 x2 =11 answer =94>> eee please input a sign:-x1 =23 x2 =28 answer =-5>>5、建立5X 6矩陣,要求輸出矩陣第n行元素。當(dāng)n值超過矩陣的行數(shù)時,自動轉(zhuǎn)為輸出矩陣的最后一行元素,并給出出錯信息。 x=ra nd(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');end>> eeeplease input n:4y =0.9383 0

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論