乘以bigints c ++ [英] multiply bigints c++
本文介绍了乘以bigints c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的乘法算法,它是我的大num类的一部分。 arr是指向我为Safe可调整大小的数组创建的类的指针。 main()显然是不完整的(只是把它放在那里以显示我的bigint类如何与我的乘法函数相关)我在这里真正需要的是修复我的算法或给出我如何解决它的建议。谢谢
This is my multiply algorithm that is part of my big num class. The arr is a pointer to a class that I created for a Safe resizable array. The main() is obviously incomplete(just put it there to show how my bigint class works in relation to my multiply function) What I really need here is a fix for my algorithm here or give my advice on how to fix it. Thanks
void multiply(const bigint &A)
{
bigint temp1; // bigint with value 0
int carry = 0;
int shift = 0;
bigint temp2;
for(int j = size-1; j >= 0; j--) //no member size in bigint
{
for(int i=size-1; i>=0; i--)
{
// bigint with value 0 and size: size + A.size
int result = (arr->get(i)*A.arr->get(j)+carry);
//if(size - shift - (i - size-1) >= 0)
temp2.arr->set(size-i, result%10);
carry=result/10;
}
shift++;
temp1.add_pos(temp2);
}
this->assign(temp1);
}
int main()
{
a.assign (12345);
b.assign (12342357);
a.multiply (b);
}
推荐答案
您显然已复制了类的add-methode并使其成倍增加-方法。但那不行。您只是将A的每个数字乘以arr的对应的数字。要进行乘法运算,您必须将A的每个数字乘以每个数字的arr!正如您在纸上所做的那样。
先在纸上试一试,然后相应地更正你的算法。
You have obviously copied the add-methode of your class and made it into a multiply-method. But that won't work. You are only multiplying each digit of A with the corresponding digit of arr. To do a multiplication you must however multiply each digit of A with each digit of arr! Just as you would do on paper.
Try it on paper first and then correct your algorithm correspondingly.
你也可以看看 Boost Multiprecision Library [< a href =http://www.boost.org/doc/libs/1_53_0/libs/multiprecision/doc/html/boost_multiprecision/intro.html\"target =_ blanktitle =New Window> ^ ]
最好的问候
Espen Harlinn
You could also take a look at the Boost Multiprecision Library[^]
Best regards
Espen Harlinn
这篇关于乘以bigints c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文