字符串解析运算符加减法(不使用BIgInterge)的方法,麻烦帮我看下

今天写一个字符串加减法(不使用BIgInterge)的方法,麻烦大家帮我看下有无问题:

public class StringCalculator {
    public static void main(String[] args) {
        // test
        System.out.println(parseStrAri("99+1+99+1-99="));
    }

    // 解析加减运算
    private static String parseStrAri(String dest) {
        // 运算数组
        String[] nums = dest.split("\\+|-|=");
        //运算符号
        char[] ops = dest.replaceAll("[0-9]", "").toCharArray();
        // 校验参数
        if (nums.length == 0 || ops.length == 0) {
            throw new IllegalArgumentException("输入参数不正确,不符合表达式");
        }
        int numIndex = 0;
        String result = nums[0];
        for (char c : ops) {
            switch (c) {
                case '+':
                    // -result +b 按照 -(a-b) 处理  -1000+ 20000
                    if ("-".equals(result.substring(0,1))){
                        String numA =  expunge(result);
                        String numB =  nums[++numIndex];
                        if (isLeftBig(numA,numB)){
                            result = subtractionStr(numA, numB);
                            result = "-"+result;
                        }else {
                            result = subtractionStr(numB, numA);
                        }
                    }else {
                        result = additionStr(result, nums[++numIndex]);
                    }
                    break;
                case '-':
                    String numB =  nums[++numIndex];
                    if ("-".equals(result.substring(0,1))){
                        String numA = expunge(result) ;
                        result = additionStr(numA, numB);
                        result = "-"+result;
                    } else {
                        String numA = result ;
                        if (isLeftBig(numA,numB)){
                            result = subtractionStr(numA, numB);
                        }else {
                            result = subtractionStr(numB, numA);
                            result = "-"+result;
                        }
                    }
                    break;
                case '=':
                    return result;// 这里返回结果
                default:
                    // ignore
            }
        }

        return result;
    }

    // add 加法
    private static String additionStr(String a, String b) {
        StringBuilder builder = new StringBuilder();
        a = reverse(a);
        b = reverse(b);
        // 调换
        if (a.length() < b.length()) {
            String c = b;
            b = a;
            a = c;
        }
        int aLen = a.length();
        int bLen = b.length();

        boolean isOver = false;
        for (int i = 0; i < aLen; i++) {
            int result = 0;
            int charA = a.charAt(i) - 48;
            if (i < bLen) {
                int charB = b.charAt(i) - 48;
                result = charA + charB;
            } else {
                result = result + charA;
            }
            // 进位
            if (isOver) {
                result++;
            }
            if (result >= 10) {
                builder.append(result - 10);
                isOver = true;
            } else {
                builder.append(result);
                isOver = false;
            }
        }
        if (isOver){
            builder.append("1");
        }
        return reverse(builder.toString());
    }

    // sub 减法
    private static String subtractionStr(String a, String b) {
        StringBuilder builder = new StringBuilder();
        a = reverse(a);
        b = reverse(b);
        // 调换
        if (a.length() < b.length()) {
            throw new IllegalArgumentException("字符串有误");
        }

        int aLen = a.length();
        int bLen = b.length();
        byte isOver = 0;//  借位

        for (int i = 0; i < aLen; i++) {
            int result = 0;
            int charA = a.charAt(i) - 48;
            result = result + isOver;
            isOver = 0;
            if (i < bLen) {
                int charB = b.charAt(i) - 48;
                result = result + charA;
                if (result < 0) {
                    result += 10;
                    isOver--;
                }
                result = result - charB;
                if (result < 0) {
                    result += 10;
                    isOver--;
                }
            } else {
                result = charA + result;
                if (result < 0) {
                    result += 10;
                    isOver--;
                }
            }
            builder.append(result);
        }
        return expungeZero(reverse(builder.toString()));
    }

    // 反序
    private static String reverse(String value) {
        if (value == null || value.length() <= 1) {
            return value;
        }
        return reverse(value.substring(1)) + value.charAt(0);
    }

    // 取零的前缀
    private static String expungeZero(String s) {
        return s.replaceFirst("^0*", "");
    }

    private static String expunge(String s){
        return s.replaceFirst("^-*", "");
    }

    private static boolean isLeftBig(String a,String b){
        if (a.length() == b.length()){
            return a.compareTo(b)>0;
        }
        if (a.length() > b.length()){
            return true;
        }
        return false;
    }

}

 

posted @ 2019-07-18 20:27  Immer  阅读(431)  评论(0编辑  收藏  举报