1
Someboyboy可以向我解釋爲什麼當我編譯這段代碼時,它有時會達到「可能或不可能」的點,但大多數時候它凍結在「hi」並且什麼也沒有發生。該程序也做了另一次迭代。我真的很困惑;我在Windows上使用Code :: blocks作爲IDE。而當我設一個BigInteger乘法> = 100C++程序無故凍結
#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;
}
你無法使用調試器的任何原因? –
它沒有給我任何有用的信息,或至少我能理解。 – user1113314
這可能只需要花費很多時間來進行計算。另外,我收到有關簽名和未簽名比較的警告。你可能想看看那些。無論如何,我的鑽石會在500到600之間瞬間完成。 – chris