C ++ VS2012处理大于32位的数字

时间:2013-02-07 00:32:48

标签: c++ visual-studio-2012

我有这个程序需要处理大数字来将数字分解为质数..就像RSA分解挑战一样。

我在带有素数的txt文件中得到了这个列表。这是我用来制作该列表的一段代码:

int export_list (int lim = 50)
{
    int last_in_txt = 0;
    {
        ifstream infile ("Primes.txt");
        int k;
        while(infile >> k)
        { last_in_txt = k; }
    }
    // Now last_in_txt is assigned properly, and Primes.txt is closed

    cout << "\nLast number in \"Primes.txt\": " << last_in_txt << endl << endl;
    cout << "Press <Enter> to start appending primes... ";
    cin.get();
    cout << "\nAppend started:\n";

    last_in_txt++;

    ofstream file ("Primes.txt" , ios::app);
    int x, counter;

    if (file.is_open()) // if it opens correctly
    {
        for (x = last_in_txt , counter = 0 ; counter < lim ; x++ , counter++)
        {
            if (check_prime (x)) // returns 1 when x is prime, returns 0 when not
            {
                cout << "Appending " << x << "\t\t" << "Estimated time remaining: " << (lim - counter) / 1000 <<endl;
                file << x << " ";
            }
        }
        cout << "Done!" << endl << endl << pressenter;
        cin.get();
    }
    else
    {
        cout << "Unable to open file" << endl << pressenter;
        cin.get();
    }
    return(0);
}

问题是,当我发现这个txt文件包含大于32位的数字时,它不会处理它们...... last_in_txt变量将始终存储txt文件中的最后一个数字,即不超过32位...

1 个答案:

答案 0 :(得分:1)

如果64位整数足够,请使用int64_t。如果您需要更大的数字,请查看以下问题:Bigint (bigbit) library