《Java語言程序設(shè)計》實驗報告可編輯_第1頁
《Java語言程序設(shè)計》實驗報告可編輯_第2頁
《Java語言程序設(shè)計》實驗報告可編輯_第3頁
《Java語言程序設(shè)計》實驗報告可編輯_第4頁
免費預(yù)覽已結(jié)束,剩余20頁可下載查看

下載本文檔

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

文檔簡介

1、java語言程序設(shè)計實驗報告可編輯 java 語言程序設(shè)計 實驗報告 實驗序號:3 實驗項目名稱:熟練 java 語法、常用方法、程序結(jié)構(gòu) 學(xué) 號 10090401gg 姓 名 gg 專業(yè)、班 電子商務(wù) 1002 實驗地點 gg 指導(dǎo)教師 gg 時間 20pp.4.26 一、實驗?zāi)康募耙?1、進一步熟練java語法和常用方法的使用; 2、進一步熟練java程序結(jié)構(gòu); 3、進一步提高算法設(shè)計與實現(xiàn)的能力; 4、為后續(xù)章節(jié)的學(xué)習(xí)打下堅實的基礎(chǔ); 二、實驗設(shè)備(環(huán)境) 實驗室計算機、windowsgp 系統(tǒng)、mpeclipse 三、 實驗內(nèi)容與步驟 1.隨機生成100個0到200的整數(shù),用折半查找

2、法(二分法)查找50是第幾個數(shù),并輸出查找過程(即和什么數(shù)進行了比較)。 (折半查找是在已經(jīng)排序的數(shù)據(jù)中做的查找,所以先要排序) 2.顯示任意一個月份的日歷(1900)(一周一行,要有月名、表頭和橫線。按列右對齊,用 string.format(%1$4d,intnumber)可以將任意整數(shù) intnumber 格式化成前面補"空格'的 4 位定長字符串) 3.顯示出任一年份的日歷(1900) 4.用數(shù)組模擬實現(xiàn)一個50個字符串的堆棧,并使這個堆棧有如下方法和參數(shù): mpstack:數(shù)組名,使用時不能直接對這個數(shù)組進行存取操作,存取操作都通過pop()和push()完成。 實

3、現(xiàn)pop():彈出 實現(xiàn)push():壓入 實現(xiàn)isfull():是否已滿 實現(xiàn)isemptp():是否為空 實現(xiàn)length():已有多少元素 要點:要將代碼設(shè)計成有通用性,也就是說要考慮各種可能的情況,考慮越多,你的程序就越穩(wěn)定可靠,高可靠性的代碼一直是一個開發(fā)人員追求的最高目標(biāo)之一。 5.使用堆棧,將中綴算術(shù)表達式轉(zhuǎn)換成后綴表達式。 a) 表達式中只有、,/,(,)六種運算符 b) 變量名為以英文字母開頭的字母數(shù)字串 c) 表達式中可以出現(xiàn)不帶符號的常數(shù) d) 適當(dāng)判斷表達式中的語法錯誤 e) 不計算結(jié)果 f) 參考:(算法描述) 1.若遇到的是數(shù)字或小數(shù)點,則直接寫入到strresul

4、t中(strresult為結(jié)果字符串); 2.若遇到的是左括號,則應(yīng)把它壓入到運算符棧中; 3.若遇到的是右括號,把從棧頂直到保存著的對應(yīng)左括號之間的運算符依次退棧并寫入 strresult 串中; 4.若遇到的是運算符: 4.1 當(dāng)該運算符的優(yōu)先級大于棧頂運算符的優(yōu)先級時,進棧 4.2 若遇到的運算符的優(yōu)先級小于或等于棧頂運算符的優(yōu)先級,這表明棧頂運算符的兩個運算對象已經(jīng)被保存到 strresult 串中,應(yīng)將棧頂運算符退棧并寫入到strresult 串中,對于新的棧頂運算符仍繼續(xù)進行比較和處理,直到被處理的運算符的優(yōu)先級大于棧頂運算符的優(yōu)先級為止,然后讓該運算符進棧即可。 四、實驗結(jié)果與數(shù)

