Java实现四则运算表达式.docx

上传人:scccc 文档编号:13564806 上传时间:2022-01-16 格式:DOCX 页数:14 大小:21.09KB
返回 下载 相关 举报
Java实现四则运算表达式.docx_第1页
第1页 / 共14页
Java实现四则运算表达式.docx_第2页
第2页 / 共14页
Java实现四则运算表达式.docx_第3页
第3页 / 共14页
Java实现四则运算表达式.docx_第4页
第4页 / 共14页
Java实现四则运算表达式.docx_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《Java实现四则运算表达式.docx》由会员分享,可在线阅读,更多相关《Java实现四则运算表达式.docx(14页珍藏版)》请在三一文库上搜索。

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

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

3、下来是 咕算符,因此弹出 7和5,计算出7X5=35 ,将35入栈;最后是-运算符,11算出 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)依次弓t出S2中的元素并输出,结果即为中缀表达式对应的前缀表达式。例如,将中缀表达式“ 1+(2+3) X 45”转换为前缀表达式的过程如下:扫描到的元素S2(栈底- 栈顶)S1 (栈底- 栈顶)说明55空数字,直接入栈-5-S1为空,运算符直接入栈)5- )右括号直接

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

6、从左至右:从左至右扫描表达式,遇到数字时,将数字压入堆栈,遇到运算符时,弹出栈顶的两个数,用运算符对它们做相应的计算(次顶元素op栈顶元素),并将结果入栈;重复上述过程直到表达式最右端,最后运算得出的值即为表达式的结果。例如后缀表达式 “3 4 + 5 X- 6:(1)从左至右扫描,将 3和4压入堆栈;(2)遇到+运算符,因此弹出4和3 (4为栈顶元素,3为次顶元素,注意与前缀表达式做比较) 计算出3+4的值,得7,再将7入栈;将5入栈;(4)接下来是 咕算符,因此弹出 5和7,计算出7X5=35 ,将35入栈;将6入栈;(6)最后是-运算符,11算出 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) X 45”转换为后缀表达式的过程如下:扫描到的元素S2(栈底- 栈顶)S1 (栈底- 栈顶)说明11空数字,直接入栈+1+S1为空,运算符直接入栈(1+ (左括号,直接入栈(1+ (同上21 2+ (数字+1 2+ ( ( +S1栈顶

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

10、法将中缀表达式转换为前缀表达式(波兰式)、 toReversePolishNotation() 方法则用于将中缀表达式转换为后缀表达式(逆波兰式): 注:下面程序是为了说明上述概念而编写,做了简单的测试package qmk.simple_test;import java.util.Scanner;import java.util.Stack;/* Example of converting an infix-expression to* Polish Notation (PN) or Reverse Polish Notation (RPN).* Written in 2011-8-25*

11、author QiaoMingkui*/public class Calculator public static final String USAGE = = usage =n + input the expressions, and then the program + will calculate them and show the result.n + input bye to exit.n;/* param args*/public static void main(String口 args) System.out.println(USAGE);Scanner scanner = n

12、ew Scanner(System.in);String input =;final String CLOSE_MARK = bye;System.out.println(input an expression:);input = scanner.nextLine();while (input.length() != 0& !CLOSE_MARK.equals(input) System.out.print(Polish Notation (PN):);try toPolishNotation(input); catch (NumberFormatException e) System.out

13、.println(ninput error, not a number.); catch (IllegalArgumentException e) System.out.println(ninput error: + e.getMessage(); catch (Exception e) System.out.println(ninput error, invalid expression.);System.out.print(Reverse Polish Notation (RPN):);try toReversePolishNotation(input); catch (NumberFor

14、matException e) System.out.println(ninput error, not a number.); catch (川egalArgumentException e) System.out.println(ninput error: + e.getMessage(); catch (Exception e) System.out.println(ninput error, invalid expression.);System.out.println(input a new expression:); input = scanner.nextLine();Syste

15、m.out.println(program exits);/* parse the expression , and calculate it.* param input* throws IllegalArgumentException* throws NumberFormatException* /private static void toPolishNotation(String input) throws IllegalArgumentException, NumberFormatException int len = input.length();char c, tempChar;S

16、tack s1 = new Stack();Stack s2 = new Stack();Stack expression = new Stack(); double number;int lastIndex = -1;for (int i=len-1; i=0; -i) c = input.charAt(i);if (Character.isDigit(c) lastIndex = readDoubleReverse(input, i);number =Double.parseDouble(input.substring(lastIndex, i+1);s2.push(number);i =

17、 lastindex;if (int) number = number) expression.push(int) number);elseexpression.push(number); else if (isOperator(c) while (!s1.isEmpty() & s1.peek() !=) &priorityCompare(c, s1.peek() 0) expression.push(s1.peek();s2.push(calc(s2.pop(), s2.pop(),s1.pop(); sl.push(c); else if (c = ) sl.push(c); else

18、if (c = () while (tempChar=s1.pop() != ) expression.push(tempChar);s2.push(calc(s2.pop(), s2.pop(), tempChar);if (s1.isEmpty() throw newIllegalArgumentException(bracket dosent match, missing right bracket ).); else if (c = ) / ignore else throw new 川egalArgumentException(wrong character + c + );whil

19、e (!s1.isEmpty() tempChar = s1.pop();expression.push(tempChar);s2.push(calc(s2.pop(), s2.pop(), tempChar);while (!expression.isEmpty() System.out.print(expression.pop() + );double result = s2.pop();if (!s2.isEmpty()throw new IllegalArgumentException(input is a wrong expression.);System.out.println()

20、;if (int) result = result)System.out.println(the result is + (int) result);elseSystem.out.println(the result is + result);/* parse the expression, and calculate it.* param input* throws IllegalArgumentException* throws NumberFormatException* /private static void toReversePolishNotation(String input)

21、throws IllegalArgumentException, NumberFormatException int len = input.length();char c, tempChar;Stack si = new Stack();Stack s2 = new Stack();double number;int lastindex = -1;for (int i=0; ilen; +i) c = input.charAt(i);if (Character.isDigit(c) | c = .) lastindex = readDouble(input, i);number= Doubl

22、e.parseDouble(input.substring(i,lastindex);s2.push(number);i = lastindex - 1;if (int) number = number) System.out.print(int) number + );else System.out.print(number + ); else if (isOperator(c) while (!s1.isEmpty()& s1.peek() !=(& priorityCompare(c, s1.peek()=0; -i) c = input.charAt(i); if (c = .) if

23、 (dotIndex != -1)throw new IllegalArgumentException( therehave more than 1 dots in thenumber.); elsedotIndex = i; else if (!Character.isDigit(c) return i + 1; else if (i = 0) return 0; throw new IllegalArgumentException(not a number.); /* read the next number* param input* param start* return* throw

24、s IllegalArgumentException*/private static int readDouble(String input, int start) throws IllegalArgumentException int len = input.length();int dotIndex = -1;char c;for (int i=start; i 1) return i;elsethrow new IllegalArgumentException(not a number, dot cant be the last part of a number.); else if (

25、i = len - 1) return len;throw new IllegalArgumentException(not a number.); /* return true if the character is an operator.* param c* return* /private static boolean isOperator(char c) return (c=+ | c=- | c=* | c=/);下面是程序运行结果(绿色为用户输入):=usage =input the expressions, and then the program will calculate

26、 them and show the result.input bye to exit.input an expression:3.8+5.3Polish Notation (PN):+ 3.8 5.3the result is 9.1Reverse Polish Notation (RPN):3.8 5.3 +the result is 9.1input a new expression:5*(9.1+3.2)/(1-5+4.88)Polish Notation (PN):/ * 5 + 9.1 3.2 + - 1 5 4.88the result is 69.88636363636364Reverse Polish Notation (RPN):5 9.1 3.2 + * 1 5 - 4.88 + /the result is 69.88636363636364input 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 exits文案大全

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

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1