C ++大整数 [英] C++ Big Integer
问题描述
我想在C ++中实现一个BigInteger类。但是,首先,我有一个基本问题,如何表示基本数据?例如,最愚蠢的方法是有一个固定(或动态)的char数组,并在char中存储每个单个数字的整数。但是,确定,这是一个非常愚蠢的方式,我在这里为您的建议。
I'm trying to implement a BigInteger Class in C++. But, first of all, I've a base question, how the "base data" can be represented? For example, the most stupid way is to have a fixed (or dynamic) array of char and store each single number of an integer in a char. But, ok, this is a very stupid way, and I'm here for your suggestions.
谢谢。
推荐答案
对于现有的实现有一堆建议: C ++处理非常大的整数
There are a bunch of suggestions here for existing implementations: C++ handling very large integers
如果你必须实现自己的(例如作业),那么你必须决定最好的方式,大你需要处理。你可以使用一个DWORD数组,并处理从一个到另一个的溢出。
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.
虽然对于一些Project Euler的东西,我实际上实现了一个BigNumber类一个字符串。原来是最简单的实现+ - * /,并缩放到显着更长的数字,我可以得到一些 unsigned long long
。而且性能完全足以解决这些难题。
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 ;-)
这篇关于C ++大整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!