【程序設(shè)計實踐】實驗報告__實驗二_第1頁
【程序設(shè)計實踐】實驗報告__實驗二_第2頁
【程序設(shè)計實踐】實驗報告__實驗二_第3頁
【程序設(shè)計實踐】實驗報告__實驗二_第4頁
免費預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

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

文檔簡介

1、.實驗報告課程名稱_程序設(shè)計實踐 _實驗項目_子程序設(shè)計實驗儀器_PC_系別 _計算機(jī)科學(xué)與工程_專業(yè) _軟件工程 _班級 / 學(xué)號 _B軟工 0701/2007011801學(xué)生姓名_ XX_實驗日期_2009-3-26_成績 _指導(dǎo)教師_XX_;.實驗二子程序設(shè)計一、實驗?zāi)康?. 學(xué)習(xí)和掌握參數(shù)傳遞的方法;2學(xué)習(xí)和掌握遞歸調(diào)用;3了解 goto 語句的使用;4了解降低嵌套深度的方法;5了解復(fù)雜度的度量。二、實驗要求1通過編程實現(xiàn),學(xué)習(xí)和理解參數(shù)傳遞和遞歸調(diào)用的使用。2使用 goto 語句編寫程序,用非goto 語句改寫程序;3了解減少嵌套層次和度量復(fù)雜度的方法。三、實驗內(nèi)容和結(jié)果1. 參數(shù)傳

2、遞( 1)創(chuàng)建控制臺應(yīng)用程序項目(CLR) 。定義函數(shù)swap,swap 函數(shù)有兩個整型形參 x,y,swap 的功能為交換 x 和 y 的值,并依次顯示出交換后的 x,y 的值。主函數(shù)定義兩個整型變量 a,b,并賦不同的初值, 依次顯示 a 和 b 的值, 調(diào)用 swap 函數(shù),a,b 作為實參,再在主函數(shù)中依次顯示 a 和 b 的值。 (值傳遞 )編寫并運行以上程序,分析運行結(jié)果,思考值傳遞中是否會改變實參的值。( 2)創(chuàng)建控制臺應(yīng)用程序項目,修改( 1)中程序,使 swap 函數(shù)的參數(shù)為兩個指向整型的指針, swap 函數(shù)中交換指針?biāo)赶虻膬?nèi)容;主函數(shù)中調(diào)用 swap 函數(shù),交換整型變量

