JavaコードをC++で変換したい
コードは
BigInteger value = new BigInteger(125, RandomNumber);
BigInteger clone = new BigInteger(value.toByteArray());
gmpライブラリを使用してcppでこのコードを書く方法は?
誰か助けてください。ありがとう。
JavaコードをC++で変換したい
コードは
BigInteger value = new BigInteger(125, RandomNumber);
BigInteger clone = new BigInteger(value.toByteArray());
gmpライブラリを使用してcppでこのコードを書く方法は?
誰か助けてください。ありがとう。
これがウィキペディアからのカーボンコピーです
これは、GMPライブラリを使用して多数を乗算および出力することを示すCコードの例です。
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
int main(void)
{
mpz_t x;
mpz_t y;
mpz_t result;
mpz_init(x);
mpz_init(y);
mpz_init(result);
mpz_set_str(x, "7612058254738945", 10);
mpz_set_str(y, "9263591128439081", 10);
mpz_mul(result, x, y);
gmp_printf("\n %Zd\n*\n %Zd\n--------------------\n%Zd\n\n", x, y, result);
/* free used memory */
mpz_clear(x);
mpz_clear(y);
mpz_clear(result);
return EXIT_SUCCESS;
}
このコードは、7612058254738945×9263591128439081の値を計算します。
このプログラムをコンパイルして実行すると、この結果が得られます。(Unixタイプのシステムでコンパイルする場合は、-lgmpフラグが使用されます。)
7612058254738945
*
9263591128439081
--------------------
70514995317761165008628990709545
C ++を使用すると、それを行うことができます
#include <gmpxx.h>
#include <gmp.h>
#include <iostream>
using namespace std;
int main(){
mpz_class value;
mpz_class clone;
gmp_randclass r(gmp_randinit_default);
value = r.get_z_bits(125);
clone = value;
cout << value << endl;
cout << clone << endl;
return 0;
}
そしてg++file.cpp-lgmpxx-lgmpでコンパイルします
libgmpxx.aをインストールするには、。/configureのビルドオプションに--enable-cxxを追加します