题解 | #四则运算#

四则运算

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

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

// 注意类名必须为 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.next() + '$';
            Deque<Integer> numStack = new ArrayDeque<>();
            Deque<Character> opStack = new ArrayDeque<>();
            int current = 0;
            while (current != expr.length()) {
                char ch = expr.charAt(current);
                switch (ch) {
                    case '+':
                    case '-':
                        if (current == 0 || expr.charAt(current - 1) == '(' || expr.charAt(current - 1) == '['
                                || expr.charAt(current - 1) == '{') {
                            // 解析为数字
                            int len = 1;
                            while (Character.isDigit(expr.charAt(current + len))) {
                                len++;
                            }
                            int num = Integer.parseInt(expr.substring(current, current + len));
                            numStack.push(num);
                            current += len;
                        } else {
                            // 解析为运算符
                            while (!opStack.isEmpty() && (opStack.peek() == '*' || opStack.peek() == '/'
                                    || opStack.peek() == '+' || opStack.peek() == '-')) {
                                char op = opStack.poll();
                                int num2 = numStack.poll();
                                int num1 = numStack.poll();
                                numStack.push(compute(num1, op, num2));
                            }
                            opStack.push(ch);
                            current++;
                        }
                        break;
                    case '*':
                    case '/':
                        while (!opStack.isEmpty() && (opStack.peek() == '*' || opStack.peek() == '/')) {
                            char op = opStack.poll();
                            int num2 = numStack.poll();
                            int num1 = numStack.poll();
                            numStack.push(compute(num1, op, num2));
                        }
                        opStack.push(ch);
                        current++;

                        break;
                    case '(':
                    case '{':
                    case '[':
                        opStack.push('(');
                        current++;
                        break;
                    case ')':
                    case '}':
                    case ']':
                        while (opStack.peek() != '(') {
                            char op = opStack.poll();
                            int num2 = numStack.poll();
                            int num1 = numStack.poll();
                            numStack.push(compute(num1, op, num2));
                        }
                        opStack.poll(); // 弹出 '('
                        current++;
                        break;
                    case '$':
                        // 结束符号
                        while (!opStack.isEmpty()) {
                            char op = opStack.poll();
                            int num2 = numStack.poll();
                            int num1 = numStack.poll();
                            numStack.push(compute(num1, op, num2));
                        }
                        current++;
                        break;
                    default:
                        // 解析数字
                        int len = 1;
                        while (Character.isDigit(expr.charAt(current + len))) {
                            len++;
                        }
                        int num = Integer.parseInt(expr.substring(current, current + len));
                        numStack.push(num);
                        current += len;
                }
            }
            System.out.println(numStack.poll());
        }
    }


    private static int compute(int num1, char op, int num2) {
        switch (op) {
            case '+':
                return num1 + num2;
            case '-':
                return num1 - num2;
            case '*':
                return num1 * num2;
            case '/':
                return num1 / num2;
        }
        throw new RuntimeException();
    }
}

全部评论

相关推荐

牛客771574427号:恭喜你,华杰
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务