将两个 int 数组相乘

Multiply two int arrays

本文关键字:int 数组 两个      更新时间:2023-10-16

我有一个类型BigInt,它将大数字作为数字数组(0-9)存储在称为privatelym_digitArray的字符数组中。 我正在重载算术和关系运算符来帮助开发。但是,我无法让乘法运算符*=为我工作。我发布的代码仅适用于int * BigInt位数操作,而我希望它适用于任何长度的数字和所有int * BigIntBigInt * int,尤其是BigInt * BigInt操作。就像它适用于 6 * bigInt(值为 6)= 36;但不是 11 * bigInt(值为 10)。

大国际.cpp

有问题的重载运算符

BigInt BigInt::operator *= (const BigInt &rhs){
int size = m_digitArraySize + rhs.getSize();
int* C = new int[size];
int s = size-1;
for(int j= rhs.getSize() - 1; j >= 0; j--){
int carry = 0;
int shift = s;
for(int i = m_digitArraySize - 1; i >= 0; i--){
int m = getDigit(i) * rhs.getDigit(j);
int sum = m + C[shift] + carry;
int num = sum % 10;
int c = sum/10;
C[shift] = num;
carry = c;
shift--;
}
C[shift]= C[shift] + carry;            
s--;            
}
reallocateArray(size);
// for(int i = 0; i < size < ++i){
//  m_digitArray[i] = '0' + C[i];
// }
// Nothing being returned, printing to debug
for (int i = 0; i < size; ++i)
{
cout << C[i];
}
return *this;
}
// Overload the * operator for BigInt * BigInt operations
BigInt operator * (const BigInt &lhs, const BigInt &rhs){
return BigInt(lhs) *= rhs;
}
// Overload the * operator for BigInt * int operations
BigInt operator * (const BigInt &lhs, int num){
return BigInt(lhs) *= num;
}
// Overload the * operator for int * BigInt operations
BigInt operator * (int num, const BigInt &rhs){
return BigInt(rhs) *= num;
}

您在逻辑中存在错误,因为您将运算符 * 编码为实现运算符 *= 来完成它的工作。你的问题是调用:

x = y * z

不应更改"y"的值。这是一个不必要的副作用。但是,如果您将 BigInt 的运算符*定义为使用 *= 来获取结果,这正是它的作用。另外,如果您这样做,则致电

x = z * y

将导致"x = y * z">的不同行为,因为现在 z 被改变而不是 y,这违反了数学的基本交换定律。

从运算符 * 和运算符 =开始然后通过将对这些运算符的调用链接在一起来构造运算符 *=。

运算符* 应该创建一个新的 BigInt,然后将两个输入的 BigInt(作为 "const BigInt&" 传递),然后返回新创建的 BigInt。为了优化这一点,您需要研究移动构造函数。但这不是必需的。

operator= 是你的复制赋值函数,以后应该构建它以使用 move 语义,但这也不是关键

然后,您可以使用 *= 组合其他两个函数,只需编写一些简单的东西,例如:

BigInt BigInt::operator*=(const BigInt &rhs)
{
(*this) = (*this) * rhs;
return (*this);
}

好吧,经过一些帮助,我能够弄清楚。以下是问题的解决方案:

BigInt BigInt::operator *= (const BigInt &rhs){
// Create new BigInt to make changes non-destructively
BigInt numbers;
// A safe capacity size for the new BigInt
int size = m_digitArraySize + rhs.getSize();
// If either number is negative, set self to negative
if(!m_isPositive || !rhs.isPositive())
numbers.initializeArray(size, false);
else
numbers.initializeArray(size, true);
// Go through the multiplier
for(int i = 0; i < rhs.getSize(); ++i){
int carry = 0;
// Go through the multiplicand
for(int j = 0; j < m_digitArraySize; ++j){
// The product of multiplicand and multiplier plus the sum of the previous digits and the carry
int product = (getDigit(j) * rhs.getDigit(i)) + numbers.getDigit(i + j) + carry;
// Reset carry, necessary if the product is just a digit
carry = 0;
// Set carry to the tens digit
carry = product / 10;
// Set product to the units digit
product = product % 10;
// Save to the new BigInt
numbers.setDigit(i +j, product);
}
// Inner loop cuts off near the end, continue the loop if there is a carry
int nextDigit = i + m_digitArraySize;
while(carry!=0){
int new_value = numbers.getDigit(nextDigit) + carry;
carry = 0;
carry = new_value / 10;
new_value = new_value % 10;
numbers.setDigit(nextDigit, new_value);
++nextDigit;
}
}
// Remove excess zeros
numbers.normalizeArray();
*this = numbers;
return *this;
}