描述
设计一个有理数类Rational,要求对运算符“+”“-”“”“/”和“+=”“-=”“=”“/=”进行重载,完成有理数的加减乘除以及加减乘除复合赋值运算;并且重载“<<”和“>>”操作符完成有理数的输入和输出。最后,重载“==”和“!=”比较两个有理数是否相等。
类的定义如下:
class Rational
{
private:
int z; //分子
int m; //分母
public:
Rational(int a=0, int b=1); //构造有理数分数,分子默认为0,分母默认为1
friend Rational& yuefen(Rational& r); //约分函数对分数化简
friend Rational operator+(const Rational &r1, const Rational &r2);
friend Rational operator-(const Rational &r1, const Rational &r2);
friend Rational operator*(const Rational &r1, const Rational &r2);
friend Rational operator/(const Rational &r1, const Rational &r2);
Rational & operator+=(const Rational &r);
Rational & operator-=(const Rational &r);
Rational & operator*=(const Rational &r);
Rational & operator/=(const Rational &r);
friend bool operator==(const Rational &, const Rational &);//判断两个有理数是否相等
friend bool operator!=(const Rational &, const Rational &);//判断两个有理数是否不等
friend ostream & operator<<(ostream &, const Rational &);
friend istream & operator>>(istream &, Rational &);
};
使用以下的main函数体进行测试:
int main()
{
Rational r1, r2,r3;
while(cin>>r1>>r2)
{
cout << "r1 = " << yuefen(r1) << "\n" << "r2 = " << yuefen(r2) << endl;
r3 = r1 + r2;
cout << "r1+r2 = " << yuefen(r3) << endl;
r3 = r1 - r2;
cout << "r1-r2 = " << yuefen(r3) << endl;
r3 = r1 * r2;
cout << "r1*r2 = " << yuefen(r3) << endl;
r3 = r1 / r2;
cout << "r1/r2 = " << yuefen(r3) << endl;
cout << (r1 == r2) << " " << (r1 != r2) << endl;
cout << yuefen(r1 += r2) << endl;
cout << yuefen(r1 -= r2) << endl;
cout << yuefen(r1 *= r2) << endl;
cout << yuefen(r1 /= r2) << endl;
}
return 0;
}
输入
输入r1,r2的值
输出
输出r1,r2在各种操作之后的值。
输入样例 1
-4 6 2 -5
输出样例 1
r1 = -2/3 r2 = -2/5 r1+r2 = -16/15 r1-r2 = -4/15 r1*r2 = 4/15 r1/r2 = 5/3 0 1 -16/15 -2/3 4/15 -2/3
提示
friend Rational& yuefen(Rational& r);
该函数原理是求得分子和分母的最大公约数gcd,然后将r.m和r.z除以gcd得到最简分数形式。求最大公约数的方法叫做 辗转相除法,具体可上网查询。
#include <iostream>
#include <cmath>
using namespace std;
int gcd(int a,int b) {
if(b==0) return a;
return gcd(b,a%b);
}
class Rational
{
private:
int z; //分子
int m; //分母
public:
Rational(int a=0, int b=1)
{
z=a;
b=m;
}; //构造有理数分数,分子默认为0,分母默认为1
friend Rational& yuefen(Rational& r)
{
if(r.m<0)
{
r.z=-r.z;
r.m=-r.m;
}
int t;
t=abs(gcd(r.z,r.m));
r.z=r.z/t;
r.m=r.m/t;
//r.z=r.z/abs(gcd(r.z,r.m));
//r.m=r.m/abs(gcd(r.z,r.m));第一行影响了第二行
return r;
};
//约分函数对分数化简
friend Rational operator+(const Rational &r1, const Rational &r2)
{
Rational t;
t.z=r1.z*r2.m+r2.z*r1.m;
t.m=r1.m*r2.m;
yuefen(t);
return t;
};
friend Rational operator-(const Rational &r1, const Rational &r2)
{
Rational t;
t.z=r1.z*r2.m-r2.z*r1.m;
t.m=r1.m*r2.m;
yuefen(t);
return t;
};
friend Rational operator*(const Rational &r1, const Rational &r2)
{
Rational t;
t.z=r1.z*r2.z;
t.m=r1.m*r2.m;
yuefen(t);
return t;
};
friend Rational operator/(const Rational &r1, const Rational &r2)
{
Rational t;
t.z=r1.z*r2.m;
t.m=r1.m*r2.z;
//cout<<endl;
//cout<<r1.z*r2.m<<endl;
//cout<<r1.m*r2.z<<endl;
yuefen(t);
return t;
};
Rational & operator+=(const Rational &r)
{
*this=*this+r;
return *this;
};
Rational & operator-=(const Rational &r)
{
*this=*this-r;
return *this;
};
Rational & operator*=(const Rational &r)
{
*this=*this*r;
return *this;
};
Rational & operator/=(const Rational &r)
{
*this=*this/r;
return *this;
};
friend bool operator==(const Rational &a, const Rational &b)
{
if(a.z==b.z&&a.m==b.m)
return true;
else
return false;
};//判断两个有理数是否相等
friend bool operator!=(const Rational &a, const Rational &b)
{
if(a.z!=b.z||a.m!=b.m)
return true;
else
return false;
}
;//判断两个有理数是否不等
friend ostream & operator<<(ostream &os, const Rational &c)
{
os<<c.z<<"/"<<c.m;
return os;
};
friend istream & operator>>(istream &is, Rational &c)
{
is>>c.z>>c.m;
return is;
}
};
//OJ要去掉main,这是什么骚操作