大数问题

大数相加

 // 只是判断了正整数的情况
public String bigNumberAdd(String f, String s) {  
        //翻转两个字符串,并转换成数组 
        char[] a = new StringBuffer(f).reverse().toString().toCharArray();  
        char[] b = new StringBuffer(s).reverse().toString().toCharArray();  
        int lenA = a.length;  
        int lenB = b.length;  
        //计算两个长字符串中的较长字符串的长度 
        int len = lenA > lenB ? lenA : lenB;  
        //存储结果
        int[] result = new int[len + 1];  
        for (int i = 0; i < len + 1; i++) {  
            //如果当前的i超过了其中的一个,就用0代替,和另一个字符数组中的数字相加 
            int aint = i < lenA ? (a[i] - '0') : 0;  
            int bint = i < lenB ? (b[i] - '0') : 0;  
            result[i] = aint + bint;  
        }  
        //处理进位
        //处理结果集合,如果大于等于10的就向前一位进位,本身进行除10取余 
        for (int i = 0; i < result.length; i++) {  
            if (result[i] >= 10) {  
                result[i + 1] += result[i] / 10;  
                result[i] %= 10;  
            }  
        }  
        StringBuffer sb = new StringBuffer();  
        //该字段用于标识是否有前置0,如果有就不要存储 
        boolean flag = true;  
        for (int i = len; i >= 0; i--) { 
            if (result[i] == 0 && flag) {  
                continue;  
            } else {  
                flag = false;  
            }  
            sb.append(result[i]);  
        }  
        return sb.reverse().toString();  
    } 

大数相减

//只是判断了正整数的情况
public static String bigNumberSub(String a, String b) {
        if(a == null || b == null || a.equals("") || b.equals(""))
            return null;
        if(a.equals(b))
            return "0";
        char[] aChars = new StringBuilder(a).reverse().toString().toCharArray();
        char[] bChars = new StringBuilder(b).reverse().toString().toCharArray();
        int lenA = aChars.length;
        int lenB = bChars.length;
        int maxLen = lenA > lenB ? lenA : lenB;
        //确定正负
        char sign = '+';
        if(lenA < lenB) {
            sign = '-';
        }else {
            if(lenA == lenB) {
                int i = maxLen - 1;
                while(i >= 0 && aChars[i] == bChars[i]) {
                    i--;
                }
                if(i >= 0 && aChars[i] < bChars[i]) {
                    sign = '-';
                }
            }
        }

        int[] result = new int[maxLen];
        //进行运算,a > b a-b a < b b - a
        for(int i = 0; i < maxLen; i++) {
            int aInt = i < lenA ? (aChars[i] - '0') : 0;
            int bInt = i < lenB ? (bChars[i] - '0') : 0;
            if(sign == '+') {
                result[i] = aInt - bInt;
            }else {
                result[i] = bInt - aInt;
            }
        }
        //处理借位
        for(int i = 0; i < maxLen-1; i++) {
            if(result[i] < 0) {
                result[i+1] -= 1;
                result[i] += 10;
            }
        }

        StringBuilder retBuilder = new StringBuilder();

        boolean flag = true;
        //从高位开始判断
        for(int i = maxLen - 1; i >= 0; i--) {
            if(flag && result[i] == 0){
                continue;
            }else {
                flag = false;
            }
            retBuilder.append(result[i]);
        }
        if(sign == '-') {
            retBuilder.insert(0, '-');
        }
        return retBuilder.toString();
    }

大数相乘



 //A中的第i位与B的第j位之积最终会存放到结果的第i+j位中 public static String bigNumberMultiply(String a, String b) { if(a == null || b == null || a.equals("") || b.equals("")) return null; char signA = a.charAt(0); char signB = b.charAt(0); //判断正负 char sign = '+'; if(signA == '+' || signA == '-') { sign = signA; a = a.substring(1); } if(signB == '+' || signB == '-') { if(sign == signB) { sign = '+'; }else { sign = '-'; } b = b.substring(1); } if(a.equals("0") && b.equals("0")) return "0"; char[] aChars = new StringBuilder(a).reverse().toString().toCharArray(); char[] bChars = new StringBuilder(b).reverse().toString().toCharArray(); int lenA = aChars.length; int lenB = bChars.length; int len = lenA + lenB; //从低位到高位保存数据 int[] result = new int[len]; for(int i = 0; i < lenA; i++) { for(int j = 0; j < lenB; j++) { //相加 result[i + j] += (aChars[i] - '0')*(bChars[j] - '0'); } } for(int i = 0; i < len; i++) { if(result[i] >= 10) { result[i+1] += result[i]/10; result[i] %= 10; } } StringBuilder retStrBuilder = new StringBuilder(); boolean flag = true; for(int i = len - 1; i >= 0; i--) { if(flag && result[i] == 0) { continue; }else { flag = false; } retStrBuilder.append(result[i]); } if(sign == '-') retStrBuilder.insert(0, "-"); return retStrBuilder.toString(); }

你可能感兴趣的:(big-number)