我应该使用哪种数据类型在 C++ 中的变量中存储最多 18 位数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19645009/
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 23:01:16  来源:igfitidea点击:

Which data type should i use to store upto 18 digits in a variable in c++?

c++c

提问by amian

I am trying to solve a code which requires me to input and output integer value upto 18 digits. Unfortunately i am unable to store the value in any data type. I have already tried

我正在尝试解决一个要求我输入和输出最多 18 位整数值的代码。不幸的是,我无法以任何数据类型存储该值。我已经试过了

  long int
  unsigned long long 
  long long double,

None of these seem to work.Can u Please suggest me something that might help me output the value.

这些似乎都不起作用。你能不能给我建议一些可以帮助我输出值的东西。

回答by templatetypedef

18 digits gives a maximum possible value of 999,999,999,999,999,999 ≈ 9.9 × 1017. This will fit into an unsigned, 64-bit integer (maximum value 264, which is about 1.8446744 × 1019). Try using the uint64_ttype to ensure that you get this.

18 位数字给出的最大值为 999,999,999,999,999,999 ≈ 9.9 × 10 17。这将适合一个无符号的 64 位整数(最大值 2 64,约为 1.8446744 × 10 19)。尝试使用uint64_t类型来确保你得到这个。

Hope this helps!

希望这可以帮助!

回答by Md. Al-Amin

You can use string. Check this big-integer library for C++ here.

您可以使用字符串。在此处检查 C++ 的这个大整数库。

http://ideone.com/7sgc4J

http://ideone.com/7sgc4J

Code:

代码:

#include <cstdio>
#include <string>
#include <algorithm>
#include <iostream>

using namespace std;

struct Bigint {
    // representations and structures
    string a; // to store the digits
    int sign; // sign = -1 for negative numbers, sign = 1 otherwise

    // constructors
    Bigint() {} // default constructor
    Bigint( string b ) { (*this) = b; } // constructor for string

    // some helpful methods
    int size() { // returns number of digits
        return a.size();
    }

    Bigint inverseSign() { // changes the sign
        sign *= -1;
        return (*this);
    }

    Bigint normalize( int newSign ) { // removes leading 0, fixes sign
        for( int i = a.size() - 1; i > 0 && a[i] == '0'; i-- )
            a.erase(a.begin() + i);
        sign = ( a.size() == 1 && a[0] == '0' ) ? 1 : newSign;
        return (*this);

    }
    // assignment operator
    void operator = ( string b ) { // assigns a string to Bigint
        a = b[0] == '-' ? b.substr(1) : b;
        reverse( a.begin(), a.end() );
        this->normalize( b[0] == '-' ? -1 : 1 );
    }

    // conditional operators
    bool operator < ( const Bigint &b ) const { // less than operator
        if( sign != b.sign ) return sign < b.sign;
        if( a.size() != b.a.size() )
            return sign == 1 ? a.size() < b.a.size() : a.size() > b.a.size();
        for( int i = a.size() - 1; i >= 0; i-- ) if( a[i] != b.a[i] )
            return sign == 1 ? a[i] < b.a[i] : a[i] > b.a[i];
        return false;
    }
    bool operator == ( const Bigint &b ) const { // operator for equality
        return a == b.a && sign == b.sign;
    }

    // mathematical operators
    Bigint operator + ( Bigint b ) { // addition operator overloading
        if( sign != b.sign ) return (*this) - b.inverseSign();
        Bigint c;
        for(int i = 0, carry = 0; i<a.size() || i<b.size() || carry; i++ ) {
            carry+=(i<a.size() ? a[i]-48 : 0)+(i<b.a.size() ? b.a[i]-48 : 0);
            c.a += (carry % 10 + 48);
            carry /= 10;
        }
        return c.normalize(sign);
    }

    Bigint operator - ( Bigint b ) { // subtraction operator overloading
        if( sign != b.sign ) return (*this) + b.inverseSign();
        int s = sign; sign = b.sign = 1;
        if( (*this) < b ) return ((b - (*this)).inverseSign()).normalize(-s);
        Bigint c;
        for( int i = 0, borrow = 0; i < a.size(); i++ ) {
            borrow = a[i] - borrow - (i < b.size() ? b.a[i] : 48);
            c.a += borrow >= 0 ? borrow + 48 : borrow + 58;
            borrow = borrow >= 0 ? 0 : 1;
        }
        return c.normalize(s);
    }

    Bigint operator * ( Bigint b ) { // multiplication operator overloading
        Bigint c("0");
        for( int i = 0, k = a[i] - 48; i < a.size(); i++, k = a[i] - 48 ) {
            while(k--) c = c + b; // ith digit is k, so, we add k times
            b.a.insert(b.a.begin(), '0'); // multiplied by 10
        }
        return c.normalize(sign * b.sign);

    }

    Bigint operator / ( Bigint b ) { // division operator overloading
        if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
        Bigint c("0"), d;
        for( int j = 0; j < a.size(); j++ ) d.a += "0";
        int dSign = sign * b.sign; b.sign = 1;
        for( int i = a.size() - 1; i >= 0; i-- ) {
            c.a.insert( c.a.begin(), '0');
            c = c + a.substr( i, 1 );
            while( !( c < b ) ) c = c - b, d.a[i]++;
        }
        return d.normalize(dSign);
    }

    Bigint operator % ( Bigint b ) { // modulo operator overloading
        if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
        Bigint c("0");
        b.sign = 1;
        for( int i = a.size() - 1; i >= 0; i-- ) {
            c.a.insert( c.a.begin(), '0');
            c = c + a.substr( i, 1 );
            while( !( c < b ) ) c = c - b;

        }
        return c.normalize(sign);
    }

    // output method
    void print() {
        if( sign == -1 ) putchar('-');
        for( int i = a.size() - 1; i >= 0; i-- ) putchar(a[i]);
    }
};


int main() {

    Bigint a, b, c; // declared some Bigint variables
    /////////////////////////

    // taking Bigint input //

    /////////////////////////
    string input; // string to take input
    cin >> input; // take the Big integer as string

    a = input; // assign the string to Bigint a

    cin >> input; // take the Big integer as string
    b = input; // assign the string to Bigint b


    //////////////////////////////////

    // Using mathematical operators //

    //////////////////////////////////



    c = a + b; // adding a and b
    c.print(); // printing the Bigint
    puts(""); // newline


    c = a - b; // subtracting b from a
    c.print(); // printing the Bigint
    puts(""); // newline


    c = a * b; // multiplying a and b
    c.print(); // printing the Bigint
    puts(""); // newline


    c = a / b; // dividing a by b
    c.print(); // printing the Bigint
    puts(""); // newline

    c = a % b; // a modulo b
    c.print(); // printing the Bigint
    puts(""); // newline

    /////////////////////////////////

    // Using conditional operators //

    /////////////////////////////////

    if( a == b ) puts("equal"); // checking equality

    else puts("not equal");
    if( a < b ) puts("a is smaller than b"); // checking less than operator



    return 0;

}

回答by sean_m

Use auto. If you're using a current compiler version, they all (gcc, clang, vc++, Intel) implement this. The tools are smart enough to optimized this for you now. Also, are you building for 32bit or 64bit?

使用auto. 如果您使用的是当前编译器版本,它们(gcc、clang、vc++、Intel)都实现了这一点。这些工具足够智能,现在可以为您进行优化。另外,您是为 32 位还是 64 位构建?