题解 | #四则运算#

四则运算

https://www.nowcoder.com/practice/9999764a61484d819056f807d2a91f1e

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String expr = in.nextLine();
            expr = expr.replace("[", "(");
            expr = expr.replace("]", ")");
            expr = expr.replace("{", "(");
            expr = expr.replace("}", ")");
            int ans = cal("(" + expr + ")");
            System.out.println(ans);
        }
    }

    private static int cal (String expr) {
        Stack<Operate> stack = new Stack<>();
        int n = expr.length();
        for (int i = 0; i < n; i++) {
            char cur = expr.charAt(i);
            if (cur == '(') {
                // 说明是开始
                stack.push(new Operate(cur));
            } else if (cur == ')') {
                // 需要计算这个括号内的值
                int temp = 0;
                while (!stack.isEmpty() && stack.peek().c != '(') {
                    temp += back(stack);
                }
                if (!stack.isEmpty()) {
                    stack.pop();
                }
                temp = back(stack, temp);
                stack.push(new Operate(temp));
            } else if (cur == '+' || cur == '-' || cur == '*' || cur == '/') {
                stack.push(new Operate(cur));
            } else {
                int temp = 0;
                while (expr.charAt(i) >= '0' && expr.charAt(i) <= '9') {
                    temp = temp * 10 + (expr.charAt(i) - '0');
                    i++;
                }
                i--;
                temp = back(stack, temp);
                stack.push(new Operate(temp));
            }
        }
        return stack.peek().val;
    }

    /**
     * 向前计算 + -
     */
    private static int back(Stack<Operate> stack) {
        int temp = 0;
        Operate value = stack.pop();
        Operate op = stack.peek();
        char cur = op.c;
        if (op.c == '(') {
            cur = '+';
        } else {
            stack.pop();
        }
        if (cur == '+') {
            temp += value.val;
        } else if (cur == '-') {
            temp -= value.val;
        }
        return temp;
    }
    /**
     * 向前计算 * /
     */
    private static int back (Stack<Operate> stack, int temp) {
        while (!stack.isEmpty() && (stack.peek().c == '*' || stack.peek().c == '/')) {
            Operate op = stack.pop();
            if (op.c == '*') {
                temp = stack.pop().val * temp;
            } else {
                temp = stack.pop().val / temp;
            }
        }
        return temp;
    }

    private static class Operate {
        int val;
        char c;
        public Operate (int val) {
            this.val = val;
        }
        public Operate (char cur) {
            this.c = cur;
        }
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务