2014-01-08 3 views
1

Может кто-нибудь объяснить мне, почему, когда я компилирую этот код, иногда он достигает точки «могущество или мигнот», но в большинстве случаев он зависает на «привет», и ничего не происходит. Программа также делает еще одну итерацию. Я действительно озадачен; Я использую Code :: blocks в Windows как IDE. И это происходит только тогда, когда я установил BigInteger для умножения> = 100.C++ программа зависает случайно без видимых причин

#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; 
    } 
+0

По какой-либо причине вы не можете использовать отладчик? –

+0

Это не дает мне никакой полезной информации или по крайней мере, что я могу понять. – user1113314

+0

Скорее всего, потребуется много времени для расчетов. Кроме того, я получаю предупреждения о подписанных и неподписанных сравнениях. Возможно, вы захотите изучить их. Во всяком случае, мои остановки заканчиваются мгновенно где-то между 500 и 600. – chris

ответ

3

result[k + j] выходит за пределы, когда j>0.

Смежные вопросы