3、 a和 b 的值。編寫并運行修改后的程序, 比較與(1)中程序運行結(jié)果的不同之處及原因。( 3)創(chuàng)建控制臺應(yīng)用程序項目,修改( 1)中程序,采用引用方式傳遞函數(shù)的參數(shù)。編寫并運行修改后的程序,比較與( 1)、(2)中程序運行結(jié)果的異同以及( 2)、(3)程序中改寫的難易程度。int swap1(int x,int y)int temp;temp=x;x=y;y=temp;return 0; / 實參傳遞函數(shù)int swap2(int &x,int &y)int temp;temp=x;x=y;y=temp;return 0;/ 形參傳遞函數(shù)int swap3(int *x,in

4、t *y)int temp;temp=*x;*x=*y;*y=temp;return 0;/ 指針傳遞函數(shù)int main()int a,b;Console:WriteLine ("plesae input a and b interger:");a=Int32:Parse(Console:ReadLine();b=Int32:Parse(Console:ReadLine();Console:WriteLine (" 實參傳遞函數(shù)交換a和 b:");Console:WriteLine (" 起始 a 和 b:"+a+"&qu

5、ot;+b);swap1(a,b);Console:WriteLine (" 交換后 a 和 b:"+a+""+b);Console:WriteLine ("-");Console:WriteLine (" 形參傳遞函數(shù)交換a 和 b:");Console:WriteLine (" 起始 a 和 b:"+a+""+b);swap2(a,b);Console:WriteLine (" 交換后 a 和 b:"+a+""+b);Console

6、:WriteLine ("+");Console:WriteLine (" 指針傳遞函數(shù)交換a 和 b:");Console:WriteLine (" 起始 a 和 b:"+a+""+b);swap3(&a,&b);Console:WriteLine (" 交換后 a 和 b:"+a+""+b);Console:WriteLine ( "%" );Console:ReadLine();return 0;2遞歸調(diào)用;.使用遞歸時,請注意如下原

7、則:a.確認(rèn)遞歸能夠停止(子程序中至少含有一條非遞歸的路徑);b.把遞歸限制在一個子程序內(nèi);c.留心??臻g;使用遞歸前,先考慮可否用其他方式編寫程序,可能效果更好。創(chuàng)建控制臺應(yīng)用程序項目,用遞歸方式計算階乘,編寫計算階乘的函數(shù),輸入?yún)?shù)n,函數(shù)返回值為n 的階乘。編寫程序并運行結(jié)果。(注意溢出)創(chuàng)建控制臺應(yīng)用程序項目,用循環(huán)方式去計算階乘。編寫程序并運行結(jié)果。(注意溢出)long double digui(long double n)if(n=0|n=1)return(1);elsereturn (digui(n-1)*n);/ 遞歸函數(shù)long double xhuan(long doubl

8、e n)long double x=1;if(n=0|n=1)return(1);elsefor(int i=1;i<=n;i+)x=x*i;return x;/ 循環(huán)函數(shù)int main(array<System:String > args)Console:WriteLine("please input an integer n:"); long double n=Int32:Parse(Console:ReadLine(); while(n<0)Console:WriteLine("n<0,please reinput an in

9、teger n:"); n=Int32:Parse(Console:ReadLine();Console:WriteLine(" 循環(huán)輸出階乘n!="+xhuan(n);Console:WriteLine(" 遞歸輸出階乘n!="+digui(n);Console:ReadLine();return 0;3 goto 語句(1)創(chuàng)建控制臺應(yīng)用程序項目,編寫和運行如下程序。主函數(shù)如下:int data;int isend;doConsole:WriteLine("please input two number "); isen

10、d= Int32:Parse(Console:ReadLine(); data= Int32:Parse(Console:ReadLine(); if (isend=0)goto LOOP_EXIT;Console:WriteLine("no exit");while(data!=-1);LOOP_EXIT:Console:ReadLine();isend不為 0 時, data 為-1:isend不為 0 時, data 不為 -1:isend為 0:( 2)將上述程序改寫為沒有g(shù)oto 語句的如下程序int data;int isend;Console:WriteLin

11、e("please input two number"); isend= Int32:Parse(Console:ReadLine(); data= Int32:Parse(Console:ReadLine(); while(isend!=0)&&(data!=-1)Console:WriteLine("no exit"); Console:WriteLine("please input two number"); isend= Int32:Parse(Console:ReadLine(); data= Int32:P

12、arse(Console:ReadLine();Console:ReadLine();isend不為 0,data 為-1:;.isend 不為 0,data 不為 -1:isend 為 0:比較結(jié)果與( 1)的不同。不執(zhí)行 goto 語句:當(dāng) isend 不為 0,data 為-1 時兩次顯示結(jié)果不同,第二次沒有“noexit”字樣( 3)將(1)中程序改寫為沒有 goto 的程序,且運行結(jié)果要求與( 1)中一致。( 4)編寫并運行下列帶 goto 語句的程序int a4;(5)改寫( 4)中程序,要求利用嵌套的if-else 語句消除 goto 的for(int i=0;i<4;i+

13、)代碼,得到與( 4)相同的運行結(jié)果。int main(array<System:String > args)Console:WriteLine("please input number");ai= Int32:Parse(Console:ReadLine();int a4;for(int i=0;i<4;i+)if (a0=0)Console:WriteLine("please input number");Console:WriteLine("a0=0");ai= Int32:Parse(Console:Read

14、Line();goto END_PROC;if (a0=0)if (a1=0)Console:WriteLine("a0=0");elseConsole:WriteLine("a1=0");goto END_PROC;if (a1=0)Console:WriteLine("a1=0");if (a2=0)elseConsole:WriteLine("a2=0");if (a2=0)goto END_PROC;Console:WriteLine("a2=0");elseif (a3=0)if (a

15、3=0)Console:WriteLine("a3=0");Console:WriteLine("a3=0");goto END_PROC;END_PROC:Console:WriteLine("end_proc");END_PROC:Console:ReadLine();Console:WriteLine("end_proc");執(zhí)行 goto 語句:Console:ReadLine();.return 0;結(jié)果:(2)用重復(fù)測試的非嵌套代碼重寫上程序,減少嵌套深度。提示: if 語句部分可以用如下思路來重寫us

16、ing namespace Systemint main(array<System:String > args)int a4;for(int i=0;i<4;i+)觀察用 if 嵌套的程序與使用goto 語句的程序的代碼特點。Console:WriteLine("please input number");4. 嵌套層次控制ai= Int32:Parse(Console:ReadLine();(1)運行以下程序if (a0=0)&& (a1=0)&& (a2=0)int a4;for(int i=0;i<4;i+)Co

17、nsole:WriteLine("a0=0");Console:WriteLine("a1=0");Console:WriteLine("please input number");Console:WriteLine("a2=0");ai= Int32:Parse(Console:ReadLine();if (a3=0)if (a0=0)Console:WriteLine("a3=0");Console:WriteLine("a0=0");elseif (a1=0)if(a0

18、=0)&& (a1=0)Console:WriteLine("a1=0");if (a2=0)Console:WriteLine("a0=0");Console:WriteLine("a1=0");Console:WriteLine("a2=0");if(a2=0)if (a3=0)Console:WriteLine("a2=0");Console:WriteLine("a3=0");elseif(a0=0)Console:ReadLine();Console

19、:WriteLine("a0=0");if(a1=0)Console:WriteLine("a1=0");結(jié)果:Console:ReadLine();.return 0;截圖:思考( 2)中的代碼與( 1)中的代碼相比的優(yōu)缺點。一中代碼量較少,但是難度增加,二中代碼量多,難度減少( 3)調(diào)試和運行如下程序。intquantity;quantity= Int32:Parse(Console:ReadLine(); if (quantity<30)if(quantity<20)if(quantity<10)Console:WriteLine("quantity<10");elseConsole:WriteLine("20> quantity>=10");elseConsole:WriteLine("30> q

溫馨提示

  • 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

提交評論