使用链表做大整数乘法

BigInteger BigInteger::operator*(BigInteger & bi)
{
BigInteger product,base;
product.bigInteger.push_back(0);
int i=0, j=0,k, result, temp;
LinkedList::d_iterator it1 = bigInteger.rbegin(), it2;
while (it1 != bigInteger.end() )
{
it2 = bi.bigInteger.rbegin();
while(it2 != bi.bigInteger.end())
{
for (k = 0; k < i+j; k++)
{
base.bigInteger.push_front(0);
}
result = *it1 * *it2;
if (result>=1000)
{
temp = result % 1000;
base.bigInteger.push_front(temp);
result = result / 1000;
}
base.bigInteger.push_front(result);
product = product + base;
while (base.bigInteger.getSize()!=0)
{
base.bigInteger.pop_back();
}
it2--;
j++;
}
j = 0;
it1--;
i++;
}
//BigInteger product,zero,copybi;
//copybi = bi; //参数为bi的引用 改变bi就会改掉实参的值 导致乘法结束第二个乘数改变产生错误 所以可选择copy或const
//product.bigInteger.push_back(0);
//zero.bigInteger.push_back(0);
//while (!(copybi==zero))
//{
// product = product + *this;
// --copybi;
//}
/*int i = 0;
short int data;*/
//LinkedList::d_iterator it = bi.bigInteger.rbegin();
/*while (it!=bi.bigInteger.end())
{
data = *it;
data *= (int)pow(1000,i);
while (data != 0)
{
product = product + *this;
data--;
}
it--;
i++;
}*/
return product;
}

你可能感兴趣的:(使用链表做大整数乘法)