数学表达式解析汇报前缀中缀后缀.doc

上传人:夺命阿水 文档编号:26252 上传时间:2022-07-19 格式:DOC 页数:15 大小:162KB
返回 下载 相关 举报
数学表达式解析汇报前缀中缀后缀.doc_第1页
第1页 / 共15页
数学表达式解析汇报前缀中缀后缀.doc_第2页
第2页 / 共15页
数学表达式解析汇报前缀中缀后缀.doc_第3页
第3页 / 共15页
数学表达式解析汇报前缀中缀后缀.doc_第4页
第4页 / 共15页
数学表达式解析汇报前缀中缀后缀.doc_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《数学表达式解析汇报前缀中缀后缀.doc》由会员分享,可在线阅读,更多相关《数学表达式解析汇报前缀中缀后缀.doc(15页珍藏版)》请在课桌文档上搜索。

1、word前缀、中缀、后缀表达式它们都是对表达式的记法,因此也被称为前缀记法、中缀记法和后缀记法。它们之间的区别在于运算符相对与操作数的位置不同:前缀表达式的运算符位于与其相关的操作数之前;中缀和后缀同理。举例:(3 + 4) 5 - 6 就是中缀表达式- + 3 4 5 6前缀表达式3 4 + 5 6 -后缀表达式中缀表达式中缀记法中缀表达式是一种通用的算术或逻辑公式表示方法,操作符以中缀形式处于操作数的中间。中缀表达式是人们常用的算术表示方法。虽然人的大脑很容易理解与分析中缀表达式,但对计算机来说中缀表达式却是很复杂的,因此计算表达式的值时,通常需要先将中缀表达式转换为前缀或后缀表达式,然后

2、再进展求值。对计算机来说,计算前缀或后缀表达式的值非常简单。前缀表达式前缀记法、波兰式前缀表达式的运算符位于操作数之前。前缀表达式的计算机求值:从右至左扫描表达式,遇到数字时,将数字压入堆栈,遇到运算符时,弹出栈顶的两个数,用运算符对它们做相应的计算栈顶元素 op 次顶元素,并将结果入栈;重复上述过程直到表达式最左端,最后运算得出的值即为表达式的结果。例如前缀表达式“- + 3 4 5 6:(1) 从右至左扫描,将6、5、4、3压入堆栈;(2) 遇到+运算符,因此弹出3和43为栈顶元素,4为次顶元素,注意与后缀表达式做比拟,计算出3+4的值,得7,再将7入栈;(3) 接下来是运算符,因此弹出7

3、和5,计算出75=35,将35入栈;(4) 最后是-运算符,计算出35-6的值,即29,由此得出最终结果。可以看出,用计算机计算前缀表达式的值是很容易的。将中缀表达式转换为前缀表达式:遵循以下步骤:(1) 初始化两个栈:运算符栈S1和储存中间结果的栈S2;(2) 从右至左扫描中缀表达式;(3) 遇到操作数时,将其压入S2;(4) 遇到运算符时,比拟其与S1栈顶运算符的优先级:(4-1) 如果S1为空,或栈顶运算符为右括号“),如此直接将此运算符入栈;(4-2) 否如此,假如优先级比栈顶运算符的较高或相等,也将运算符压入S1;(4-3) 否如此,将S1栈顶的运算符弹出并压入到S2中,再次转到(4

4、-1)与S1中新的栈顶运算符相比拟;(5) 遇到括号时:(5-1) 如果是右括号“),如此直接压入S1;(5-2) 如果是左括号“(,如此依次弹出S1栈顶的运算符,并压入S2,直到遇到右括号为止,此时将这一对括号丢弃;(6) 重复步骤(2)至(5),直到表达式的最左边;(7) 将S1中剩余的运算符依次弹出并压入S2;(8) 依次弹出S2中的元素并输出,结果即为中缀表达式对应的前缀表达式。例如,将中缀表达式“1+(2+3)4)-5转换为前缀表达式的过程如下:扫描到的元素S2(栈底-栈顶)S1 (栈底-栈顶)说明55空数字,直接入栈-5-S1为空,运算符直接入栈)5- )右括号直接入栈45 4-

