メソッドのオーバーロードに問題はありますか?実際には動作するはずですが、そうではないと思います。 このコードが正しく動作しないのはなぜですか?私は実行時に何も表示されません。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class BIGINT
{
vector<int> big_number;
public:
BIGINT(){}
BIGINT(int);
BIGINT(string);
BIGINT operator+(const BIGINT&)const;
BIGINT operator+(int)const;
BIGINT& operator+=(const BIGINT&);
BIGINT& operator==(const BIGINT&);
vector<int> get_bn() { return big_number; }
friend ostream& operator<<(ostream& out,const BIGINT&);
};
//コンストラクタ
BIGINT::BIGINT(int n)
{
vector<int> temp;
while (n > 0)
{
int a;
a = n % 10;
n /= 10;
temp.push_back(a);
}
for (int i = temp.size() - 1;i > 0;i--)
{
big_number.push_back(temp[i]);
}
}
BIGINT::BIGINT(string sn)
{
vector<char> temp;
for (int i = 0;i < sn.size();i++)
{
temp.push_back(sn[i]);
}
for (int i = 0;i < temp.size();i++)
{
big_number.push_back(temp[i]-48);
}
}
//オーバーロードオペレータ+
BIGINT BIGINT::operator+(const BIGINT& bn) const
{
BIGINT temp;
int size;
int k = 0;
if (bn.big_number.size()>big_number.size()) size = big_number.size();
else size = bn.big_number.size();
for (int i = size - 1;i > 0;i--)
{
if(k)
{
if (big_number[i] + bn.big_number[i]>9)
{
temp.big_number.push_back(big_number[i] + bn.big_number[i] - 10 + 1);
k = 1;
}
else
{
temp.big_number.push_back(big_number[i] + bn.big_number[i] + 1);
k = 0;
}
}
else
{
if(big_number[i]+bn.big_number[i]>9)
{
temp.big_number.push_back(big_number[i] + bn.big_number[i] - 10);
k = 1;
}
else
{
temp.big_number.push_back(big_number[i] + bn.big_number[i]);
k = 0;
}
}
}
return temp;
}
BIGINT BIGINT::operator+(int n)const
{
BIGINT temp;
int size;
int k = 0;
vector<int> temp_int;
while (n > 0)
{
int a;
a = n % 10;
n /= 10;
temp_int.push_back(a);
}
if (temp_int.size()>big_number.size()) size = big_number.size();
else size = temp_int.size();
for (int i = size - 1;i > 0;i--)
{
if (k)
{
if (big_number[i] + temp_int[i]>9)
{
temp.big_number.push_back(big_number[i] + temp_int[i] - 10 + 1);
k = 1;
}
else
{
temp.big_number.push_back(big_number[i] + temp_int[i] + 1);
k = 0;
}
}
else
{
if (big_number[i] + temp_int[i]>9)
{
temp.big_number.push_back(big_number[i] + temp_int[i] - 10);
k = 1;
}
else
{
temp.big_number.push_back(big_number[i] + temp_int[i]);
k = 0;
}
}
}
return temp;
}
BIGINT operator+(int n, BIGINT bn)
{
BIGINT temp;
int size;
int k = 0;
vector<int> temp_int;
while (n > 0)
{
int a;
a = n % 10;
n /= 10;
temp_int.push_back(a);
}
if (temp_int.size()>bn.get_bn().size()) size = bn.get_bn().size();
else size = temp_int.size();
for (int i = size - 1;i > 0;i--)
{
if (k)
{
if (bn.get_bn()[i] + temp_int[i]>9)
{
temp.get_bn().push_back(bn.get_bn()[i] + temp_int[i] - 10 + 1);
k = 1;
}
else
{
temp.get_bn().push_back(bn.get_bn()[i] + temp_int[i] + 1);
k = 0;
}
}
else
{
if (bn.get_bn()[i] + temp_int[i]>9)
{
temp.get_bn().push_back(bn.get_bn()[i] + temp_int[i] - 10);
k = 1;
}
else
{
temp.get_bn().push_back(bn.get_bn()[i] + temp_int[i]);
k = 0;
}
}
}
return temp;
}
//オーバーロード演算子+ =
BIGINT& BIGINT::operator+=(const BIGINT& bn)
{
int size;
int k = 0;
if (bn.big_number.size()>big_number.size()) size = big_number.size();
else size = bn.big_number.size();
for (int i = size - 1;i > 0;i--)
{
if (k)
{
if (big_number[i] + bn.big_number[i]>9)
{
big_number[i] = big_number[i] + bn.big_number[i] - 10 + 1;
k = 1;
}
else
{
big_number[i] = big_number[i] + bn.big_number[i] + 1;
k = 0;
}
}
else
{
if (big_number[i] + bn.big_number[i]>9)
{
big_number[i] = big_number[i] + bn.big_number[i] - 10;
k = 1;
}
else
{
big_number[i] = big_number[i] + bn.big_number[i];
k = 0;
}
}
}
return *this;
}
//オーバーロードオペレータ==
BIGINT& BIGINT::operator==(const BIGINT& bn)
{
for (int i = 0;i < big_number.size();i++)
{
big_number[i] == bn.big_number[i];
}
return *this;
}
//演算子オーバーロード< <
ostream& operator<<(ostream& out, const BIGINT& bn)
{
for (int i = 0;i < bn.big_number.size();i++)
{
out << bn.big_number[i];
}
return out;
}
//メイン
int main()
{
BIGINT a(123);
BIGINT b(2);
BIGINT c;
c = a + b;
cout << c << endl;
system("pause");
return 0;
}
男ああ、テキストの壁。あなたがデバッガでそれを踏んだとき何が起こったのですか? – RyanP
代わりに[mcve]と[edit]を作成してください。これは無関係のコードです。 – chris
デバッガ。デバッガを使用します。または、* Print Statement Debugging *という古代の技術を使用してください。 –