<分区>
Possible Duplicate:
Division with really big numbers
我需要重载/运算符来处理两个 HugeInt 对象,这两个对象被简单地定义为一个包含 30 个短裤的数组。顺便说一句,这是家庭作业,但我已经为这个问题绞尽脑汁好几天了。
我已经重载了 * 运算符:
HugeInt HugeInt::operator*(const HugeInt &op2){
HugeInt temp;
short placeProducts[hugeIntSize + 1][hugeIntSize] = {0};
short product;
int carry = 0;
int k, leftSize, rightSize, numOfSumRows;
leftSize = getDigitLength();
rightSize = op2.getDigitLength();
if(leftSize <= rightSize) {
numOfSumRows = leftSize;
for(int i = (hugeIntSize - 1), k = 0; k < numOfSumRows; i--, k++) {
for(int j = (hugeIntSize - 1); j >= k; j--) {
product = integer[i] * op2.integer[j] + carry;
if (product > 9) {
carry = product / 10;
product %= 10;
} else {
carry = 0;
}
placeProducts[k][j - k] = product;
}
}
} else {
numOfSumRows = rightSize;
for(int i = (hugeIntSize - 1), k = 0; k < numOfSumRows; i--, k++) {
for(int j = (hugeIntSize - 1); j >= k; j--) {
product = integer[j] * op2.integer[i] + carry;
if (product > 9) {
carry = product / 10;
product %= 10;
} else {
carry = 0;
}
placeProducts[k][j - k] = product;
}
}
}
sumProductsArray(placeProducts, numOfSumRows);
for(int i = 0; i < hugeIntSize; i++)
{
temp.integer[i] = placeProducts[hugeIntSize][i];
}
return temp;}
但是我如何重载/操作?我的主要问题不是 C++ 代码或语法,而是我的除法算法。当我乘法时,我可以一个数字一个数字地计算。我将每个产品(也就是 1 的底部数字乘以上面的每个数字,然后使用我的进位算法将 10 的数字乘以上面的每个数字)存储在我的二维数组中。每次我得到新产品时,它都会向左偏移 n + 1,这会将它“乘以”所需的 10 次方。然后我只对所有列求和。
我这辈子都想不出如何编写长除法的代码。因为我处理的是两个数组,所以它必须是一个数字一个数字地排列,而且我怀疑它可能就像反转乘法算法一样简单。嵌套循环和减法?我需要一个商和提醒的变量吗?有更好的方法吗?我只需要指出正确的方向。