その答えを表示するプログラムを作ろうとしたのですが四則演算の演算子のオーバーロードが
うまくいきません。
クラス部のコードを乗せるので、アドバイスをお願いします。
class Bunsu
{
int bunshi; //分子
int bunbo; //分母
public:
//デフォルトコンストラクタ
Bunsu()
{
bunshi = 1;
bunbo = 0;
}
//コンストラクタ
Bunsu( int b1, int b2 )
{
//約分をする
yakubun( b1, b2 );
}
//出力
void p_bunsu(void)
{
cout << get_bunshi() << " / " << get_bunbo() << "\n";
}
//分子の値を返す
int get_bunshi(void)
{
return bunshi;
}
//分母の値を返す
int get_bunbo(void)
{
return bunbo;
}
//分子に代入
void set_bunshi(int b)
{
bunshi = b;
}
//分子に代入
void set_bunbo(int b)
{
bunbo = b;
}
//約分をする
void yakubun( int si, int bo )
{
int yaku;
//最大公約数から約分をできるかを判定
if( bo > si )
yaku = gcm( bo , si );
else
yaku = gcm( si , bo );
if( yaku != 0 )
{
bunbo = bo / yaku;
bunshi = si / yaku;
}
}
//出力用のメンバ関数 (出力演算子のオーバーロード)
friend ostream& operator<<( ostream& , Bunsu& );
//入力用のメンバ関数 (入力演算子のオーバーロード)
friend istream& operator>>( istream& , Bunsu& );
//加算用のメンバ関数
friend Bunsu operator+( const Bunsu& );
//減算用のメンバ関数
friend Bunsu operator-( const Bunsu& );
//掛算用のメンバ関数
friend Bunsu operator*( const Bunsu& );
//除算用のメンバ関数
friend Bunsu operator/( const Bunsu& );
};
//最大公約数をユークリッドの互助法で求める関数
int gcm( int x, int y )
{
int r;
while( y != 0 ) //yの値が0以外のときに中でループを続ける
{
r = x % y;
x = y;
y = r;
}
return x;
}
ostream& operator<< ( ostream& s, Bunsu& a)
{
s << a.bunshi << " / " << a.bunbo << "\n";
return s;
}
istream& operator>> ( istream& i, Bunsu& d )
{
char ch;
i >> d.bunshi >> ch >> d.bunbo;
return i;
}
//加算用のメンバ関数
Bunsu operator+( const Bunsu& a );
{
Bunsu tmp;
tmp.bunshi = bunshi + a.bunshi;
tmp.bunbo = bunbo + a.bunbo;
return tmp;
}
//減算用のメンバ関数
Bunsu operator-( const Bunsu& a );
{
Bunsu tmp;
tmp.bunshi = bunshi - a.bunshi;
tmp.bunbo = bunbo - a.bunbo;
return tmp;
}
//掛算用のメンバ関数
Bunsu operator*( const Bunsu& a );
{
Bunsu tmp;
tmp.bunshi = bunshi * a.bunshi;
tmp.bunbo = bunbo * a.bunbo;
return tmp;
}
//除算用のメンバ関数
Bunsu operator/( const Bunsu& a );
{
Bunsu tmp;
tmp.bunshi = bunshi / a.bunshi;
tmp.bunbo = bunbo / a.bunbo;
return tmp;
}