方法返回222 *(2 + 2 + 2)而不是222 * 222

时间:2015-11-29 22:46:12

标签: java logic bcd

public BCD multiplyBCDs(BCD other) {
    BCD thisBCD = new BCD(digit);
    BCD newBCD = new BCD(0);
    int DIGIT = 0;
    do {
        newBCD = newBCD.addBCDs(other.multiplyBy(thisBCD.nthDigit(DIGIT)));
        other = other.multiplyByTen();
        DIGIT++;
    } while (DIGIT < thisBCD.numberOfDigits());
    return newBCD;
}

程序的其余部分对于解决我的问题不应该太重要,但如果是,我会将其余部分发布在底部。每个BCD基本上是存储在数组中的整数。 thisBCD等于222,BCD other也等于222.我的目标是将这两个BCD相乘。

nthDigit与thisBCD [DIGIT]基本相同,返回该数字的某个数字。

MultiplyBy将BCD乘以给定的int。

MultiplyByTen乘以10.(duh)

addBCDs将2个BCD加在一起。

我的问题是,该方法不是返回49,284((222 * 2)+(222 * 20)+(222 * 200),而是返回1332(222 *(2 + 2 + 2)。

如果您需要,我的程序和驱动程序的其余部分如下:

public class BCD {
    private int[] digit;

    // constructor (creates array from another array)
    public BCD(int bcdDigits[]) {
        digit = new int[bcdDigits.length];
        for (int i = 0; i < digit.length; i++) {
            digit[i] = bcdDigits[i];
        }
    }

    // constructor (creates array from an int)
    public BCD(int num) {
        digit = new int[1];
        int DIGIT = num % 10;
        digit[0] = DIGIT;
        num /= 10;
        while (num > 0) {
            DIGIT = num % 10;
            addADigit(DIGIT);
            num /= 10;
        }
    }

    // returns number of digits in bcd
    public int numberOfDigits() {
        int length = digit.length;
        return length;
    }

    // returns the value of a certain digit in the BCD
    public int nthDigit(int n) {
        System.out.println("N: " + n);
        if (n >= digit.length || n < 0) {
            return -1;
        } else {
            return digit[digit.length - 1 - n];
        }
    }

    // prints the BCD backwards (printing the original number being stored in
    // the array)
    public void print() {
        for (int i = digit.length - 1; i >= 0; i--) {
            System.out.print(digit[i]);
            if (i % 3 == 0 && i != 0) {
                System.out.print(",");
            }
        }
    }

    // adds a digit to the BCD
    public void addADigit(int newdigit) {
        int[] a = new int[digit.length + 1];
        for (int i = 0; i < digit.length; i++) {
            a[i] = digit[i];
        }
        a[a.length - 1] = newdigit;
        digit = a;
    }

    public BCD addBCDs(BCD other) {
        BCD newBCD = null;
        int length;
        if (other.numberOfDigits() > digit.length) {
            length = other.numberOfDigits();
        } else {
            length = digit.length;
        }
        int count = 0;
        int temp = 0;
        int carry = 0;
        while (count < length + 1) {
            temp = 0;
            // Adds the value of digit[count] to temp, assuming that there is a
            // number available there.
            if (count <= digit.length - 1) {
                temp += digit[count];
            }

            // Adds the value of other.nthDigit(count) to temp, assuming that
            // there is a number available there.
            if (count <= (other.numberOfDigits()) - 1) {
                temp += other.nthDigit(count);
            }

            // either adds temp as the first digit of the newBCD, or adds temp %
            // 10, and sets up a carry. This section is for the first digit
            // only.
            if (count == 0 && temp < 10) {
                newBCD = new BCD(temp);
            } else if (count == 0 && temp >= 10) {
                newBCD = new BCD(temp % 10);
                carry++;
            }

            // either adds temp as the next digit of the newBCD, or adds temp %
            // 10, and sets up a carry. This section works for all digits
            // excluding the first.

            if (count > 0 && temp + carry < 10 && count < length) {
                newBCD.addADigit(temp + carry);
                carry = 0;
            } else if (count > 0 && temp + carry < 10 && count == length && temp + carry != 0) {
                newBCD.addADigit(temp + carry);
                carry = 0;
            } else if (temp + carry >= 10 && count > 0) {
                newBCD.addADigit((temp + carry) % 10);
                carry = 0;
                carry++;
            }
            count++;
        }
        return newBCD;
    }

    public BCD multiplyByTen() {
        BCD oldBCD = new BCD(digit);
        if (oldBCD.numberOfDigits() == 1 && oldBCD.nthDigit(0) == 0) {
            BCD newBCD = new BCD(0);
            return newBCD;
        } else {
            int[] newArray = new int[oldBCD.numberOfDigits() + 1];
            newArray[0] = 0;
            for (int i = 1; i < newArray.length; i++) {
                newArray[i] = oldBCD.nthDigit(i - 1);
            }
            BCD newBCD = new BCD(newArray);
            return newBCD;
        }
    }

    public BCD multiplyBy(int num) {
        int[] digit2 = new int[digit.length + num];
        System.out.println("Num: " + num);
        for (int i = 0; i < digit.length; i++) {
            digit2[i] = digit[i];
        }
        int length = 0;
        if (digit.length > num) {
            length = num;
        } else {
            length = digit.length;
        }

        if (num == 0) {
            BCD newBCD = new BCD(0);
            return newBCD;
        } else if (num == 1) {
            BCD newBCD = new BCD(digit);
            return newBCD;
        } else if (num == 10) {
            BCD newBCD = new BCD(digit);
            newBCD = newBCD.multiplyByTen();
            return newBCD;
        }
        BCD newBCD = null;
        int temp = 0;
        int carry = 0;
        for (int count = 0; count < (digit2.length); count++) {
            if (count == 0) {
                temp = digit2[count] * num;
                if ((temp + carry) == 0 && count >= length) {
                } else {
                    newBCD = new BCD((temp + carry) % 10);
                }
                carry = temp / 10;
            } else {
                temp = digit2[count] * num;
                if ((temp + carry) == 0 && count >= length) {
                } else {
                    newBCD.addADigit((temp + carry) % 10);
                }
                carry = temp / 10;
            }
        }
        return newBCD;
    }

    public BCD multiplyBCDs(BCD other) {
        BCD thisBCD = new BCD(digit);
        BCD newBCD = new BCD(0);
        int DIGIT = 0;
        do {
            newBCD = newBCD.addBCDs(other.multiplyBy(thisBCD.nthDigit(DIGIT)));
            System.out.println("thisBCD nthDigit: " + thisBCD.nthDigit(DIGIT));
            other = other.multiplyByTen();
            System.out.print("\nOther: ");
            other.print();
            DIGIT++;
        } while (DIGIT < thisBCD.numberOfDigits());
        return newBCD;
    }

    // public static BCD factorial(int num) {

    // return newBCD; // this is a different newBCD you still need to create
    // }
}

司机:

public class BCDDriver {
public static void main(String[] args) {
     BCD B1 = new BCD(22);
     System.out.print("B1: ");
     B1.print();
     B1 = B1.multiplyBy(2);
     System.out.print("\nProduct: ");
     B1.print();
     BCD B2 = new BCD(22);
     System.out.print("\nB2: ");
     B2.print();
     BCD B3 = new BCD(22);
     System.out.print("\nB3: ");
     B3.print();
     B2 = B2.multiplyBCDs(B3);
     System.out.print("\nProduct: ");
     B2.print();
}

}

1 个答案:

答案 0 :(得分:1)

这里有几个问题。我不打算为你做功课,但是:

  1. 在添加方法中,您要将this.digit[count]添加到other.nthdigit(count)。您应该将this.digit[count]添加到other.digit[count]this.nthdigit(count)添加到other.nthdigit(count),具体取决于循环的运行方式。 (这个问题是通过使用测试值来掩饰的,例如222:不要这样做。)

  2. add方法的其余部分基本上是无意义的。不需要为每个数字创建一个新的BCD,例如:您可以直接对目标数组进行所有添加。你需要写下添加的手工方法并实现它。它有大约五行代码,而不是四十几行。

  3. 在'multiplyByTen()方法中,确实不需要使用零来创建一个特殊情况,尽管我可能会出于速度原因这样做。没有必要证明您对问题的理解。

  4. 同样,您的multiplyBy()方法大约是必要时间的四倍。这不复杂。仅需要4-5行代码。在这里,您再次创建一个新的BCD每个数字,这是不必要的,并且与其他地方的特殊套管零点的想法背道而驰。事实上,除了性能优化之外,零点并不是特殊情况。

  5. 您的multiplyBCDs()方法看起来非常优雅,与其他方法不同,但我不相信它处理正确。