题解 | #表达式求值#

表达式求值

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

太难写了,这是简单题???

import java.util.*;

public class Main{
    public static int eval(int first_num,int second_num,StringBuffer op){
        if(String.valueOf(op).equals("+")){
            return first_num+second_num;
        }else if(String.valueOf(op).equals("-")){
            return first_num-second_num;
        }else if (String.valueOf(op).equals("*")){
            return first_num*second_num;
        }else{
            return first_num/second_num;
        }
    }
    public static ArrayList<StringBuffer> transfer(String line){//将所有数字整合在一起,并且如有负数将-num看做一个整体
        ArrayList<StringBuffer> res = new ArrayList<>();
        StringBuffer num = new StringBuffer();
        for (int i = 0; i < line.length(); i++) {
            if ("0123456789".contains(Character.toString(line.charAt(i)))){
                num.append(line.charAt(i));
            }else{
                if (num.length()>0){
                    res.add(num);
                    num=new StringBuffer();
                }
                if (line.charAt(i)=='-'){
                    if (i==0 || "+-/*(".contains(Character.toString(line.charAt(i-1)))){
                        num.append("-");
                        continue;
                    }
                }
                StringBuffer temp = new StringBuffer(Character.toString(line.charAt(i)));
                res.add(temp);
            }
        }
        if (num.length()>0){
            res.add(num);
        }
        return res;
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String line = sc.nextLine();
            ArrayList<StringBuffer> list = transfer(line);
            Stack<StringBuffer> stack_num = new Stack<>();
            Stack<StringBuffer> stack_op = new Stack<>();
        /**
            遍历数字和符号列表list:
            1.如果遇到数字,添加到数字栈stack_num中;
            2.如果遇到*除(,直接添加到符号栈stack_op中;
            3.如果遇到+-号:
                (1).如果符号栈stack_op为空或栈顶元素是左括号(的话,直接入栈;
                (2).如果符号栈stack_op不为空,则不断从符号栈stack_op中弹出一个符号,
                    同时从数字栈stack_n中弹出两个数字进行运算,并将运算结果保存到数字栈stack_num中。
                    期间若遇到左括号(,则跳出循环,最后再将加号+或者减号-添加到符号栈中。
            4.如果遇到右括号),在栈顶元素不是左括号(之前,不断地取出数字和符号进行运算,
              同时将结果保存到数字栈stack_num中,最后删除左括号。
        */
            for (StringBuffer elem : list) {
                if (!"+-/*()".contains(elem)){
                    stack_num.push(elem);
                }else if ("/*(".contains(elem)){
                    stack_op.push(elem);
                }else if ("+-".contains(elem)){
                    if(stack_op.size()==0 || String.valueOf(stack_op.peek()).equals("(")){
                        stack_op.push(elem);
                    }else{
                        while (stack_op.size()>0){
                            if (String.valueOf(stack_op.peek()).equals("(")){
                                break;
                            }
                            StringBuffer op = stack_op.pop();
                            int second = Integer.parseInt(String.valueOf(stack_num.pop()));
                            int first = Integer.parseInt(String.valueOf(stack_num.pop()));
                            int eval = eval(first, second, op);
                            StringBuffer buffer = new StringBuffer(String.valueOf(eval));
                            stack_num.push(buffer);
                        }
                        stack_op.push(elem);
                    }
                }else if (String.valueOf(elem).equals(")")){
                    while (!String.valueOf(stack_op.peek()).equals("(")){
                        StringBuffer op = stack_op.pop();
                        int second = Integer.parseInt(String.valueOf(stack_num.pop()));
                        int first = Integer.parseInt(String.valueOf(stack_num.pop()));
                        int eval = eval(first, second, op);
                        StringBuffer buffer = new StringBuffer(String.valueOf(eval));
                        stack_num.push(buffer);
                    }
                    stack_op.pop();
                }
            }
            while (stack_op.size()>0){
                StringBuffer op = stack_op.pop();
                int second = Integer.parseInt(String.valueOf(stack_num.pop()));
                int first = Integer.parseInt(String.valueOf(stack_num.pop()));
                int eval = eval(first, second, op);
                StringBuffer buffer = new StringBuffer(String.valueOf(eval));
                stack_num.push(buffer);
            }
            System.out.println(Integer.parseInt(String.valueOf(stack_num.pop())));
        }

    }}

全部评论

相关推荐

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