Discuss / Java / 代码

代码

Topic source

可姆可汗

#1 Created at ... [Delete] [Delete and Lock User]
import java.util.*;public class Suffix {    public static void main(String[] args) {        String exp = "x + 2 * (y - 5)";        SuffixExpression se = compile(exp);        Map<String, Integer> env = Map.of("x", 1, "y", 9);        int result = se.execute(env);        System.out.println(exp + " = " + result + " " + (result == 1 + 2 * (9 - 5) ? "✓" : "✗"));    }    static SuffixExpression compile(String exp) {        // TODO:        SuffixExpression se = new SuffixExpression(exp);        se.compile();        return se;    }}class SuffixExpression {    private String infix = "";    private String suffix = "";    private Deque<String> opStack = new LinkedList<>();    public SuffixExpression(String infix) {        this.infix = infix;    }    private void opPop() {        while (!opStack.isEmpty() && !opStack.peek().equals("(")) {            suffix = suffix.concat(opStack.pop() + " ");        }    }    public void compile() {        Map<String, Integer> priority = Map.of("(", 1, "+", 2, "-", 2, "*", 3, "/", 3, ")", 4);        int length = infix.length();        int i = 0;        while (i < length) {            if (infix.charAt(i) == ' ') {                i++;                continue;            } else if (infix.charAt(i) == '(') {                opStack.push(String.valueOf('('));            } else if (infix.charAt(i) == ')') {                opPop();                opStack.pop();            } else if (infix.charAt(i) == '+' || infix.charAt(i) == '-' || infix.charAt(i) == '*' || infix.charAt(i) == '/') {                String operator = String.valueOf(infix.charAt(i));                if (!opStack.isEmpty()) {                    String topOp = opStack.peek();                    if (priority.get(operator) <= priority.get(topOp)) {                        opPop();                    }                }                opStack.push(operator);            } else {                int begin = i;                for (; i + 1 < length && infix.charAt(i + 1) != ' ' && infix.charAt(i + 1) != ')'; i++);                int end = i + 1;                suffix = suffix.concat(infix.substring(begin, end) + " ");            }            i++;        }        opPop();        suffix = suffix.substring(0, suffix.length() - 1);    }    int execute(Map<String, Integer> env) {        // TODO:        Deque<Integer> stack = new LinkedList<>();        int length = suffix.length();        int i = 0;        while (i < length) {            if (suffix.charAt(i) == ' ') {                i++;                continue;            } else if (suffix.charAt(i) == '+' || suffix.charAt(i) == '-' || suffix.charAt(i) == '*' || suffix.charAt(i) == '/') {                char op = suffix.charAt(i);                int num1 = stack.pop();                int num2 = stack.pop();                int num = 0;                if (op == '+') {                    num = num2 + num1;                } else if (op == '-') {                    num = num2 - num1;                } else if (op == '*') {                    num = num2 * num1;                } else if (op == '/') {                    num = num2 / num1;                }                stack.push(num);            } else {                int begin = i;                for (; i + 1 < length && suffix.charAt(i + 1) != ' '; i++);                int end = i + 1;                String value = suffix.substring(begin, end);                if (env.containsKey(value)) {                    stack.push(env.get(value));                } else {                    stack.push(Integer.parseInt(value));                }            }            i++;        }        int res = 0;        try {            res = stack.peek();        } catch (Exception e) {            System.out.println("Error!");        }        return res;    }}

  • 1

Reply