中綴運算式轉換為后綴運算式(Java)
博客說明
文章所涉及的資料來自互聯網整理和個人總結,意在于個人學習和經驗匯總,如有什么地方侵權,請聯系本人洗掉,謝謝!
步驟
-
初始化兩個堆疊:運算子堆疊 s1 和儲存中間結果的堆疊 s2
-
從左至右掃描中綴運算式
-
遇到運算元時,將其壓 s2
-
遇到運算子時,比較其與 s1 堆疊頂運算子的優先級:
- 如果 s1 為空,或堆疊頂運算子為左括號“(”,則直接將此運算子入堆疊
- 否則,若優先級比堆疊頂運算子的高,也將運算子壓入 s1;
- 否則,將 s1 堆疊頂的運算子彈出并壓入到 s2 中,再次轉到(4-1)與 s1 中新的堆疊頂運算子相比較;
-
遇到括號時:
- 如果是左括號“(”,則直接壓入 s1
- 如果是右括號“)”,則依次彈出 s1 堆疊頂的運算子,并壓入 s2,直到遇到左括號為止,此時將這一對括號丟棄
-
重復步驟 2 至 5,直到運算式的最右邊
-
將 s1 中剩余的運算子依次彈出并壓入 s2
-
依次彈出 s2 中的元素并輸出,結果的逆序即為中綴運算式對應的后綴運算式
案例
將中綴運算式“1+((2+3)×4)-5”轉換為后綴運算式的程序如下
因此結果為 :"123+4 × +5 –"

代碼
package stack;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* @author guizimo
* @date 2020/4/6 12:25 下午
*/
public class PolandNotation {
public static void main(String[] args) {
//運算式
String suffixExpression = "1+((2+3)*4)-5";
//中綴運算式對應的List
System.out.println("中綴運算式對應的List");
List<String> infixExpressionList = toInfixExpressionList(suffixExpression);
System.out.println(infixExpressionList);
//后綴運算式對應的List
System.out.println("后綴運算式對應的List");
List<String> suffixExpressionList = parseSuffixExpressionList(infixExpressionList);
System.out.println(suffixExpressionList);
//計算逆波蘭運算式
System.out.printf("suffixExpression=%d", calculate(suffixExpressionList));
}
public static List<String> parseSuffixExpressionList(List<String> ls) {
//定義兩個堆疊
Stack<String> s1 = new Stack<String>(); //符號堆疊
List<String> s2 = new ArrayList<String>(); //結果
for (String item : ls) {
//如果是一個數
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();
} 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;
}
//將中綴運算式轉換成list
public static List<String> toInfixExpressionList(String s) {
List<String> ls = new ArrayList<String>();
int i = 0;
String str; //多位數
char c;
do {
//非數字
if ((c = s.charAt(i)) < 48 || (c = s.charAt(i)) > 57) {
ls.add("" + c);
i++;
} else { //數字,但是考慮到多位數
str = "";
while (i < s.length() && (c = s.charAt(i)) >= 48 && (c = s.charAt(i)) <= 57) {
str += c;
i++;
}
ls.add(str);
}
} while (i < s.length());
return ls;
}
//完成對逆波蘭運算式的計算
public static int calculate(List<String> ls) {
Stack<String> stack = new Stack<>();
for (String item : ls) {
//使用正則運算式
if (item.matches("\\d+")) { //匹配多位數
//入堆疊
stack.push(item);
} else {
int num2 = Integer.parseInt(stack.pop());
int num1 = Integer.parseInt(stack.pop());
int res = 0;
if (item.equals("+")) {
res = num1 + num2;
} else if (item.equals("-")) {
res = num1 - num2;
} else if (item.equals("*")) {
res = num1 * num2;
} else if (item.equals("/")) {
res = num1 / num2;
} else {
throw new RuntimeException("運算子有問題");
}
//把結果入堆疊
stack.push("" + res);
}
}
return Integer.parseInt(stack.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 result = 0;
switch (operation) {
case "+":
result = ADD;
break;
case "-":
result = SUB;
break;
case "*":
result = MUL;
break;
case "/":
result = DIV;
break;
default:
System.out.println("不存在");
break;
}
return result;
}
}
感謝
尚硅谷
萬能的網路
以及勤勞的自己
關注公眾號: 歸子莫,獲取更多的資料,還有更長的學習計劃
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/157143.html
標籤:Java
上一篇:Java中的例外
下一篇:完整逆波蘭計算器(Java)
