2

可能重复:
如何在 C++ 中实现大整数

例如,假设我有一个“long”类型的变量。long 可以存储从 -⁠9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 的值。我是否可以创建自己的 C++ 数据类型,允许我创建无限大小的整数?我知道从记忆的角度来看这有点鲁莽,但我只是好奇。

4

3 回答 3

2

Ha! Here is a reckless answer!

Just create a class that dynamically creates and adds bit sets. To implement the class, you should create a vector (or any appropriate container you want/need) that store unsigned longs (each unsigned long represents a bit set of higher dimension than that of what precedes it; you can also use any POD types you want).

For the class to have virtually unlimited possible values, you should implement automatic detection (preferably on operators) for value overflows and underflows. For overflows you should then add a bit set of higher dimension after the last bit set. Underflows should be handled by setting a reference bit set that contains the value zero. If the value becomes lower than zero (or the lowest value), you should then add a bit set before the reference (or that which contains the lowest value) bit set.

Please read about binary arithmetic and base numbers. They should give you the idea (and are very useful indeed!).

于 2012-08-24T05:54:53.910 回答
0

当然,看看例如GNU Multiple Precision Arithmetic Library

于 2012-08-24T02:59:57.667 回答
0

是的,只需实施它(或谷歌)。一个例子是http://sourceforge.net/projects/cpp-bigint/

于 2012-08-24T03:00:03.083 回答