Java实验研究报告Word下载.docx

上传人:b****6 文档编号:17439218 上传时间:2022-12-01 格式:DOCX 页数:19 大小:65.24KB
下载 相关 举报
Java实验研究报告Word下载.docx_第1页
第1页 / 共19页
Java实验研究报告Word下载.docx_第2页
第2页 / 共19页
Java实验研究报告Word下载.docx_第3页
第3页 / 共19页
Java实验研究报告Word下载.docx_第4页
第4页 / 共19页
Java实验研究报告Word下载.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

Java实验研究报告Word下载.docx

《Java实验研究报告Word下载.docx》由会员分享,可在线阅读,更多相关《Java实验研究报告Word下载.docx(19页珍藏版)》请在冰豆网上搜索。

Java实验研究报告Word下载.docx

c)表达式中可以出现不带符号的常数

d)适当判断表达式中的语法错误

e)不计算结果

f)参考:

(算法描述)

1.若遇到的是数字或小数点,则直接写入到strResult中(strResult为结果字符串):

2.若遇到的是左括号,则应把它压入到运算符栈中;

3.若遇到的是右括号,把从栈顶直到保存着的对应左括号之间的运算符依次退栈并写入strResult串中;

4.若遇到的是运算符:

4.1当该运算符的优先级大于栈顶运算符的优先级时,进栈4.2若遇到的运算符的优先级小于或等于栈顶运算符的优先级,这表明栈顶运算符的两个运算对象已经被保存到strResult串中,应将栈顶运算符退栈并写入到strResult串中,对于新的栈顶运算符仍继续进行比较和处理,直到被处理的运算符的优先级大于栈顶运算符的优先级为止,然后让该运算符进栈即可。

四、实验结果与数据处理

程序代码:

importjava・io・BufferedReader°

importjava・io・IOExaeption,,

importjava・io・InputstreamReader<

>

publicclassexperiment^{

staticintintlnputValue®

staticStringstrInputValueo

staticintlength。

publicstaticvoidmain(Stringargs[])

{

randomO.//第1题随机数排序査找的功能

calendar()o//第二題日历的功能

String[]myString=newString[]{"

"

}<

System.out.print(H请输入合法的中缀表达式:

)o

if(getStringHnput(myS仁ring))

MidfixToSuffix(myString[0])。

}

staticvoidrandom()

int[][]anArray«

anArray=newint[100][2]<

»

//二维数组,存取随机数和其固定编号

for(inti=0°

KlOOoi++)

anArray[i][0]=(int)(Math・random()"

200)+1。

//产生100个在1-200之间的随机数

anJLrray[i][1]=i+lo

System,out・printin(H随机产生的数为:

”)。

for(inti=0oi<

100oi++)//打印排序前的随机数

System.out.print(String・format(H%1$5d"

zanArray[i][0]))。

if((i+l)%20==0)

BobbleSort(anArray)o//调用排序函数对随机数进行排序System.out.printin(M排序后的数为:

”。

iXlOO。

i++)

System・out・print(String・format(H%1$5d'

f.anArray[i][0]))©

System・out・println()。

intnum=BinarySearch(anArray,50,anArray・leng七h)。

//返回査找到的数的下标,没有则为

System・out・printin()o

if(num<

0)

System・out.printin(nNOFOUND!

)。

//输出査找失败信息

else

System.out.print('

•50是排序前的第"

//输出该数为第几个数

intm=num€

for(inti=numoi>

0oi——)

<

if(anArray[i][0]==50)

System,out.print(an2Lrray[i][1]+H"

)o//输出该数为第几个数

for(inti=m+l<

i<

100oi++)

System,out.print(anArray[i][l]+nn)o//输出该数为第几个数

System.out.printin(n个数”)。

staticintBinarySearch(int[][]arrfintsearchkeyfintn)//二分查找算法

intlow=0。

inthigh=n-l<

System.out.print("

和50比较过的数依次为:

n)o

while(low<

=high)

intmid=(low+high)/2<

"

+arr[mid][0])0//每一次都是下标为mid的数和

50比较

if(searchkey==arr[mid][0])returnmid。

//如果找到,返回其编号else

if(searchkey>

arr[mid][0])

low=mid+l<

high=mid-l<

return-16

staticvoidBobbleSort(int[][]arr)//冒泡法曲乍序

intizjzchange。

ahange=l0

j=arr・length-l。

while(j>

0&

&

change==1)

change=0c

for(i=0ei<

joi++)

if(arr[i][0]>

arr[i+l][0])//将随机数和其编号同时交换

inttempl=arr[i][0]o

inttemp2=arr[i][1]«

arr[i][0]=arr[i+l][0]<

arr[i][l]=arr[i+l][1]«

arr[1+1][0]=templo

arr[1+1][1]=temp2c

change=l€

staticvoidcalendar()

intyearl=0。

intmonthl=0。

if(getlntegerlnput(”请输入要査询日历的年月"

+n\rn+'

f年份”))yearl=intInputValue0

if(getlntegerlnput("

月份”))

monthl=intInputValue<

