若是計算一個表達式,好比 4+5+6*2,隨着計算器的不一樣,簡單的四功能計算器是30,許多科學計算器知道乘法的優先級高於加法,因此科學答案是21。典型計算順序能夠是計算4+5,存爲臨時變量a,再計算6*2,存爲b,最後計算a+b可得出最後結果。這種操做順序以下:45+62*+java
這種記法就是後綴表達式,其求值的過程就是上面描述的整個過程。那如何生成後綴表達式呢,也就是從中綴表達式轉換爲後綴表達式,能夠藉助於棧來實現,整個步驟以下:spa
好比(1+2)*(3+2)-4/2表達式,其後綴表達式和計算結果爲:.net
Java示例代碼以下:code
import java.util.ArrayList; import java.util.List; import java.util.Stack; /** * MyComputer */ public class MyComputer { public static int computer(String input) { List<String> cutList = cutInput(input); List<String> afterList = getAfterList(cutList); return getResultFromAfterList(afterList); } /** * 根據後綴表達式計算結果 */ private static int getResultFromAfterList(List<String> afterList) { Stack<Integer> stack = new Stack<>(); for (String ele : afterList) { if (isFlag(ele.charAt(0))) { int b = stack.pop(); int a = stack.pop(); stack.push(cal(a, b, ele.charAt(0))); } else { stack.push(Integer.valueOf(ele)); } } if (stack.size() != 1) { throw new StackOverflowError(); } return stack.pop(); } /** * 獲取兩個數的計算結果 */ private static int cal(int a, int b, char flag) { int result = 0; switch (flag) { case '+': { result = a + b; break; } case '-': { result = a - b; break; } case '*': { result = a * b; break; } case '/': { result = a / b; break; } default: { break; } } return result; } /** * 生成後綴表達式 */ private static List<String> getAfterList(List<String> cutList) { List<String> output = new ArrayList<>(); Stack<Character> stack = new Stack<>(); for (String ele : cutList) { char flag = ele.charAt(0); if (isFlag(ele.charAt(0)) || (flag == '(') || (flag == ')')) { // 計算符入棧 if (stack.isEmpty()) { stack.push(flag); } else { // 若是待入棧計算符大於棧頂計算符,則直接入棧;不然出棧直到棧爲空或者待入棧計算符小於棧頂計算符 if (flag == '(') { stack.push(flag); } else if (flag == ')') { while (stack.peek() != '(') { output.add(String.valueOf(stack.pop())); } stack.pop(); } else if (isFlagSmaller(stack.peek(), flag)) { stack.push(flag); } else if (stack.peek() == '(') { stack.push(flag); } else{ do { if (stack.peek() == '(') { break; } output.add(String.valueOf(stack.pop())); } while (!stack.isEmpty() && !isFlagSmaller(stack.peek(), flag)); stack.push(flag); } } } else { // 數字直接添加到輸出中 output.add(ele); } } while (!stack.isEmpty()) { if ((stack.peek() != '(') || (stack.peek() != ')')) { output.add(String.valueOf(stack.pop())); } } return output; } /** * 將字符串以操做符爲分隔符切片 */ private static List<String> cutInput(String input) { List<String> cutList = new ArrayList<>(); boolean running = true; while ((input.length() > 0) && running) { char c = input.charAt(0); if (isFlag(c) || (c == '(') || (c == ')')) { cutList.add(String.valueOf(c)); input = input.substring(1); } else { for (int i = 0; i < input.length(); i++) { char tmpC = input.charAt(i); if (isFlag(tmpC) || (tmpC == '(') || (tmpC == ')')) { cutList.add(input.substring(0, i)); cutList.add(String.valueOf(tmpC)); input = input.substring(i + 1); break; } if (i == input.length() - 1) { cutList.add(input); running = false; } } } } return cutList; } /** * 判斷一個字符是不是操做符 */ private static boolean isFlag(char c) { return (c == '+' || c == '-' || c == '*' || c == '/'); } /** * 第一個操做符優先級是否小於第二個 */ private static boolean isFlagSmaller(char a, char b) { boolean flag = true; switch (a) { case '+': case '-': { if ((b == '+') || (b == '-')) { flag = false; } break; } case '*': case '/': { flag = false; } case '(': { flag = false; } default: { break; } } return flag; } }
表達式樹是由後綴表達式構造而來的,那麼如何構造呢,也是藉助於棧來實現。上述代碼已經實現了從中綴表達式轉換爲爲後綴表達式,整個步驟大體以下:blog