逆波兰计算器及中缀表达式转后缀表达式

86 阅读1分钟

5.2逆波兰计算器及中缀表达式转后缀表达式

计算后缀表达式的思路

1665048320327.png

中缀表达式转后缀表达式

1665048466944.png

代码实现

package PolandNotation;
​
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
​
public class PolandNotationByStack {
    public static void main(String[] args) {
        String suffixExpression = "10 6 9 3 + -11 * / * 17 + 5 +";
        String infixExpression = "1+((222+31)*4)-5";
        List<String> sufList = getListByString(suffixExpression);
//        List<String> list = changeInfixExpressionToList(infixExpression);
//        List<String> list1 = changeInfixListToSuffixList(list);
//        int res = calculate(list1);
        int res1 = calculate(sufList);
//        System.out.println(res);
        System.out.println(res1);
    }
    public static List<String> getListByString(String poland) {
        List<String> list = new ArrayList<>();
        String[] split = poland.split(" ");
        for(String ele: split) {
            list.add(ele);
        }
        return list;
    } // 将后缀字符串转化为列表
    public static int calculate(List<String> list) {
        Stack<String> stack = new Stack<>();
        int num1 = 0;
        int num2 = 0;
        for(String item: list) {
            if (item.matches("[-+]?\d+(?:\.\d+)?")) { // 匹配多位数
                stack.push(item);
            } else {
                num2 = Integer.parseInt(stack.pop());
                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 {
                    System.out.println("非法字符");
                }
                stack.push("" + res);
            }
        }
        return Integer.parseInt(stack.pop());
    } // 计算后缀表达式
    public static List<String> changeInfixExpressionToList(String str) { // 将中缀表达式转化成列表
        List<String> ls = new ArrayList<>();
        int i = 0;
        String mulNumber;
        char ch;
        do{
            if((ch=str.charAt(i)) < 48 || (ch=str.charAt(i)) > 57) {
                ls.add("" + ch);
                i++;
            } else {
                mulNumber = "";
                while ((i < str.length()) && ((ch=str.charAt(i)) >= 48) && ((ch=str.charAt(i)) <= 57)) {
                    mulNumber += ch;
                    i++;
                }
                ls.add(mulNumber);
            }
        }while (i < str.length());
        return ls;
    } // 将中缀字符串转化成中缀列表
    public static List<String> changeInfixListToSuffixList(List<String> ls) {
        Stack<String> stack = new Stack<>();
        List<String> list = new ArrayList<>();
        int num1 = 0;
        int num2 = 0;
        String opera;
        for(String str : ls) {
            if(str.matches("\d+")) {
                list.add(str);
            } else if (str.equals("(")) {
                stack.push(str);
            } else if (str.equals(")")) {
                while(!stack.peek().equals("(")) {
                    list.add(stack.pop());
                }
                stack.pop();
            } else {
                while (stack.size() != 0 && operaPriority(str) <= operaPriority(stack.peek())) {
                    list.add(stack.pop());
                }
                stack.push(str);
            }
        }
        while (stack.size() != 0) {
            list.add(stack.pop());
        }
        return list;
    } // 将中缀列表转化成后缀列表
    public static int operaPriority (String str) {
        int res;
        if (str.equals("+") || str.equals("-")) {
            res = 1;
        } else if (str.equals("*") || str.equals("/")) {
            res = 2;
        } else {
            res = 0;
        }
        return res;
    } // 返回运算符的优先级
};

\