题解 | #四则运算#

四则运算

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Stack;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        int pre=-1; //一组数字的第一个
        boolean n_o=false; //false为前一个是数,true为前一个是符号
        //当该字符为-号且前一个为字符时为true,意味着当前这个符号是下一个数的符号
        int f=0; //当前要存入的数为负;
        ArrayList<String> arr= new ArrayList<>();
        for (int i = 0;i < s.length(); i++) {
            if(Character.isDigit(s.charAt(i))&&pre==-1){
                pre=i;
            }
            else if (Character.isDigit(s.charAt(i))) {
                continue;
            }else {
                //创建多位数
                if(pre>=0){
                    String temp=f==1?"-":"";
                    for (int j = pre; j <i; j++) {
                        temp+=s.charAt(j);
                    }
                    arr.add(temp);
                    n_o=false;
                    f=0;
                }
                if(n_o&&s.charAt(i)=='-') f=1;
                else {
                    if(s.charAt(i)=='{'||s.charAt(i)=='[') {
                        arr.add("(");
                        n_o = true;
                    }
                    else if(s.charAt(i)=='}'||s.charAt(i)==']')
                        arr.add(")");
                    else
                        arr.add(""+s.charAt(i));

                    pre=-1;
                }
            }
       }
        //当最后元素为数字时:
        if(pre>=0){
            String temp=f==1?"-":"";
            for (int j = pre; j <s.length(); j++) {
                temp+=s.charAt(j);
            }
            arr.add(temp);
        }

     // arr.forEach(s1-> System.out.print(s1+" "));
      ArrayList<String> post=inToPost(arr);
    //    System.out.println(" ");
    // post.forEach(s2-> System.out.print(s2+" "));
     //   System.out.println(" ");
      System.out.println(cal(post));
    }

    public static ArrayList<String> inToPost(ArrayList<String> s){
        HashMap<String,Integer> hm =new HashMap<>();
        hm.put("+",1);
        hm.put("-",1);
        hm.put("*",2);
        hm.put("/",2);
        Stack<String> op= new Stack<>();

        ArrayList<String> post =new ArrayList<>();

        for (int i = 0; i < s.size(); i++) {
            if(s.get(i).length()==1){ //为字符或者为个位数
                if(isNumeric(s.get(i))){
                    post.add(s.get(i)); //一位数的数,入后缀
                }else {
                    if(s.get(i).equals("(")){
                        op.push(s.get(i)); //入op栈
                    }else if(s.get(i).equals(")")){
                        String temp=""; //遇到),弹出op内运算符并加入后缀表达式,直到弹出"("为止
                        while (!op.isEmpty() &&!(temp=op.pop()).equals("(")){
                            post.add(temp);
                        }
                    }else { //遇到op,弹出栈中高于或等于当前op优先级的所有运算符,加入post,
                        //直到遇到"("或栈空
                        //并添加至op
                        String temp2 = "";
                        while (!op.isEmpty() && !(temp2 = op.peek()).equals("(")) { //任何一个为false 都停止
                            if (hm.get(s.get(i)) > hm.get(temp2))//当前的优先级大于op顶部的优先级
                                break;
                            post.add(op.pop());
                        }
                        op.push(s.get(i));
                    }
                }
            }else { //两位,一定为数字
                post.add(s.get(i));
            }
        }
        while (!op.isEmpty()){
            post.add(op.pop());
        }

        return post;
    }

    public static boolean isNumeric(String s){
        boolean flag=true;
        for (int i = 0; i < s.length(); i++) {
            if(!Character.isDigit(s.charAt(i)))
                flag=false;
        }
        return flag;
    }

    public static int cal(ArrayList<String> s){
        Stack<Integer> num=new Stack<>();
        for (int i = 0; i < s.size(); i++) {
            if(s.get(i).length()>1||isNumeric(s.get(i))) //大于1的均为多位数或者为负数
                num.push(Integer.valueOf(s.get(i)));
            else {
                int num2 = num.pop();
                int num1 = num.pop();
                String op = s.get(i);
                int res = 0;
                switch (op) {
                    case "+":
                        res = num1 + num2;
                        break;
                    case "-":
                        res = num1 - num2;
                        break;
                    case "*":
                        res = num1 * num2;
                        break;
                    case "/":
                        res = num1 / num2;
                        break;
                    default:
                        break;
                }
                num.push(res);
            }
        }
        return num.pop();
    }

}

全部评论

相关推荐

07-01 13:37
门头沟学院 Java
steelhead:不是你的问题,这是社会的问题。
点赞 评论 收藏
分享
程序员小白条:这比例牛逼,750:1
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务