题解 | #表达式求值#

表达式求值

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String line = sc.nextLine();
            //给最后加一位符号,保证结束的适合能处理一次
            int ans = compute(line + '-' );
            System.out.println(ans);
        }
    }

    public static int compute(String line){
        //第一步:定义好存储的变量
        int n = line.length(), p = 0;
        Stack<Character> operators = new Stack<>();//计算符号栈
        Stack<Integer> nums = new Stack<>();//操作数栈
        String numStr = "";
        //第二步:循环处理传入的字符串
        while(p < n){
            char c = line.charAt(p);
            if(Character.isDigit(c)){
                //1、如果遍历到的字符是数字,则将字符加入到数字字符串中
                numStr += c;
            }else{
                //2、如果遍历到的字符不是数字,那么就先处理一下数字字符串
                if(numStr.length()>0){//如果数字字符串不为空,就像操作数栈中压栈
                    nums.push(Integer.valueOf(numStr));
                    numStr = "";
                }
                //接着处理当前的字符,字符的情况有四种
                
                //情况一:字符是加减
                if(c == '+' || c == '-'){
                    //此时 减号 表示负号,则特殊处理一下
                    if(c == '-' && ( p==0 || line.charAt(p-1) == '(' ) ){ 
                        numStr = "-";
                        p++;
                        continue;
                    }
                    while(!operators.isEmpty() && (operators.peek() == '*' || operators.peek() == '/' ||
                            operators.peek() == '+' || operators.peek() == '-')){
                        char top = operators.pop();
                        int num2 = nums.pop();
                        int num1 = nums.pop();
                        nums.push(doCompute(top, num1, num2));
                    }
                    operators.push(c);
                }
                //情况二:字符是乘除,直接入栈
                else if(c == '*' || c == '/'){
                    operators.push(c);
                }
                //情况三:字符是左括号,直接入栈
                else if(c == '('){
                    operators.push(c);
                }
                //情况四:字符是右括号,处理直到匹配到左括号
                else if(c == ')'){
                    while(operators.peek() != '('){
                        char top = operators.pop();
                        int num2 = nums.pop();
                        int num1 = nums.pop();
                        nums.push(doCompute(top, num1, num2));
                    }
                    //注意!情况四处理完需要把左括号弹出来
                    operators.pop();
                }
            }
            //处理完后看下一位
            p++;
        }
        //第三步:把结果数据返回
        return nums.peek();
    }
    
    //计算的方法
    public static int doCompute(char op,int num1, int num2){
        switch(op){
            case '+':
                return num1 + num2;
            case '-':
                return num1 - num2;
            case '*':
                return num1 * num2;
            case '/':
                return num1 / num2;
        }
        return 0;
    }
}
全部评论

相关推荐

小谷围鸡肉卷阿姨:+1,腾子投完一动不动
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务