C ++中的大数字库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12988099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:50:10  来源:igfitidea点击:

Big numbers library in c++

c++biginteger

提问by Rontogiannis Aristofanis

I'm doing a project which requires really big numbers, up to 100 digits. I have read that java supports big integers (java.Math.BigInteger), and I want to know if there is something like that in C++. So, here is my question: Is there a standard or non-standard c++ library which implements big integers?

我正在做一个需要非常大的数字的项目,最多 100 位数字。我读过java支持大整数(java.Math.BigInteger),我想知道C++中是否有类似的东西。所以,这是我的问题:是否有实现大整数的标准或非标准 C++ 库?

Note:If there is no standard implementation for big integers, I would like a simplenon-standard. Thanks in advance.

注意:如果没有大整数的标准实现,我想要一个简单的非标准。提前致谢。

回答by saeedn

The GNU Multiple Precision Arithmetic Library does what you want http://gmplib.org/

GNU 多精度算术库可以满足您的需求http://gmplib.org/

Gnu MP is a C library but it has a C++ class Interfaceand if you are interested only in big integers, you may just deal with mpz_class. Look at the sample below which I took from the page C++ Interface General

Gnu MP 是一个 C 库,但它有一个C++ 类接口,如果你只对大整数感兴趣,你可以只处理mpz_class. 看看下面我从页面C++ Interface General 中获取的示例

 int main (void)
 {
   mpz_class a, b, c;

   a = 1234;
   b = "-5678";
   c = a+b;
   cout << "sum is " << c << "\n";
   cout << "absolute value is " << abs(c) << "\n";

   return 0;
 }

回答by user2001885

Unfortunately, there is no standard library for big numbers. You said you are looking for a "simple" library, the simplest library I know of is InfInt. It consists of just one header file. Its usage is fairly simple. Here is a sample code:

不幸的是,没有大数字的标准库。您说您正在寻找一个“简单”的库,我所知道的最简单的库是InfInt。它只包含一个头文件。它的用法相当简单。这是一个示例代码:

InfInt myint1 = "15432154865413186646848435184100510168404641560358";
InfInt myint2 = 156341300544608LL;

myint1 *= --myint2 - 3;
std::cout << myint1 << std::endl;

回答by john

You said you want a simple interface/implementation, here's one http://www.di-mgt.com.au/bigdigits.html. Personally I'd still go for GMP however.

你说你想要一个简单的界面/实现,这是一个http://www.di-mgt.com.au/bigdigits.html。就个人而言,我仍然会选择 GMP。

回答by my time

You will be taking input in a char array, and then will change it into an int array. The size of array can also be changed.

您将在 char 数组中输入,然后将其更改为 int 数组。数组的大小也可以改变。

#include<iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
    int b, i, arrayint[100];
    char arraychar[100];

    for(i = 0; i < 100; i++)
        cin >> arraychar[i];

    for(i = 0; i < 100; i++)
        cout << arraychar[i];

    cout << endl;

    for(i = 0; i < 100; i++)
        arrayint[i] = arraychar[i] - '0';

    for(i = 0; i < 100; i++)
        cout << arrayint[i];

    return 0;
}