[英]C++ program freezes randomly for no apparent reason
Can someboy explain to me why when I compile this code, sometimes it reaches the "might or mighnot" point, but most of the times it freezes on the "hi" and nothing happens. someboy可以向我解释为什么当我编译这段代码时,它有时会达到“可能或者没有”点,但大多数时候它会冻结在“hi”上并且没有任何反应。 The program doest make another iteration either.
该程序也会进行另一次迭代。 I'm really puzzled;
我真的很困惑; I'm using Code::blocks on Windows as IDE.
我在Windows上使用Code :: blocks作为IDE。 And this only happens when I set a BigInteger for multiplication >= 100.
这只有在我为乘法设置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.