十万火急!!帮忙看看我重载运算符里面是不是有什么错误 啊。为什么运行跟口算 的结果不对呢,检查不出来,希望能得到实质性的解决,谢谢
#include<iostream>using namespace std;
class RatNum
{
private:
int fz;
int fm;
public:
RatNum(){}
~RatNum(){}
RatNum(int z,int m){fz=z;fm=m;cout<<"有理数为:"<<fz<<"/"<<fm<<endl;}
void set_Rat(int z,int m){fz=z;fm=m;}
int get_Ratz();
int get_Ratm();
int common();
RatNum& operator +(RatNum &R);
RatNum& operator -(RatNum &R);
RatNum& operator *(RatNum &R);
RatNum& operator /(RatNum &R);
};
int RatNum::get_Ratz()
{
return fz;
}
int RatNum::get_Ratm()
{
return fm;
}
int RatNum::common()
{
int temp,a,b,i;
if(fz<fm)
{
temp=fm;
fm=fz;
fz=temp;
}
a=fz;
b=fm;
i=fz%fm;
while(i!=0)
{
fz=fm;
fm=i;
i=fz%fm;
}
cout<<"化简后的有理数分数为:"<<b/fm<<"/"<<a/fm<<endl;
return 0;
}
RatNum &RatNum:: operator +(RatNum &R)
{
fz=fz*(R.fm)+fm*(R.fz);
fm=fm*(R.fm);
return *this;
}
RatNum &RatNum:: operator -(RatNum &R)
{
fz=fz*R.fm-fm*R.fz;
fm=fm*R.fm;
return *this;
}
RatNum &RatNum:: operator *(RatNum &R)
{
fz=fz*R.fz;
fm=fm*R.fm;
return *this;
}
RatNum &RatNum:: operator /(RatNum &R)
{
fz=fz*R.fm;
fm=fm*R.fz;
return *this;
}
int main()
{
RatNum R1(3,4);
();
RatNum R2(2,4);
();
RatNum R3;
R3.set_Rat(4,5);
cout<<"有理数为:"<<R3.get_Ratz()<<"/"<<R3.get_Ratm()<<endl;
();
RatNum R4(2,5);
R4.operator+(R1); //就是这里
cout<<"有理数相加后的结果为:"<<R4.get_Ratz()<<"/"<<R4.get_Ratm();
cout<<endl<<endl;
RatNum R5(5,6);
R5.operator-(R1); //就是这里
cout<<"有理数相减后的结果为:"<<R5.get_Ratz()<<"/"<<R5.get_Ratm();
cout<<endl<<endl;
RatNum R6(1,3);
R6.operator*(R1); //就是这里
cout<<"有理数相乘后的结果为:"<<R6.get_Ratz()<<"/"<<R6.get_Ratm();
cout<<endl<<endl;
RatNum R7(1,2);
R7.operator/(R1); //就是这里
cout<<"有理数相除后的结果为:"<<R7.get_Ratz()<<"/"<<R7.get_Ratm();
cout<<endl<<endl;
system("pause");
return 0;
}