Output(yearl,monthl)(>

//对特定的yearlymonthl的日历输出intyeaz?

2=0。

if(getlntegerlnput(n请输入要査询的全年日历的年份"

))

year2=intInputValue<

for(inti=l<

=12°

i++)

Output(year2,i)<

//调用同一个函数,进2012年的12个月的日历输出

staticvoidOutput(intyearzintmonth)//输出函数

inttotalDays=0<

for(inty=1901oy<

yearoy++)

for(intm=l€m<

=120m++)

totalDays+=Day(y,m)°

for(intm=l<

m<

month<

m++)

totalDays+=Day(yearzm)。

//在199:

1・1.1的基础上算当前日期是第几天

System・out・printin("

年”+month+"

月n)o//打印表头和横线

System・out・printin("

System,out.println(八日一’二三四五

六。

intweek=Week(totalDays)//表示当前月的第一天的星期

intday=Day(year,month)<

//当前月的天数

for(inti=0。

iXweek%7°

//打印3■号前的空格

for(inti=l。

i<

=day<

System.out.print(String・format("

%1$4dnri))o

if((i+week)%7==0)//依次打印出一个月的日期

System.out.println("

\r”)<

staticintWeek(intdays)//计算某一天的星期

intweek[]={l,2z3,4z5z6z7}o

//intday_1991_l_l=week[l],//经査找,1901年1月1日是星期二

inttheweek=week[(days+1)%7]o

returntheweek®

staticintDay(intyearfintmonth)//用来确定任意一年任意一月的天数

intnumDays=0。

switch(month)

case1:

case3:

case5:

case7:

case10:

case12:

numDays=31^//大月的情况

break<

case

4:

6:

9:

11:

numDays=30e//小月的情况

break®

case2:

//闰年的情况

if(((year%4==0)&

!

(year*100==0))||(year%400==0))

niimDays=29<

nxiinDays=28o

returnnumDayso

staticbooleanisEmpty(char[]stack)//判断栈是否为空<

if(length==0)

returntrue。

returnfalse<

staticbooleanisFull(char[]stack)//判断栈是否为满

if(length==50)

returntrue©

returnfalse®

staticintLength(char[]stack)//返回栈中元素个数

returnlength©

staticchargetTop(char[]stack)//返回栈顶元素

if(isEmpty(stack))

staticvoidpop(char[]stack)//出栈

staticvoidMidfixToSuffix(Strings)

push(myStack,s・charAt(i))«

break©

•)•)//是右括号则栈中左抬号上面

elseif(s・charAt(i)

的所有符号依次出栈

pop(myStack)<

breako

elseif(isEmpty(myStack)||getTop(myStack)==

push(myStack,s・charAt(i))°

break®

elseif((s・charAt(i)==1*1||s・charAt(i)=='

/

s・charAt(i)==f%*)

(getTop(myS仁ack)==1+1||getTop(myStack)

{//根据运算符的优先级判断,如果是高优先级进栈

push(myStack,s・charAt(i))o

System・out・print(getTop(myStack))°

pop(myStack)o

else//运算变量直接输出

System・out.print(s・charAt(i))。

while(•isEmpty(myStack))//将栈中剩余的运算符逍接输出{

System・out・print(getTop(myStack))o

pop(myStack)©

staticbooleangetlntegerlnput(StringaPrompt)//返回值输入值

{//若返回false,则输入出错,输入的值不能用。

若返回true,输入正确,输入值可以用

intanlnteger=O<

Stringstrlnput=null。

try

System.out.print(aPrompt+H:

°

BufferedReaderbr=newBufferedReader(new

InputStreamReader(System・in))。

strlnput=br・readLine()。

anlnteger=lnteger.parselntL(strinput)。

catch(IOExceptione)

System.out.printin(HTO错误!

请重新运行程序。

catch(NiunberFormatExceptione)

System.out.printin("

你的输入可能有格式错课!

returnfalse®

intInputValue=anInteger<

returntrue®

staticbooleangetStringlnput(String[]str)

//Stringstrlnput=null°

booleanisInputMistake=:

false。

try{

//System.out.print(H请输入一个字符串:

BufferedReaderbr=newBufferedReader(newInputstreamReader(

System.in))。

str[0]=br・readLine()<

}catch(IOExceptione){

isInputMistake=true®

//System.out.printin(nIO错误!

return!

isInputMistake<

程序运行的结果:

(复制到word中排版变混乱了)

随机产生的数为:

154

192

52

184

47

773109

42

133

164

48

177

89

120

76

115

64

120130

143

41

178

127

8

4310159

81

34

149

185

104

1

26

85

43

168146

12

121

68143

182

5510

191

155

57

14

179

54106

158

14099

197

15

16

54159

25

93117

103

36

59

10

44164

140157

18

156

50

23195124

83

150

160

37

51

193

8987

190

70124

151

176

排序后的数为:

7

1214

2325

2526

44

5051

54

55

5759

68

70

73

87

8989

93

99

101

104104

106

109

115117120120

124

130133

140

146149

154155156157

159

168

176177

185190191192192193195197

和50比较过的数依次为:

9947685451485050是排序前的第85个数

请输入要查询日历的年月

年份:

2012

月份:

4

2012年4月

2

5

6

9

11

13

17

19

20

21

22

23

24

27

28

29

30

请输入要查询的全年日历的年份:

2012年1月

EI

■■■

31

2012年2月

日一二三四

567

121314

192021

262728

2012年3月

2930

2012年5月

2012年6月

2012年7月

2012年8月

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 其它

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1