题解 | #表达式求值#

表达式求值

https://www.nowcoder.com/practice/9566499a2e1546c0a257e885dfdbf30d

package org.example.test.practice.third;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        if (s.startsWith("-")){
            s = "(0-1)*"+s.substring(1);
        }
        s = s.replace("(-", "((0-1)*");
        System.out.println(s);
        Deque<Object> endPrefix = new LinkedList<>();
        Deque<Character> stack = new LinkedList<>();
        Map<Character, Integer> pro = new HashMap<>();
        pro.put('+', 1);
        pro.put('-', 1);
        pro.put('*', 2);
        pro.put('/', 2);
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (Character.isDigit(c)) {
                int n = 0;
                while (Character.isDigit(c)) {
                    n = n * 10 + (c - '0');
                    i++;
                    if (i >= s.length()) {
                        break;
                    }
                    c = s.charAt(i);
                }
                endPrefix.push(n);
                i--;
            } else {
                if (c == '(') {
                    stack.push(c);
                } else if (c == ')') {
                    while (!stack.isEmpty() && stack.peek() != '(') {
                        endPrefix.push(stack.pop());
                    }
                    stack.pop();
                } else {
                    while (!stack.isEmpty() && (stack.peek() != '(' && pro.get(stack.peek()) >= pro.get(c))) {
                        endPrefix.push(stack.pop());
                    }
                    stack.push(c);
                }
            }
        }
        while (!stack.isEmpty()) {
            endPrefix.push(stack.pop());
        }
        Deque<Integer> computStack = new LinkedList<>();
        while (!endPrefix.isEmpty()) {
            Object o = endPrefix.pollLast();
            if (o.toString().equals("+")) {
                int a = computStack.pop();
                int b = computStack.pop();
                computStack.push(a + b);
            } else if (o.toString().equals("-")) {
                int a = computStack.pop();
                int b = computStack.pop();
                computStack.push(b - a);
            } else if (o.toString().equals("/")) {
                int a = computStack.pop();
                int b = computStack.pop();
                computStack.push(b / a);
            } else if (o.toString().equals("*")) {
                int a = computStack.pop();
                int b = computStack.pop();
                computStack.push(a * b);
            } else {
                computStack.push((Integer) o);
            }
        }
        System.out.println(computStack.peek());
    }
}

全部评论

相关推荐

没有offer的小土豆:专业面试一般是分配面试官然后联系你面试 应该是还没给你分配对应面试官
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务