使用链表做大整数乘法

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;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值