题解 | #四则运算#

四则运算

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



import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.*;


public class Main {
    public static void main(String[] args) throws IOException {

        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
        String input;
        while ((input = read.readLine()) != null) {
            System.out.println(getResultResult(input));

        }



    }

    static  ArrayList<String> list  = new ArrayList<String>() {
        {
            add("(");
            add(")");
            add("[");
            add("]");
            add("{");
            add("}");
            add("+");
            add("-");
            add("*");
            add("/");
        }
    };
    static int[] temp = new int[2];
    public static  String getResultResult(String str) {
        if (str.contains("(")) {
            str = handleBrackets(str, "(", ")");

        }
        if (str.contains("[")) {
            str = handleBrackets(str, "[", "]");
        }
        if (str.contains("{")) {
            str = handleBrackets(str, "{", "}");
        }
        if (str.contains("*")) {
            str = getArrayMul(str, "*");
        }
        if (str.contains("/")) {
            str = getArrayMul(str, "/");
        }
        if (isNumberLessChar(str)) {
            str = str.replace("--", "+");
        }
        if (str.contains("-")) {
            str = str.replace("-", "+-");
        }
        if (str.contains("+")) {
            String[] split = str.split("\\+");
            if (split.length < 2) return str;

            int res = 0;

            split = Arrays.stream(split).filter(s -> !Objects.equals(s,
                                                "")).toArray(String[]::new);

            for ( int i = 0; i < split.length - 1; i++) {
                res = Integer.parseInt(split[i]) + Integer.parseInt(split[i + 1]);
                split[i + 1] = res + "";
            }
            if (split.length == 1) {
                res = Integer.parseInt(split[0]);
            }
            return res + "";
        }

        return str;
    }

    private static String getArrayMul(String str, String brackets) {
        int[] array = new int[2];
        while (isContainsList(str) && str.contains(brackets)) {
            int indexOf = str.indexOf(brackets);
            for (int i = indexOf + 1; i < str.length(); i++) {
                if ((str.charAt(i - 1) + "").equals(brackets) && str.charAt(i) == '-')continue;
                if (i == str.length() - 1) {
                    array[1] = Integer.parseInt(str.substring(indexOf + 1));
                    temp[1] = i + 1;
                    break;
                }
                if (list.contains(str.charAt(i) + "")) {
                    array[1] = Integer.parseInt(str.substring(indexOf + 1, i));
                    temp[1] = i;
                    break;
                }
            }
            for (int i = indexOf - 1; i >= 0; i--) {

                if (list.contains(str.charAt(i) + "")) {
                    array[0] = Integer.parseInt(str.substring(i + 1, indexOf));
                    temp[0] = i + 1;
                    break;
                }
                if (i == 0) {
                    array[0] = Integer.parseInt(str.substring(0, indexOf));
                    temp[0] = i;
                    break;
                }
            }
            BigDecimal res;
            if (brackets.equals("*")) {
                res = BigDecimal.valueOf((long) array[0] * array[1]);
            } else {
                res = BigDecimal.valueOf(array[0] / array[1]);
            }
            String substring = str.substring(temp[0], temp[1]);
            str = str.replace(substring, res + "");
        }

        return str;

    }

    public static Boolean isContainsList(String string) {
        for (String s : list) {
            if (string.contains(s)) {
                return true;
            }
        }
        return false;
    }
    public static String handleBrackets(String str, String brackets,
                                        String brackets2) {

        //TODO ()()和(())根据栈拿下标
        while (isContainsList(str) && str.contains(brackets2)) {
            int[] indexOf = getIndexOfBrackets(str, brackets, brackets2);
            /*  int indexOf_first = str.indexOf(brackets);
              int indexOf_second = str.lastIndexOf(brackets2);*/
            int indexOf_first = indexOf[0];
            int indexOf_second = indexOf[1];
            String substring = str.substring(indexOf_first + 1, indexOf_second);
            String resultResult = getResultResult(substring);
            str = str.replace(brackets + substring + brackets2, resultResult);
        }

        return str;
    }

    private static int[] getIndexOfBrackets(String str, String brackets,
                                            String brackets2) {
        int[] index = new int[2];
        boolean b = true;
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == brackets.charAt(0)) {
                stack.push(str.charAt(i));
                if (b) {
                    index[0] = i;
                    b = !b;
                }
                continue;
            }
            if (str.charAt(i) != brackets2.charAt(0)) {
                continue;
            }
            if (!stack.isEmpty() && stack.peek() == brackets.charAt(0) &&
                    str.charAt(i) == brackets2.charAt(0)) {
                stack.pop();
            }
            if (stack.isEmpty() && !b) {
                index[1] = i;
                break;
            }
        }
        return index;
    }

    public static Boolean isNumberLessChar(String str) {
        int charNumber = 0;
        int number = 0;
        String[] splitDeduce = str.split("-");
        for (String s : splitDeduce) {
            if (s.contains("\\+")) {
                number  = number + s.split("\\+").length;
            } else if (s.isEmpty()) {

            } else {
                number++;
            }
        }
        for (int i = 0; i < str.length(); i++) {
            if (list.contains(str.substring(i, i + 1))) {
                charNumber++;
            }
        }
        return charNumber >= number;
    }
}


全部评论

相关推荐

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