您应该如何表示 BigInt(大数)类中的数据?
我正在尝试在 C++ 中实现 BigInteger 类。
基础数据如何表示?最简单的方法是使用固定或动态的 char
数组,并将整数的每个数字存储在 char
中。还有更好的办法吗?
I'm trying to implement a BigInteger
class in C++.
How can the base data be represented? The most naive way is to have a fixed or dynamic array of char
and store each digit of an integer in a char
. Is there any better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有很多针对现有实现的建议:C++ 处理非常大的整数
如果您必须实现你自己的(例如家庭作业),然后你必须决定最好的方法,以及你需要处理多大的“大”。您可以使用 DWORD 数组,并处理从一个到下一个的溢出。
不过,对于欧拉项目的一些内容,我实际上实现了一个基于字符串构建的 BigNumber 类。事实证明,它是最简单的 +-*/ 实现方法,并且可以扩展到比我用几个
unsigned long long
所能得到的要长得多的数字。其性能完全足以解决这些难题。因此,您面临着易于实施和最佳性能之间的权衡。玩得开心 ;-)
There are a bunch of suggestions here for existing implementations: C++ handling very large integers
If you have to implement your own (e.g. for homework), then you have to decide the best way, and how "big" you need to handle. You could use an array of DWORDs, and handle overflowing from one to the next.
Although, for some of the Project Euler stuff, I actually implemented a BigNumber class built on a string. It turned out to be the simplest to implement for +-*/, and scaled to significantly longer numbers than I could get with a few
unsigned long long
s. And the performance was perfectly adequate for solving those puzzles.So, you face a tradeoff between ease of implementation and optimal performance. Have fun ;-)
这是我的十进制实现。 string 代表值,bool 代表符号(注意:bool = true => 数字是负数)。
几乎所有基本功能都在这里进行了测试和测试准备使用。
注意:按位运算符的实现方式略有不同 (sda-ka-dish),所有这些都在 getBinary() 函数中进行了解释。
把自己打垮。
和 cpp 文件(我想如果你寻找特定的运算符,Ctrl + F 会做得更好):
That's my decimal base implementation. string for the value and a bool for the sign (Note: notice that bool = true => the number is negative).
pretty much all the basic functions are here tested & ready to use.
Pay Attention: the bitwise operators were implemented a bit (sda-ka-dish) differently, all explained in the getBinary() function.
knock yourself out.
and the cpp file (I guess Ctrl + F will do better if you look for a specific operator):
您可以完全按照您描述的方式创建一个大整数。事实上,我第一次实现这样的类时,我就是这么做的。它帮助我实现了算术运算(
+
、-
等),因为它是我习惯的基数 (10)。对“字符数组”的一个自然增强是将其保持为基数 10,但使用 4 位作为数字,而不是整个字节。因此,数字 123,456 可能由字节
12 34 56
表示,而不是字符串123456
。 (三个字节而不是六个字节。)从那里,您可以以二为基数存储数字。加法等基本算术运算在基数 2 中的工作方式与在基数 10 中的工作方式完全相同。因此,可以使用字节
FF FF
来存储数字 65565。 (例如,在unsigned char
向量中。)BigInt 的某些实现使用较大的块,例如short
或long
,以提高效率。如果您要进行大量显示和/或序列化为基数 10,并且希望避免转换为基数 2,则基数 10 大整数可能会很有用。
You can create a big integer in exactly the way you describe. In fact, the first time I implemented such a class, that's exactly the way I did it. It helped me implement the arithmetic operations (
+
,-
, etc) since it was in the base (10) that I was used to.A natural enhancement to your "array of chars" is to keep it in base 10, but use 4 bits for the digit, instead of the whole byte. Thus, the number 123,456 might be represented by the bytes
12 34 56
instead of the string123456
. (Three bytes as opposed to six.)From there, you could make the storage for the number in base two. The basic arithmetic operations such as addition work exactly the same in base 2 as they do in base 10. Thus, the number 65565 could be stored using the bytes
FF FF
. (In a vector ofunsigned char
s, for example.) Some implementations of BigInts use larger chunks, such asshort
orlong
, for efficiency.Base-10 big ints can be useful if you're doing a lot of displaying and/or serializing to base-10, and want to avoid the conversion to base-2.