C ++程序随机冻结,没有明显的原因

时间:2014-01-08 23:57:45

标签: c++ codeblocks

有些男孩可以向我解释为什么当我编译这段代码时,它有时会达到“可能或不可能”的点,但大多数时候它会在“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;
    }

1 个答案:

答案 0 :(得分:3)

result[k + j]

j>0超出界限。