中缀转后缀表达式,带括号的后缀表达式综合计算器,Java栈数据结构实现
生活随笔
收集整理的這篇文章主要介紹了
中缀转后缀表达式,带括号的后缀表达式综合计算器,Java栈数据结构实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 中綴表達式轉后綴表達式思路
- 逆波蘭表達式計算思路
- 代碼實現
中綴表達式轉后綴表達式思路
1、初始化兩個棧:運算符棧s1和儲存中間結果的棧s2
2、從左至右掃描中綴表達式
3、遇到操作數時,將其壓入s2
4、遇到運算符時,比較其與s1棧頂運算符的優先級
①如果s1為空,或棧頂運算符為左括號“(”, 則直接將此運算符入棧s1
②否則,若優先級比棧頂運算符的高,也將運算符壓入s1
③否則,將s1棧頂的運算符彈出并壓入到s2中,再次轉到(4.1)
5、遇到括號時
①如果是左括號“(”,則直接壓入s1
②如果是右括號“)”,則依次彈出s1棧頂的運算符,并壓入s2,直到遇到左括號為
止,此時將這一對括號丟棄
6、重復步驟2至5,直到表達式結束
7、將s1中剩余的運算符依次彈出并壓入s2
8、依次彈出s2中的元素并輸出,結果的逆序即為中綴表達式對應的后綴表達式
逆波蘭表達式計算思路
(3+4)*5-6 對應的后綴表達式為 3 4 + 5 * 6 -,針對后綴表達式求值步驟如下:
1、從左至右掃描,將3和4壓入堆棧
2、遇到+運算符,彈出4和3 (4 為棧頂元素,3為次頂元素),計算出3+4的值,得 7,再將7入棧
3、將5入棧
4、接下來是運算符,彈出5和7,計算出75=35,將35入棧
5、將6入棧
6、最后是 - 運算符,彈出35和6,計算35-6=29, 由此得出最終結果
代碼實現
import java.util.ArrayList; import java.util.List; import java.util.Stack;/*** @Author: Yeman* @Date: 2021-10-27-21:59* @Description:*/ public class PolandNotation {public static void main(String[] args) {//給一個中綴表達式String expression = "1+((2+3)*4)-10";//將中綴表達式放入ListList<String> infixList = toInfixExpression(expression);//將中綴表達式對應的List轉換為逆波蘭表達式對應的ListList<String> suffixList = parseSuffixExpression(infixList);//用逆波蘭表達式(后綴表達式)進行計算int result = calculate(suffixList);System.out.println(result);}//將中綴表達式放入List中public static List<String> toInfixExpression(String expression){List<String> ls = new ArrayList<>();int i = 0; //相當于一個指針,用來遍歷表達式String str; //用來拼接多位數char ch; //每遍歷一個,就存入chdo {//如果不是數,直接加入if ((ch = expression.charAt(i)) < 48 || (ch = expression.charAt(i)) > 57){ls.add(ch + "");i++;}else {str = "";while (i < expression.length() && (ch = expression.charAt(i)) >= 48 && (ch = expression.charAt(i)) <= 57){str += ch;i++;}ls.add(str);}}while (i < expression.length());return ls;}//將中綴表達式對應的List轉成逆波蘭表達式對應的Listpublic static List<String> parseSuffixExpression(List<String> infixList){Stack<String> s1 = new Stack<String>(); //符號棧//由于操作中沒有進行過pop,可以使用List<String>替換中間結果棧Stack<String>List<String> s2 = new ArrayList<String>();for (String item : infixList){if (item.matches("\\d+")){s2.add(item);}else if (item.equals("(")){s1.push(item);}else if (item.equals(")")){while (!s1.peek().equals("(")){s2.add(s1.pop());}s1.pop(); //將 ( pop出去}else {while (s1.size() != 0 && Operation.getValue(s1.peek()) >= Operation.getValue(item)){s2.add(s1.pop());}s1.push(item);}}while (s1.size() != 0){s2.add(s1.pop());}return s2;}//逆波蘭表達式計算public static int calculate(List<String> expressionList){//創建一個棧Stack<String> strings = new Stack<String>();//遍歷列表for (String item : expressionList){if (item.matches("\\d+")){ //匹配多位數strings.push(item);}else {int num2 = Integer.parseInt(strings.pop());int num1 = Integer.parseInt(strings.pop());String ch = item;int res = 0;switch (ch){case "+":res = num1 + num2;break;case "-":res = num1 - num2;break;case "*":res = num1 * num2;break;case "/":res = num1 / num2;break;default:throw new RuntimeException("運算符不正確!");}strings.push(res + "");}}//for循環結束留在棧里的即為計算結果return Integer.parseInt(strings.pop());} }//該類返回運算符優先級 class Operation{private static int ADD = 1;private static int SUB = 1;private static int MUL = 2;private static int DIV = 2;public static int getValue(String operation){int res = 0;switch (operation){case "+":res = ADD;break;case "-":res = SUB;break;case "*":res = MUL;break;case "/":res = DIV;break;default:System.out.println("運算符不正確!");break;}return res;} }總結
以上是生活随笔為你收集整理的中缀转后缀表达式,带括号的后缀表达式综合计算器,Java栈数据结构实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大班教案《自觉排队日》
- 下一篇: Java实现最小二乘法线性拟合,传感与检