题目描述
定义有理数类(分母不为0的分数,分子分母均为整数)Rational,实现相应操作符的重载。
(1)定义私有数据成员:分子int iUp; 分母 int iDown。
(2)定义私有成员函数:void Reduce() 和 int Gcd(int l, int r),分别用于有理数的约简和求两个整数的最大公约数。其中,在约简时需要求取分子与分母的最大公约数。
(3)定义构造函数,在构造函数体内可调用Reduce对有理数进行约简。
(4)将负号-和赋值运算符=重载为公有成员函数,分别用于求有理数的负数和赋值。
(5)将前置++、前置--、后置++、后置--重载为公有成员函数,实现有理数自增1或自减1。
(6)将+、-、*、/重载为友员函数,实现有理数的加减乘除。
(7)将<、<=、>、>=重载为友员函数,实现有理数的大小关系比较。
(8)重载流插入符<<和流提取符>>,分别用于有理数的输出和输入。其中,输出格式为“分子/分母”,若为整数,则直接输出整数。
在main函数中,根据输入的分子和分母定义两个有理数对象a和b。再定义几个有理数对象分别用于表示a和b的加、减、乘、除、前置自增a、前置自减a、后置自增a、后置自减a,并依次各个对象的结果。最后依次用<、<=、>、>=比较a和b的大小关系,并依次输出比较结果(true或false)。
输入
两个有理数a和b的的分子和分母
输出
有理数a和b的加、减、乘、除以及前置自增a、前置自减a、后置自增a、后置自减a
有理数a和b的<、<=、>、>=的结果
样例输入
4 3 3 2
样例输出
a+b: 17/6
a-b: -1/6
a*b: 2
a/b: 8/9
-a: -4/3
++a: 7/3
--a: 4/3
a++: 4/3
a--: 7/3
a<b: true
a<=b: true
a>b: false
a>=b: false
#include <iostream>
#include <cmath>
using namespace std;class Rational
{
private:int iUp,iDown;void Reduce(){int t=abs(Gcd(iUp,iDown));iUp=iUp/t;iDown=iDown/t;if(iDown<0) { iDown=-iDown; iUp=-iUp; }}int Gcd(int a,int b){int r=a%b;while(r!=0){a=b; b=r; r=a%b;}return b;}
public:Rational(){}Rational(int a,int b){iUp=a; iDown=b;Reduce();}Rational& operator=(const Rational& a){iUp=a.iUp;iDown=a.iDown;Reduce();return *this;}Rational operator-(){Rational t;t.iUp=-iUp;t.iDown=iDown;return t;}Rational& operator++();Rational operator++(int);Rational& operator--();Rational operator--(int);friend Rational operator+(const Rational&,const Rational&);friend Rational operator-(const Rational&,const Rational&);friend Rational operator*(const Rational&,const Rational&);friend Rational operator/(const Rational&,const Rational&);friend bool operator<(const Rational&,const Rational&);friend bool operator<=(const Rational&,const Rational&);friend bool operator>(const Rational&,const Rational&);friend bool operator>=(const Rational&,const Rational&);friend ostream& operator<<(ostream& out,const Rational&);friend istream& operator>>(istream& in,Rational&);
};Rational& Rational::operator++()
{iUp=iUp+iDown;return *this;
}
Rational Rational::operator++(int)
{Rational temp(iUp,iDown);iUp=iUp+iDown;return temp;
}
Rational& Rational::operator--()
{iUp=iUp-iDown;return *this;
}
Rational Rational::operator--(int)
{Rational temp(iUp,iDown);iUp=iUp-iDown;return temp;
}
Rational operator+(const Rational& a,const Rational& b)
{Rational t;t.iUp = a.iUp*b.iDown + b.iUp*a.iDown;t.iDown = a.iDown * b.iDown;return t;}
Rational operator-(const Rational& a,const Rational& b)
{Rational t;t.iUp = a.iUp*b.iDown - b.iUp*a.iDown;t.iDown = a.iDown * b.iDown;return t;
}
Rational operator*(const Rational& a,const Rational& b)
{Rational t;t.iUp = a.iUp * b.iUp;t.iDown = a.iDown * b.iDown;return t;
}
Rational operator/(const Rational& a,const Rational& b)
{Rational t;t.iUp = a.iUp * b.iDown;t.iDown = a.iDown * b.iUp;return t;
}
bool operator<(const Rational&a,const Rational&b)
{return( a.iUp/(double)a.iDown < b.iUp/(double)b.iDown );
}
bool operator<=(const Rational&a,const Rational&b)
{return( a.iUp/(double)a.iDown <= b.iUp/(double)b.iDown );
}
bool operator>(const Rational&a,const Rational&b)
{return( a.iUp/(double)a.iDown > b.iUp/(double)b.iDown );
}
bool operator>=(const Rational&a,const Rational&b)
{return( a.iUp/(double)a.iDown >= b.iUp/(double)b.iDown );
}
ostream& operator<<(ostream& out,const Rational&a)
{if(a.iUp%a.iDown!=0)out<<a.iUp<<"/"<<a.iDown;else out<<(a.iUp/a.iDown);return out;
}
istream& operator>>(istream& in,Rational&a)
{in>>a.iUp>>a.iDown;return in;
}int main()
{Rational a,b;cin>>a>>b;Rational c;c=a+b;cout<<"a+b: "<<c<<endl;c=a-b;cout<<"a-b: "<<c<<endl;c=a*b;cout<<"a*b: "<<c<<endl;c=a/b;cout<<"a/b: "<<c<<endl;c=-a;cout<<"-a: "<<c<<endl;c=++a;cout<<"++a: "<<c<<endl;c=--a;cout<<"--a: "<<c<<endl;c=a++;cout<<"a++: "<<c<<endl;c=a--;cout<<"a--: "<<c<<endl;bool x;x=a<b;cout<<"a<b: "<<boolalpha<<x<<endl;x=a<=b;cout<<"a<=b: "<<x<<endl;x=a>b;cout<<"a>b: "<<x<<endl;x=a>=b;cout<<"a>=b: "<<x<<endl;return 0;
}