2017-12-20 137 views
5

Tôi đang cố gắng nhân hai số nguyên 64 chữ số và nhận được lỗi - uint512_t không được khai báo trong phạm vi này khi tôi cố gắng lưu trữ sản phẩm trong các loại dữ liệu uint512_t. Có loại dữ liệu thay thế nào mà tôi có thể sử dụng để lưu trữ các giá trị lớn như vậy không? Các mảng của tôi chứa các chữ số của các số tôi đang cố nhân lên.Làm cách nào để xác định số nguyên 512 bit trong C++?

#include <cstdint> 
#include <iostream> 
#include <stdint.h> 
using namespace std; 

int multiply(int x, int y, int carry) 
{ 
    int product; 
    product = x * y + carry; 
    return product; 
} 

int add(int multiplier, int product_current, int product_new) 
{ 
    product_current = product_current + multiplier * product_new; 
    return product_current; 
} 

int main() 
{ 
    int a[64] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5, 0, 2, 8, 8, 4, 1, 9, 7, 1, 6, 9, 3, 9, 9, 3, 7, 5, 1, 0, 5, 8, 2, 0, 9, 7, 4, 9, 4, 4, 5, 9, 2 }; 
    int b[64] = { 2, 7, 1, 8, 2, 8, 1, 8, 2, 8, 4, 5, 9, 0, 4, 5, 2, 3, 5, 3, 6, 0, 2, 8, 7, 4, 7, 1, 3, 5, 2, 6, 6, 2, 4, 9, 7, 7, 5, 7, 2, 4, 7, 0, 9, 3, 6, 9, 9, 9, 5, 9, 5, 7, 4, 9, 6, 6, 9, 6, 7, 6, 2, 7 }; 
    int carryin = 0; 
    uint512_t temp_result = 0; 
    uint512_t temp_product = 0; 
    int temp_carry = 0; 
    uint512_t product_acch = 0; 
    uint512_t product_accr = 0; 
    uint512_t mul = 1; 
    uint512_t mul2 = 1; 
    for (int i = 3; i >= 0; i--) { 
     carryin = 0; 
     product_acch = 0; 
     mul = 1; 
     for (int j = 3; j >= 0; j--) { 
      temp_result = multiply(a[j], b[i], carryin); 
      temp_product = temp_result % 10; 
      temp_carry = temp_result/10; 
      product_acch = add(mul, product_acch, temp_product); 
      mul = mul * 10; 
      carryin = temp_carry; 
      if (carryin != 0 && j == 0) { 
       product_acch = product_acch + mul * carryin; 
      } 
     } 
     product_accr = add(mul2, product_accr, product_acch); 

     cout << product_accr << endl; 
     mul2 = mul2 * 10; 
    } 

    cout << product_accr; 
    return 0; 
} 
+7

Không có 'uint512_t' trong thư viện chuẩn. bạn sẽ phải sử dụng thư viện bigint. – AShelly

+1

https://stackoverflow.com/search?q=big+integer+C%2B%2B –

Trả lời

7

Sử dụng Boost:

#include <boost/multiprecision/cpp_int.hpp> 

using namespace boost::multiprecision; 

int512_t x; 
uint512_t y; 
Các vấn đề liên quan