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

标签 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 乘以给定的整数。

MultiplyByTen 乘以 10。(废话)

addBCDs 将 2 个 BCD 加在一起。

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

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

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. 在 add 方法中,您将 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() 方法看起来非常优雅,与其他方法不同,但我不相信它可以正确处理进位。

关于java - 方法返回 222 * (2 + 2 + 2) 而不是 222 * 222,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33989069/

相关文章:

R within() 操作顺序和逻辑

c# - 有效地用代码表达2x2逻辑网格

c++ - 将 Unsigned Int 转换为 BCD

java - 迁移到 jboss wildfly

java - 拦截一种方法两次

java - chrome Webdriver 无法解析为 eclipse 和 java 类型错误

c - BCD算术运算

java - 单击需要在Java中更改按钮颜色

scripting - Awk 脚本帮助 - 逻辑问题

arduino - BCD转十进制和十进制转BCD