5、據(jù)處理 程序代碼: importjava.io.bufferedreader; importjava.io.ioegception; importjava.io.inputstreamreader; publicclassegperiment_3 staticintintinputvalue; staticstringstrinputvalue; staticintlength; publicstaticvoidmain(stringargs) random();/ 第1 題隨機數(shù)排序查找的功能 calendar();/ 第二題日歷的功能 stringmpstring=newstring; s

6、pstem.out.print( 請輸入合法的中綴表達式:); if(getstringinput(mpstring) midfigtosuffig(mpstring0); staticvoidrandom() intanarrap; anarrap=newint1002;/ 二維數(shù)組,存取隨機數(shù)和其固定編號 for(inti=0;i100;i+) anarrapi0=(int)(math.random()g200)+1;/ 產(chǎn)生100 個在1-200之間的隨機數(shù) anarrapi1=i+1; spstem.out.println( 隨機產(chǎn)生的數(shù)為:); for(inti=0;i100;i+)

7、/ 打印排序前的隨機數(shù) spstem.out.print(string.format(%1$5d,anarrapi0); if(i+1)%20=0) spstem.out.println(); bobblesort(anarrap);/ 調(diào)用排序函數(shù)對隨機數(shù)進行排序 spstem.out.println( 排序后的數(shù)為:); for(inti=0;i100;i+) spstem.out.print(string.format(%1$5d,anarrapi0); if(i+1)%20=0) spstem.out.println(); intnum=binarpsearch(anarrap,50,

8、anarrap.length);/ 返回查找到的數(shù)的下標(biāo),沒有則為-1 spstem.out.println(); if(num0) spstem.out.println(nofound!);/ 輸出 查找失敗信息 else spstem.out.print(50 是排序前的第);/ 輸出該數(shù)為第幾個數(shù) intm=num; for(inti=num;i0;i-) if(anarrapi0=50) spstem.out.print(anarrapi1+);/ 輸出該數(shù)為第幾個數(shù) for(inti=m+1;i100;i+) if(anarrapi0=50) spstem.out.print(ana

9、rrapi1+);/ 輸出該數(shù)為第幾個數(shù) spstem.out.println( 個數(shù));/ 輸出該數(shù)為第幾個數(shù) staticintbinarpsearch(intarr,intsearchkep,intn)/ 二分查找算法 intlow=0; inthigh=n-1; spstem.out.print( 和50 比較過的數(shù)依次為:); while(low=high) intmid=(low+high)/2; spstem.out.print(+arrmid0);/ 每一次都是下標(biāo)為mid 的數(shù)和50比較 if(searchkep=arrmid0) returnmid;/ 如果找到,返回其編號

10、 else if(searchkeparrmid0) low=mid+1; else high=mid-1; return-1; staticvoidbobblesort(intarr)/ 冒泡法排序 inti,j,change; change=1; j=arr.length-1; while(j0change=1) change=0; for(i=0;ij;i+) if(arri0arri+10)/ 將隨機數(shù)和其編號同時交換 inttemp1=arri0; inttemp2=arri1; arri0=arri+10; arri1=arri+11; arri+10=temp1; arri+11

11、=temp2; change=1; j-; staticvoidcalendar() intpear1=0; intmonth1=0; if(getintegerinput( 請輸入要查詢?nèi)諝v的年月+r+ 年份) pear1=intinputvalue; if(getintegerinput( 月份) month1=intinputvalue; output(pear1,month1);/ 對特定的pear1 和month1 的日歷輸出 intpear2=0; if(getintegerinput( 請輸入要查詢的全年日歷的年份) pear2=intinputvalue; for(inti=1

12、;i=12;i+) output(pear2,i);/ 調(diào)用同一個函數(shù),進20pp 年的12 個月的日歷輸出 staticvoidoutput(intpear,intmonth)/ 輸出函數(shù) inttotaldaps=0; for(intp=1901;ppear;p+) for(intm=1;m=12;m+) totaldaps+=dap(p,m); for(intm=1;mmonth;m+) totaldaps+=dap(pear,m);/ 在1991.1.1 的基礎(chǔ)上算當(dāng)前日期是第幾天 spstem.out.println( +pear+ 年+month+ 月);/ 打印表頭和橫線 sps

13、tem.out.println(-); spstem.out.println( 日一二三四五六); intweek=week(totaldaps);/ 表示當(dāng)前月的第一天的星期 intdap=dap(pear,month);/ 當(dāng)前月的天數(shù) for(inti=0;iweek%7;i+) spstem.out.print();/ 打印1 號前的空格 for(inti=1;i=dap;i+) spstem.out.print(string.format(%1$4d,i); if(i+week)%7=0)/ 依次打印出一個月的日期 spstem.out.println(); spstem.out.p

14、rintln(r); staticintweek(intdaps)/ 計算某一天的星期 intweek=1,2,3,4,5,6,7; /intdap_1991_1_1=week1;/ 經(jīng)查找,1901 年1 月1 日是星期二 inttheweek=week(daps+1)%7; returntheweek; staticintdap(intpear,intmonth)/ 用來確定任意一年任意一月的天數(shù) intnumdaps=0; switch(month) case1: case3: case5: case7: case8: case10: case12: numdaps=31;/ 大月的情況

15、 break; case4: case6: case9: case11: numdaps=30;/ 小月的情況 break; case2:/ 閏年的情況 if(pear%4=0)!(pear%100=0)|(pear%400=0) numdaps=29; else numdaps=28; break; returnnumdaps; staticbooleanisemptp(charstack)/ 判斷棧是否為空 if(length=0) returntrue; returnfalse; staticbooleanisfull(charstack)/ 判斷棧是否為滿 if(length=50)

16、returntrue; returnfalse; staticintlength(charstack)/ 返回棧中元素個數(shù) returnlength; staticchargettop(charstack)/ 返回棧頂元素 if(isemptp(stack) return"0" else returnstacklength(stack)-1; staticvoidpop(charstack)/ 出棧 if(isemptp(stack) return; length-; return; staticbooleanpush(charstack,charachar)/ 入棧 if

17、(isfull(stack) returnfalse; stacklength+=achar; returntrue; staticvoidmidfigtosuffig(strings) charmpstack=newchar50; spstem.out.println( 其后綴表達式為:); for(inti=0;is.length();i+) if(s.charat(i)="+"|s.charat(i)="-"|s.charat(i)="g"|s.charat(i)="/" |s.charat(i)="

18、;%"|s.charat(i)="("|s.charat(i)=")") / 如果是運算符則進一步判斷 spstem.out.print(); while(true) if(s.charat(i)="(")/ 是左括號則直接進棧 push(mpstack,s.charat(i); break; elseif(s.charat(i)=")")/ 是右括號則棧中左括號上面的所有符號依次出棧 while(gettop(mpstack)!="(") spstem.out.print(getto

19、p(mpstack)+); pop(mpstack); pop(mpstack); break; elseif(isemptp(mpstack)|gettop(mpstack)="(") push(mpstack,s.charat(i); break; elseif(s.charat(i)="g"|s.charat(i)="/"|s.charat(i)="%") (gettop(mpstack)="+"|gettop(mpstack)="-") / 根據(jù)運算符的優(yōu)先級判斷,如

20、果是高優(yōu)先級進棧 push(mpstack,s.charat(i); break; else spstem.out.print(gettop(mpstack); pop(mpstack); else/ 運算變量直接輸出 spstem.out.print(s.charat(i); while(!isemptp(mpstack)/ 將棧中剩余的運算符直接輸出 spstem.out.print(gettop(mpstack); pop(mpstack); staticbooleangetintegerinput(stringaprompt)/ 返回值輸入值 / 若返回false ,則輸入出錯,輸入的

21、值不能用。若返回true ,輸入正 確,輸入值可以用 intaninteger=0; stringstrinput=null; trp spstem.out.print(aprompt+ :); bufferedreaderbr=newbufferedreader(newinputstreamreader(spstem.in); strinput=br.readline(); aninteger=integer.parseint(strinput); catch(ioegceptione) spstem.out.println(io 錯誤!請重新運行程序。); returnfalse; cat

22、ch(numberformategceptione) spstem.out.println( 你的輸入可能有格式錯誤!請重新運行程序。); returnfalse; intinputvalue=aninteger; returntrue; staticbooleangetstringinput(stringstr) /stringstrinput=null; booleanisinputmistake=false; trp / spstem.out.print( 請輸入一個字符串:); bufferedreaderbr=newbufferedreader(newinputstreamreade

23、r( spstem.in); str0=br.readline(); catch(ioegceptione) isinputmistake=true; /spstem.out.println(io 錯誤!請重新運行程序。); return!isinputmistake; 程序運行的結(jié)果: (復(fù)制到word中排版變混亂了) 隨機產(chǎn)生的數(shù)為: 15419252184477731094213316448177891207611564120pp0 14341178127843101598134149185104126858943168146 121216814318255101911211555714

24、179541061581409919715 165415925127931171034736598104416410414015719218 25156765023195124831501603751193898719070124151176 排序后的數(shù)為: 178810101214151618232525263436374142 4343444747485051525454555759596468707376 76818385878989899399101103104104106109115117120pp0 12112112412412712713013314014014314314614

25、9150151154155156157 158159160164164168176177178179182184185190191192192193195197 和50比較過的數(shù)依次為:99476854514850 50是排序前的第85個數(shù) 請輸入要查詢?nèi)諝v的年月 年份:20pp 月份:4 20pp年4月 - 日一二三四五六 1234567 891011121314 15161718192021 22232425262728 2930 請輸入要查詢的全年日歷的年份:20pp 20pp年1月 - 日一二三四五六 1234567 891011121314 15161718192021 222324

26、25262728 293031 20pp年2月 - 日一二三四五六 1234 567891011 12131415161718 19202122232425 26272829 20pp年3月 - 日一二三四五六 123 45678910 11121314151617 18192021222324 25262728293031 20pp年4月 - 日一二三四五六 1234567 891011121314 15161718192021 22232425262728 2930 20pp年5月 - 日一二三四五六 12345 6789101112 13141516171819 202122232425

27、26 2728293031 20pp年6月 - 日一二三四五六 12 3456789 10111213141516 17181920212223 24252627282930 20pp年7月 - 日一二三四五六 1234567 891011121314 15161718192021 22232425262728 293031 20pp年8月 - 日一二三四五六 1234 567891011 12131415161718 19202122232425 262728293031 20pp年9月 - 日一二三四五六 1 2345678 9101112131415 16171819202122 232

28、42526272829 30 20pp年10月 - 日一二三四五六 123456 78910111213 14151617181920 21222324252627 28293031 20pp年11月 - 日一二三四五六 123 45678910 11121314151617 18192021222324 252627282930 20pp年12月 - 日一二三四五六 1 2345678 9101112131415 16171819202122 23242526272829 3031 請輸入合法的中綴表達式:9+(8g5-6)%6-8/3 其后綴表達式為:985g6-6%+83/- 數(shù)據(jù)處理:

29、 第1題:定義一個二維數(shù)組,用來存儲產(chǎn)生的隨機數(shù)和初始的編號,當(dāng)用冒泡法排序的時候,其固定的編號也隨之移動,再用二分查找法查找50的時候,將下標(biāo)為mid的數(shù)輸出即為依次比較過的數(shù),如果找到50則將其編號輸出即為其排序前所在的位置,如果沒找到則輸出提示信息。這道題的主要問題在于,如果產(chǎn)生了若干個50,如何都能打印出來,采用的方法是將二分查找出的第一個50的當(dāng)前下標(biāo),再前后搜索還有沒有50了,有就輸出對應(yīng)的原始下標(biāo)。 第 2、3 題:我采用的是以 1991 年 1 月 1 日為基礎(chǔ),查詢出該天是星期幾,再根據(jù)每一年中 12 個月分的天數(shù),要注意 2 月分平年和閏年的相差一天的區(qū)別,計算出輸入的年月的第一天是距 1991.1.1 多少天,總天數(shù) mod7 就可以得出第一天是周幾,輸出日歷和時候就可以從計算出的星期幾開始,依次打印出該月的所有天,第七個一行,第 3 題也就是將第 2 題的程序按照 1-1

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論