题解 | #表达式求值#

表达式求值

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

import java.util.Scanner;
import java.lang.StringBuilder;
import java.util.Stack;
import java.lang.Math;
// 注意类名必须为 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 input = in.nextLine();
            int length = input.length();
            Stack<Character> operator = new Stack<>(); //运算符栈
            Stack<Integer> number = new Stack<>(); //操作数栈
            for (int i = 0; i < length; i++) {
                char temp = input.charAt(i);
                if (temp == '(') { //左括号直接入栈
                    operator.push(temp);
                } else if (temp == ')') {
                    while (operator.peek() != '(' && !operator.empty()) { //右括号出栈栈顶运算符,直至遇到左括号为止
                        int num1 = number.pop();
                        int num2 = number.pop();
                        int result = arithmetic(num1, num2, operator.pop());
                        number.push(result);
                    }
                    operator.pop();
                } else if (temp == '+' || temp == '-') {//加减号只有遇到左括号或者栈空入栈,栈顶为其余运算符时均出栈
                    if (operator.empty()) {
                        operator.push(temp);
                    } else if (!operator.empty() && (operator.peek() == '(')) {
                        operator.push(temp);
                    } else {
                        while(!operator.empty()&&(operator.peek() == '*'||operator.peek() == '/'||operator.peek() == '+'||operator.peek() == '-')){
                            int num1 = number.pop();
                            int num2 = number.pop();
                            int result = arithmetic(num1,num2,operator.pop());
                            number.push(result);
                        }
                        operator.push(temp);
                    }
                } else if (temp == '*' || temp == '/') {//乘除号遇到加减号,左括号,栈空时入栈,遇到乘除号时出栈栈顶元素
                    if (operator.empty()) {
                        operator.push(temp);
                    } else if (!operator.empty() && ( operator.peek() == '(' || operator.peek() == '+' || operator.peek() == '-')) {
                        operator.push(temp);
                    } else{
                        while(!operator.empty()&&(operator.peek() == '*'||operator.peek() == '/')){
                            int num1 = number.pop();
                            int num2 = number.pop();
                            int result = arithmetic(num1,num2,operator.pop());
                            number.push(result);
                        }
                        operator.push(temp);                        
                    }
                } else if(Character.isDigit(temp)){
                        StringBuilder sb = new StringBuilder();
                        boolean isNegative = false;//判断是否是负数
                        if(i-1 == 0&&input.charAt(i-1) == '-'){ 
                            isNegative = true;
                            operator.pop();
                        }else if(i-1 > 0 && i-2 > 0&&input.charAt(i-1) == '-'&&input.charAt(i-2) == '('){
                            isNegative = true;
                            operator.pop();                           
                        }
                        while(Character.isDigit(input.charAt(i))){//合并数字
                            sb.append(input.charAt(i));
                            if(i < length - 1 && Character.isDigit(input.charAt(i+1))){
                                i++;
                            }else{
                                break;
                            }
                        }
                        Integer tempNumber = Integer.valueOf(sb.toString());
                        //System.out.println(tempNumber);
                        if(isNegative == true){
                            tempNumber = -tempNumber;
                            isNegative = false;
                        }
                        number.push(tempNumber);                         
                }
            }
            while(!operator.empty()){ //所有字符都经过入栈操作后将运算符栈全都出栈
                int num1 = number.pop();
                int num2 = number.pop();
                char op = operator.pop();
                int result = arithmetic(num1,num2,op);
                number.push(result);
            }
            System.out.println(number.pop());
            return;
        }
    }
    public static int arithmetic(int n1,int n2,char op){
        switch(op){
            case '+':
            return n2+n1;
            case '-':
            return n2-n1;
            case '*':
            return n2*n1;
            case '/':
            return n2/n1;
        }
        return 0;
    }
}

全部评论

相关推荐

11-28 17:48
中山大学 C++
点赞 评论 收藏
分享
M_bao:换个排版吧哥们,看着费劲
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务