第2节增加两个大数字

时间:2014-01-21 11:58:17

标签: java arrays

所以在我上一个问题的代码中是一个错误。我试图修改代码,它应该添加两个大数字作为两个数组(我不能使用BigIntiger,我必须自己制作方法)。但它仍然给我错误的添加结果。

例如(我已经有了这个构造函数):

BigNumber dl1 = new BigNumber(1500);
BigNumber dl2 = new BigNumber("987349837937497938943242");

dl3 = dl1.add(dl2);
    System.out.println("Result: " + dl3);

它给了我6575,这是错误的结果。

public BigNumber add(BigNumber num2){

    char[] m = null;
    long y = 0;
    long x = 0;
    boolean tmpBool = false;
    boolean leftIsBigger = false;
    String tmpString = "";
    int ending = 0;

    if (this.n.length >= num2.n.length){
        m = new char[this.n.length + 1];
        y = num2.n.length;
        x = this.n.length;
        leftIsBigger = true;
    }
    else{
        m = new char[this.n.length + 1];
        y = this.n.length;
        x = num2.n.length;
    }

    for(int i = 0; i < y; i++){
        int left = 0;
        if(leftIsBigger) left = Character.getNumericValue(this.n[i]);
        else left = Character.getNumericValue(num2.n[i]);

        for(int j = 0; j < y; j++){
            int right = 0;
            if(!leftIsBigger) right = Character.getNumericValue(num2.n[j]);
            else righta = Character.getNumericValue(this.n[j]);

            int z = left + right;

            if(tmpBool){
                z++;
                tmpBool = false;
            }
            if(z > 9){
                tmpBool = true;
                z = z%10;
            }
            m[i] = Character.forDigit(z, 10);
        }

        ending++;
    }

    for(int k = ending; k < m.length - 1; k++){
        if (leftIsBigger){
            if (tmpBool){
                int c = Character.getNumericValue(this.n[k]);
                if (c > 9){
                    tmpBool = true;
                    c = c%10;
                    m[k] = Character.forDigit(c, 10);
                }
                else{
                    tmpBool = false;
                    m[k] = Character.forDigit((c+1), 10);
                }
            }
            else
                m[k] = this.n[k];
        }else{
            if (tmpBool){
                int c = Character.getNumericValue(liczba2.n[k]);
                if (c > 9){
                    tmpBool = true;
                    c = c%10;
                    m[k] = Character.forDigit(c, 10);
                }
                else{
                    tmpBool = false;
                    m[k] = Character.forDigit((c+1), 10);
                }
            }
            else
                m[k] = this.n[k];
        }
    }
    for (int it = m.length - 1; it >= 0; it--){
        tmpString += m[it];
    }

    BigNumber dl = new BigNumber(tmpString);
    return dl;      
}

2 个答案:

答案 0 :(得分:1)

在你的初始化if语句(检查内部数组的长度)中的问题不是你将m char数组初始化为this.n而不是num2.n的长度吗?

编辑:另外,你设置迭代的方式,我假设你的内部数组从左到右?因为索引0是10 ^ 0,索引1是10 ^ 1,索引2是10 ^ 2等?否则这也是一个问题。请注意,这意味着您必须在String类型构造函数中还原内部String char数组。

答案 1 :(得分:1)

您的代码太复杂,我无法搜索错误。整个“左更长”的逻辑是有缺陷的,恕我直言。

我这样做,假设我们正在处理带有十进制数字的char-Arrays:

char [] x, y;    // the operands we want to add
char [] result = new char[max (x.length, y.length) + 1]; 
int xi = x.length-1;        // index in 1st operand
int yi = y.length-1;        // index in 2nd operand
int ri = result.length-1;   // index in result
boolean carry = false;
while (xi >= 0 || yi >= 0) {
   char xc  = xi >= 0 ? x[xi--] : '0';
   char yc  = yi >= 0 ? y[yi--] : '0';
   char res = xc + yc - '0';
   if (carry) res++;
   carry = res > '9';
   if (carry) res -= 10;
   result[ri--] = res;
}
assert (ri == 0);
result[0] = carry ? '1' : '0';

请注意,结果数组总是比最长参数长1个字符。这并不好,因为重复添加将导致更长和更长的阵列在前面携带大量的0。 因此,如果最后一次添加没有进位,则将结果复制到另一个数组,或者 - 甚至更好 - 更改算法,使其忽略前导零。

这是一项练习。

相关问题