java大整数相加

-- 正整数相加

/*
 * description: 1. 反转 2. 补0 3. 计算 4. 最后结果反转
 * @author maduar
 * @date 21/11/2018 10:15 PM
 * @email maduar@163.com
 *
 * */
public class MaxNum {

    private MaxNum() {
    }

    public static String sumMaxNumber(String numberA, String numberB) {

        // 校验空傎
        if (!isAllNum(numberA) || !isAllNum(numberB)) {
            return null;
        }

        // 反转
        numberA = reverseString(numberA);
        numberB = reverseString(numberB);

        String maxString = getMaxLengthString(numberA, numberB);
        String minString = getMinLengthString(numberA, numberB);

        // 补0
        int maxLength = maxString.length();
        int minLength = minString.length();

        if (maxLength > minLength) {
            minString = getMoreZero(minString, maxLength);
        }

        // 获取sum
        return getSumNumber(maxString, minString, maxLength);
    }

    // 判断字符是否为空
    public static boolean isBlank(String str) {
        return str == null || "".equals(str);
    }

    // 反转字符串
    public static String reverseString(String str) {
        return new StringBuilder(str).reverse().toString();
    }

    // 获取长度较大的字符
    public static String getMaxLengthString(String numberA, String numberB) {
        return numberA.length() > numberB.length() ? numberA : numberB;
    }

    // 获取长度较小的字符
    public static String getMinLengthString(String numberA, String numberB) {
        return numberA.length() > numberB.length() ? numberB : numberA;
    }

    // 反转后补0
    public static String getMoreZero(String str, int maxLen) {

        int strLen = str.length();
        int addLen = maxLen - strLen;

        if (addLen < 1) {
            return str;
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < addLen; i++) {
            sb.append("0");
        }
        return str + sb.toString();
    }

    // 反转后,获取sum
    public static String getSumNumber(String maxString, String minString, int maxLength) {
        int[] resultArray = new int[maxLength + 1];
        int[] maxStringArray = stringToIntArray(maxString);
        int[] minStringArray = stringToIntArray(minString);

        int temp = 0;
        for (int i = 0; i < maxLength; i++) {

            // a1 + b1 + c1
            temp = maxStringArray[i] + minStringArray[i] + resultArray[i];
            if (temp > 9) {
                resultArray[i] = temp % 9;
                // 结果大于9,进位1
                resultArray[i + 1] = 1;
            } else {
                resultArray[i] = temp;
            }
        }


        StringBuilder maxNumberS = new StringBuilder();
        for (int i = 0; i < maxLength; i++) {
            maxNumberS.append(resultArray[i]);
        }

        // 判断最后一位是否有进位,有则保留
        if (resultArray[maxLength] != 0) {
            maxNumberS.append(resultArray[maxLength]);
        }

        return maxNumberS.reverse().toString();
    }

    // #char to int
    public static int stringCharToInt(char ch) {
        int result = (int) ch - 48;
        if (result < 0) {
            result = 0;
        }
        return result;
    }

    // 判断字符串是不是全数字
    public static boolean isAllNum(String s) {

        if (isBlank(s)) {
            return false;
        }

        int cursor = 0;

        int len = s.length();
        while (cursor < len &&
                Character.isDigit(s.charAt(cursor))) {
            cursor++;
        }

        if (cursor == len) {
            return true;
        }

        return false;
    }

    public static int[] stringToIntArray(String str) {

        int cursor = 0;
        int len = str.length();
        int[] arr = new int[len];

        while (cursor < len) {
            arr[cursor] = (int) str.charAt(cursor) - 48;
            cursor++;
        }

        return arr;
    }
}

  

-- test

/*
 * description:
 * @author maduar
 * @date 21/11/2018 10:16 PM
 * @email maduar@163.com
 *
 * */
public class MaxNumTest {

    @Test
    public void testBigInteger() {
        String numberA = "66666666666666666666666";
        String numberB = "2222222222222222222222";

        BigInteger a = new BigInteger(numberA);
        BigInteger b = new BigInteger(numberB);

        String result = "68888888888888888888888";
        String testResult = a.add(b).toString();

        Assert.assertEquals(result, testResult);
    }

    @Test
    public void testSumMaxNumber() {

        String numberA = "66666666666666666666666";
        String numberB = "2222222222222222222222";
//        String numberA = "123456";
//        String numberB = "54321";

        String numberC = MaxNum.sumMaxNumber(numberA, numberB);

        String result = "68888888888888888888888";
//        String result = "177777";

        Assert.assertEquals(result, numberC);
    }

    @Test
    public void testCheckStr() {
        String a = "";
        String b = null;
        String c = "1";
        String d = "1a";

        Assert.assertFalse(MaxNum.isAllNum(a));
        Assert.assertFalse(MaxNum.isAllNum(b));
        Assert.assertFalse(MaxNum.isAllNum(d));

        Assert.assertTrue(MaxNum.isAllNum(c));
    }

    @Test
    public void testIsBlank() {
        String a = "";
        String b = null;
        String c = "1";

        Assert.assertTrue(MaxNum.isBlank(a));
        Assert.assertTrue(MaxNum.isBlank(b));
        Assert.assertFalse(MaxNum.isBlank(c));
    }

    @Test
    public void testReverseString() {
        String a = "12345";
        String b = MaxNum.reverseString(a);
        String result = "54321";

        Assert.assertEquals(result, b);
    }

    @Test
    public void testGetMaxLengthString() {
        String a = "12345";
        String b = "12";

        String result = "12345";
        Assert.assertEquals(result, MaxNum.getMaxLengthString(a, b));

    }

    @Test
    public void testGetMinLengthString() {
        String a = "12345";
        String b = "12";

        String result = "12";
        Assert.assertEquals(result, MaxNum.getMinLengthString(a, b));
    }

    @Test
    public void getGetMoreZero() {
        String a = "123";
        int len = 5;

        String result = "12300";

        Assert.assertEquals(result, MaxNum.getMoreZero(a, len));
    }

    @Test
    public void testGetSumNumber() {
        String numberA = "666";
        String numberB = "220";

        int len = numberA.length();
        String result = "688";

        Assert.assertEquals(result, MaxNum.getSumNumber(numberA, numberB, len));
    }

    @Test
    public void testStringCharToInt() {
        char a = '6';
        int result = 6;

        Assert.assertEquals(result, MaxNum.stringCharToInt(a));
    }

    @Test
    public void testIsAllNum() {
        String numberA = "66666666666666666666666";
        String numberb = "6666666666666666666666a";

        Assert.assertTrue(MaxNum.isAllNum(numberA));
        Assert.assertFalse(MaxNum.isAllNum(numberb));

    }
}

  

原文地址:https://www.cnblogs.com/maduar/p/10014474.html