5、)数字直接入栈5 4- ) S1栈顶是右括号,直接入栈)5 4- ) )右括号直接入栈35 4 3- ) )数字+5 4 3- ) ) +S1栈顶是右括号,直接入栈25 4 3 2- ) ) +数字(5 4 3 2 +- ) 左括号,弹出运算符直至遇到右括号(5 4 3 2 + -同上+5 4 3 2 + - +优先级与-一样,入栈15 4 3 2 + 1- +数字到达最左端5 4 3 2 + 1 + -空S1中剩余的运算符因此结果为“- + 1 + 2 3 4 5。后缀表达式后缀记法、逆波兰式后缀表达式与前缀表达式类似,只是运算符位于操作数之后。后缀表达式的计算机求值:与前缀表达式类似,只是

6、顺序是从左至右:从左至右扫描表达式,遇到数字时,将数字压入堆栈,遇到运算符时,弹出栈顶的两个数,用运算符对它们做相应的计算次顶元素 op栈顶元素,并将结果入栈;重复上述过程直到表达式最右端,最后运算得出的值即为表达式的结果。例如后缀表达式“3 4 + 5 6 -:(1) 从左至右扫描,将3和4压入堆栈;(2) 遇到+运算符,因此弹出4和34为栈顶元素,3为次顶元素,注意与前缀表达式做比拟,计算出3+4的值,得7,再将7入栈;(3) 将5入栈;(4)接下来是运算符,因此弹出5和7,计算出75=35,将35入栈;(5) 将6入栈;(6) 最后是-运算符,计算出35-6的值,即29,由此得出最终结果

7、。将中缀表达式转换为后缀表达式:与转换为前缀表达式相似,遵循以下步骤:(1) 初始化两个栈:运算符栈S1和储存中间结果的栈S2;(2) 从左至右扫描中缀表达式;(3) 遇到操作数时,将其压入S2;(4) 遇到运算符时,比拟其与S1栈顶运算符的优先级:(4-1) 如果S1为空,或栈顶运算符为左括号“(,如此直接将此运算符入栈;(4-2) 否如此,假如优先级比栈顶运算符的高,也将运算符压入S1注意转换为前缀表达式时是优先级较高或一样,而这里如此不包括一样的情况;(4-3) 否如此,将S1栈顶的运算符弹出并压入到S2中,再次转到(4-1)与S1中新的栈顶运算符相比拟;(5) 遇到括号时:(5-1)

8、如果是左括号“(,如此直接压入S1;(5-2) 如果是右括号“),如此依次弹出S1栈顶的运算符,并压入S2,直到遇到左括号为止,此时将这一对括号丢弃;(6) 重复步骤(2)至(5),直到表达式的最右边;(7) 将S1中剩余的运算符依次弹出并压入S2;(8) 依次弹出S2中的元素并输出,结果的逆序即为中缀表达式对应的后缀表达式转换为前缀表达式时不用逆序。例如,将中缀表达式“1+(2+3)4)-5转换为后缀表达式的过程如下:扫描到的元素S2(栈底-栈顶)S1 (栈底-栈顶)说明11空数字,直接入栈+1+S1为空,运算符直接入栈(1+ (左括号,直接入栈(1+ ( (同上21 2+ ( (数字+1

9、2+ ( ( +S1栈顶为左括号,运算符直接入栈31 2 3+ ( ( +数字)1 2 3 + (右括号,弹出运算符直至遇到左括号1 2 3 + ( S1栈顶为左括号,运算符直接入栈41 2 3 + 4+ ( 数字)1 2 3 + 4 +右括号,弹出运算符直至遇到左括号-1 2 3 + 4 +-与+优先级一样,因此弹出+,再压入-51 2 3 + 4 + 5-数字到达最右端1 2 3 + 4 + 5 -空S1中剩余的运算符因此结果为“1 2 3 + 4 + 5 -注意需要逆序输出。编写Java程序将一个中缀表达式转换为前缀表达式和后缀表达式,并计算表达式的值。其中的toPolishNotati

10、on()方法将中缀表达式转换为前缀表达式波兰式、toReversePolishNotation()方法如此用于将中缀表达式转换为后缀表达式逆波兰式:注:(1) 程序很长且注释比拟少,但如果将上面的理论内容弄懂之后再将程序编译并运行起来,还是比拟容易理解的。有耐心的话可以研究一下。(2) 此程序是笔者为了说明上述概念而编写,仅做了简单的测试,不保证其中没有Bug,因此不要将其用于除研究之外的其他场合。javaview plaincopy1. packageqmk.simple_test;2. importjava.util.Scanner;3. importjava.util.Stack;4.

11、/*5. *Exampleofconvertinganinfix-expressionto6. *PolishNotation(PN)orReversePolishNotation(RPN).7. *Writtenin2011-8-258. *authorQiaoMingkui9. */10. publicclassCalculator11. publicstaticfinalStringUSAGE=usage=n12. +inputtheexpressions,andthentheprogram13. +willcalculatethemandshowtheresult.n14. +inpu

12、tbyetoexit.n;15. /*16. *paramargs17. */18. publicstaticvoidmain(Stringargs)19. System.out.println(USAGE);20. Scannerscanner=newScanner(System.in);21. Stringinput=;22. finalStringCLOSE_MARK=bye;23. System.out.println(inputanexpression:);24. input=scanner.nextLine();25. while(input.length()!=026. &!CL

13、OSE_MARK.equals(input)27. System.out.print(PolishNotation(PN):);28. try29. toPolishNotation(input);30. catch(NumberFormatExceptione)31. System.out.println(ninputerror,notanumber.);32. catch(IllegalArgumentExceptione)33. System.out.println(ninputerror:+e.getMessage();34. catch(Exceptione)35. System.o

14、ut.println(ninputerror,invalidexpression.);36. 37. System.out.print(ReversePolishNotation(RPN):);38. try39. toReversePolishNotation(input);40. catch(NumberFormatExceptione)41. System.out.println(ninputerror,notanumber.);42. catch(IllegalArgumentExceptione)43. System.out.println(ninputerror:+e.getMes

15、sage();44. catch(Exceptione)45. System.out.println(ninputerror,invalidexpression.);46. 47. System.out.println(inputanewexpression:);48. input=scanner.nextLine();49. 50. System.out.println(programexits);51. 52. /*53. *parsetheexpression,andcalculateit.54. *paraminput55. *throwsIllegalArgumentExceptio

16、n56. *throwsNumberFormatException57. */58. privatestaticvoidtoPolishNotation(Stringinput)59. throwsIllegalArgumentException,NumberFormatException60. intlen=input.length();61. charc,tempChar;62. Stacks1=newStack();63. Stacks2=newStack();64. Stackexpression=newStack();65. doublenumber;66. intlastIndex

17、=-1;67. for(inti=len-1;i=0;-i)68. c=input.charAt(i);69. if(Character.isDigit(c)70. lastIndex=readDoubleReverse(input,i);71. number=Double.parseDouble(input.substring(lastIndex,i+1);72. s2.push(number);73. i=lastIndex;74. if(int)number=number)75. expression.push(int)number);76. else77. expression.pus

18、h(number);78. elseif(isOperator(c)79. while(!s1.isEmpty()80. &s1.peek()!=)81. &prioritypare(c,s1.peek()0)82. expression.push(s1.peek();83. s2.push(calc(s2.pop(),s2.pop(),s1.pop();84. 85. s1.push(c);86. elseif(c=)87. s1.push(c);88. elseif(c=()89. while(tempChar=s1.pop()!=)90. expression.push(tempChar

19、);91. s2.push(calc(s2.pop(),s2.pop(),tempChar);92. if(s1.isEmpty()93. thrownewIllegalArgumentException(94. bracketdosentmatch,missingrightbracket).);95. 96. 97. elseif(c=)98. /ignore99. else100. thrownewIllegalArgumentException(101. wrongcharacter+c+);102. 103. 104. while(!s1.isEmpty()105. tempChar=

20、s1.pop();106. expression.push(tempChar);107. s2.push(calc(s2.pop(),s2.pop(),tempChar);108. 109. while(!expression.isEmpty()110. System.out.print(expression.pop()+);111. 112. doubleresult=s2.pop();113. if(!s2.isEmpty()114. thrownewIllegalArgumentException(inputisawrongexpression.);115. System.out.pri

21、ntln();116. if(int)result=result)117. System.out.println(theresultis+(int)result);118. else119. System.out.println(theresultis+result);120. 121. /*122. *parsetheexpression,andcalculateit.123. *paraminput124. *throwsIllegalArgumentException125. *throwsNumberFormatException126. */127. privatestaticvoi

22、dtoReversePolishNotation(Stringinput)128. throwsIllegalArgumentException,NumberFormatException129. intlen=input.length();130. charc,tempChar;131. Stacks1=newStack();132. Stacks2=newStack();133. doublenumber;134. intlastIndex=-1;135. for(inti=0;ilen;+i)136. c=input.charAt(i);137. if(Character.isDigit

23、(c)|c=.)138. lastIndex=readDouble(input,i);139. number=Double.parseDouble(input.substring(i,lastIndex);140. s2.push(number);141. i=lastIndex-1;142. if(int)number=number)143. System.out.print(int)number+);144. else145. System.out.print(number+);146. elseif(isOperator(c)147. while(!s1.isEmpty()148. &s

24、1.peek()!=(149. &prioritypare(c,s1.peek()=0;-i)243. c=input.charAt(i);244. if(c=.)245. if(dotIndex!=-1)246. thrownewIllegalArgumentException(247. therehavemorethan1dotsinthenumber.);248. else249. dotIndex=i;250. elseif(!Character.isDigit(c)251. returni+1;252. elseif(i=0)253. return0;254. 255. 256. t

25、hrownewIllegalArgumentException(notanumber.);257. 258.259. /*260. *readthenextnumber261. *paraminput262. *paramstart263. *return264. *throwsIllegalArgumentException265. */266. privatestaticintreadDouble(Stringinput,intstart)267. throwsIllegalArgumentException268. intlen=input.length();269. intdotInd

26、ex=-1;270. charc;271. for(inti=start;i1)284. returni;285. else286. thrownewIllegalArgumentException(287. notanumber,dotcantbethelastpartofanumber.);288. elseif(i=len-1)289. returnlen;290. 291. 292.293. thrownewIllegalArgumentException(notanumber.);294. 295. /*296. *returntrueifthecharacterisanoperat

27、or.297. *paramc298. *return299. */300. privatestaticbooleanisOperator(charc)301. return(c=+|c=-|c=*|c=/);302. 303. 下面是程序运行结果绿色为用户输入:= usage =input the expressions, and then the program will calculate them and show the result.input bye to exit.input an expression:Reverse Polish Notation (RPN):3.8 5.3

28、 +input a new expression:5*(9.1+3.2)/(1-5+4.88)Reverse Polish Notation (RPN):5 9.1 3.2 + * 1 5 - 4.88 + /input a new expression:1+(2+3)*4)-5Polish Notation (PN):- + 1 * + 2 3 4 5the result is 16Reverse Polish Notation (RPN):1 2 3 + 4 * + 5 -the result is 16input a new expression:byeprogram exits15 / 15

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

当前位置:首页 > 在线阅读 > 生活休闲


备案号:宁ICP备20000045号-1

经营许可证:宁B2-20210002

宁公网安备 64010402000986号