簡體   English   中英

C ++程序隨機凍結,沒有明顯的原因

[英]C++ program freezes randomly for no apparent reason

someboy可以向我解釋為什么當我編譯這段代碼時,它有時會達到“可能或者沒有”點,但大多數時候它會凍結在“hi”上並且沒有任何反應。 該程序也會進行另一次迭代。 我真的很困惑; 我在Windows上使用Code :: blocks作為IDE。 這只有在我為乘法設置BigInteger> = 100時才會發生。

#include <iostream>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;

    class BigInteger
    {
        vector<int> representation;
    private:
        vector<int> truncateZeros()
        {
            int end = 0;
            for(int i = this->representation.size()-1; i >= 0; --i)
            {
                if(this->representation[i] != 0)
                {
                    end = i;
                    break;
                }
            }
            vector<int> truncated;
            for(int i = 0; i <= end; ++i)
            {
                truncated.push_back(this->representation[i]);
            }
            return truncated;
        }
    public:
        BigInteger(int number)
        {
            this->representation.resize(100);
            int pos = 0;
            while(number != 0)
            {
                this->representation[pos] = number % 10;
                number /= 10;
                ++pos;
            }
        }
        BigInteger(string number)
        {
            this->representation.resize(100);
            int pos = 0;
            for(int i = number.size()-1; i >= 0; --i)
            {
                this->representation[pos] = number[i] - '0';
                ++pos;
            }
        }
        BigInteger(vector<int> number)
        {
            this->representation.resize(100);
            int pos = 0;
            for(int i = number.size()-1; i >= 0; --i)
            {
                this->representation[pos]=number[i];
                ++pos;
            }
        }
        vector<int> getInteger()
        {
            vector<int> copy;
            vector<int> truncated = this->truncateZeros();
            copy.resize(truncated.size());
            std::copy(truncated.begin(), truncated.end(), copy.begin());
            reverse(copy.begin(), copy.end());
            return copy;
        }
        string toString()
        {
            string result;
            vector<int> truncated = this->truncateZeros();
            for(int i = truncated.size()-1; i >= 0; --i)
            {
                result.push_back(truncated[i] + '0');
            }
            return result;
        }
        BigInteger multiplyBy(BigInteger bigNumber)
        {
            vector<int> bigNum = bigNumber.getInteger();
            vector<int> sum(this->representation.size());
            int size = bigNum.size();
            for(int j = 0, i = size-1; i >= 0; --i, ++j)
            {
                int desetici = 0;
                vector<int> result(this->representation.size());
                int startAt = 0;
                for(int k = 1; k <= j; ++k)
                {
                    ++startAt;
                }
                for(int k = 0; k < this->representation.size(); ++k)
                {
                    int res = this->representation[k] * bigNum[i];
                    if(desetici > 0)
                    {
                        res += desetici;
                        desetici = 0;
                    }
                    if(res > 9)
                    {
                        result[k + j] = res % 10;
                        res /= 10;
                        desetici = res % 10;
                    }
                    else
                    {
                        result[k + j] = res;
                    }
                }
                //now sum
                for(int k = 0; k < result.size(); ++k)
                {
                    sum[k] += result[k];
                    int pos = k;
                    while(sum[pos] > 9)
                    {
                        ++sum[pos+1];
                        sum[pos] = sum[pos] % 10;
                        ++pos;
                    }
                }
                cout<<"hi"<<endl;
            }
            cout<<"Might or might not";
            //now we return sum
            reverse(sum.begin(), sum.end());
            return BigInteger(sum);

        }
    };
    int main()
    {
        int a = 10000;
        BigInteger big = BigInteger(a);
        vector<int> rep = big.getInteger();
        for(int i = 0; i < rep.size(); ++i)
        {
            cout<<rep[i];
        }
        cout<<endl;
        string asResult = big.toString();
        cout<<asResult<<endl;
        cout<<"Try to multiply:"<<endl;
        BigInteger res = big.multiplyBy(BigInteger(876));
        string aaa = res.toString();
        cout<<aaa;
        return 0;
    }

j>0時, result[k + j]超出范圍。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM