简体   繁体   中英

add method with two different array of integer objects

I'm trying to implement a method in a class that create and returns a new object for the sum of integers represented by the called object and other.

For example

BigInt val1 = new BigInt(1111111);
BigInt val2 = new BigInt(2222);
BigInt sum = val1.add(val2);
System.out.println(sum);
should print
1113333

My add method seems to work fine but when it gets to the last index, it does not carry 1 to the last index. Here is my code and test cases.

public BigInt add(BigInt other) {
//        int length = this.digits.length < other.digits.length ? this.digits.length : other.digits.length;
        BigInt added = new BigInt();
        int MAX_numsig = this.numSigDigits < other.numSigDigits ? other.getNumSigDigits() : this.getNumSigDigits();

        added.numSigDigits = MAX_numsig;


        if (other == null) {
            throw new IllegalArgumentException("parameter is null");
        }
        if (this.getNumSigDigits() >= SIZE || other.getNumSigDigits() >= SIZE) {
            throw new ArithmeticException("Sum size bigger than final static value SIZE");
        }
        int carry = 0;
        for (int i = this.digits.length - 1; i >= 0; i--) {


            if (this.digits[i] + other.digits[i] + carry < 10) {
                added.digits[i] = this.digits[i] + other.digits[i] + carry;
                carry = 0;

            }
            if (this.digits[i] + other.digits[i] + carry >= 10) {
                added.digits[i] = (this.digits[i] + other.digits[i] + carry) % 10;

                if (i == SIZE - MAX_numsig) {
                    added.numSigDigits = MAX_numsig + 1;
                }
                if (i == 0) {
                    throw new ArithmeticException("overflow");
                } else {
                    carry = 1;
                }
            }

        }



        return added;

    }

and here is the test case:

 System.out.println("Test 12: result should be\n123456789123456789");
        int[] a4 = {3, 6, 1, 8, 2, 7, 3, 6, 0, 3, 6, 1, 8, 2, 7, 3, 6};
        int[] a5 = {8, 7, 2, 7, 4, 0, 5, 3, 0, 8, 7, 2, 7, 4, 0, 5, 3};
        BigInt b4 = new BigInt(a4);
        BigInt b5 = new BigInt(a5);
        BigInt sum2 = b4.add(b5);
        System.out.println(sum2);
        System.out.println();

This test case should return 123456789123456789. Instead, it's returning 23456789123456789. I don't understand why 1 is not passing at the last index.

SIZE is a default static final value of 20. Can anyone help me with a bug?

The reason is number of digits of the sum of two numbers can be bigger than both numbers, for example in your given example the number of digits of sum are 18 and the number of digits of the both values are 17. But your code is not considering this part.

So, there will have a carry value after the loop exists, if the number of digits in the sum is bigger. You should add the carry value to the result.

Hope it helps. Let me know if you dont understand any part.

Here is one approach. There are probably more efficient ways but it highlights some of the issues to resolve.

        String a = "9999";
        String b = "999";

        // pad smallest on left with 0's.
        int diff = a.length() - b.length();
        if (diff < 0) {
            a  = "0".repeat(-diff) + a;
        } else {
            b = "0".repeat(diff) + b;
        }

        // convert both string to arrays of int digits.
        int[] aa = a.chars().map(c -> c - '0').toArray();
        int[] bb = b.chars().map(c -> c - '0').toArray();

        int carry = 0;
        // iterate over the array and add and process the carry bit
        for (int i = aa.length-1; i >= 0; i--) {
            bb[i] += (aa[i] + carry);
            carry = 0;
            if (bb[i] > 9) {
                bb[i] %= 10;
                carry = 1;
            }
        }

        // Now convert the array of digits to a numeric string.
        String result =
                Arrays.stream(bb).mapToObj(c -> (char)(c + '0') + "")
                        .collect(Collectors.joining());

        // there could still be a carry to process. e.g "999" + "1"
        if (carry == 1) {
            result = '1' + result;
        }
        System.out.println(result);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM