任何有理数都可以表示为“分子/分母”的方式,其中分子、分母都是整数。
现实现一个有理数类,为这个有理数类重载四则运算(+、-、*、/)和所有的比较逻辑运算符(==、!=、>=、>、<=、<)
class Rational
{
private:
int numerator; // 分子
int denominator; // 分母
public:
//构造函数及运算符重载的函数声明
};
//重载函数的实现及用于测试的main()函数
重载四则运算(+、-、*、/)和所有的比较逻辑运算符(==、!=、>=、>、<=、<)
方法一:所有重载均以类的成员函数的方式实现。
#include
#include
using namespace std;
class Rational{
public:
Rational(int num=1,int denomi=1){ //构造函数,带默认参数
numerator = num;
denominator = denomi;
normalize();
}
Rational operator + (Rational &s); //重载加法运算
Rational operator - (Rational &s); //重载减法运算
Rational operator * (Rational &s); //重载乘法运算
Rational operator / (Rational &s); //重载除法运算
bool operator > (Rational &s); //重载">"运算符
bool operator < (Rational &s); //重载"<"运算符
bool operator >= (Rational &s); //重载">="运算符
bool operator <= (Rational &s); //重载"<="运算符
bool operator == (Rational &s); //重载"=="运算符
friend istream& operator >>(istream&,Rational&);
friend ostream& operator <<(ostream&,Rational &); //重载输出运算符
int gcd(int a,int b){
return b?gcd(b,a%b):a;
}
void normalize(); //将分数化为最简式函数
private:
int numerator; //分子
int denominator; //分母
};
void Rational::normalize(){
if(denominator<0){ //保证分母大于0
numerator *= -1;
denominator *= -1;
}
int a = abs(numerator);
int b = abs(denominator);
int gcd_ab = gcd(a,b); //求分子、分母的最大公约数
//分子、分母分别除以最大公约数,得到最简式
numerator /= gcd_ab;
denominator /=gcd_ab;
}
Rational Rational:: operator + (Rational &s){
Rational t;
t.numerator = numerator*s.denominator+denominator*s.numerator;
t.denominator = denominator*s.denominator;
return Rational(t.numerator,t.denominator);
}
Rational Rational::operator - (Rational &s){
Rational t;
t.numerator = numerator*s.denominator-denominator*s.numerator;
t.denominator = denominator*s.denominator;
return Rational(t.numerator,t.denominator);
}
Rational Rational::operator * (Rational &s){
Rational t;
t.numerator = numerator*s.numerator;
t.denominator = denominator*s.denominator;
return Rational(t.numerator,t.denominator);
}
Rational Rational::operator / (Rational &s){
Rational t;
t.numerator = numerator*s.denominator;
t.denominator = denominator*s.numerator;
return Rational(t.numerator,t.denominator);
}
bool Rational::operator > (Rational &s){
if(numerator*s.denominator-denominator*s.numerator>0)
return true;
else
return false;
}
bool Rational::operator < (Rational &s){
if(numerator*s.denominator-denominator*s.numerator<0)
return true;
else
return false;
}
bool Rational::operator >= (Rational &s){
if(numerator*s.denominator-denominator*s.numerator>=0)
return true;
else
return false;
}
bool Rational::operator <= (Rational &s){
if(numerator*s.denominator-denominator*s.numerator<=0)
return true;
else
return false;
}
bool Rational::operator == (Rational &s){
if(numerator*s.denominator-denominator*s.numerator==0)
return true;
else
return false;
}
bool Rational::operator == (Rational &s){
if(numerator*s.denominator-denominator*s.numerator!=0)
return true;
else
return false;
}
istream& operator >> (istream& in,Rational &obj){
cout << "输入分子:";
in >> obj.numerator;
cout << "输入分母:";
in >> obj.denominator;
return in;
}
ostream& operator << (ostream& out,Rational &obj){
if(obj.numerator==0)
out << obj.numerator;
if(obj.denominator==1)
out << obj.numerator;
else
out << obj.numerator << '/' << obj.denominator;
return out;
}
int main(){
Rational r1,r2;
cout << "输入r1:" << endl;
cin >> r1;
cout << "r1 = " << r1 << endl;
cout << "输入r2:" << endl;
cin >> r2;
cout << "r2 = " << r2 << endl;
//测试四则运算运算符
Rational r3 = r1+r2;
Rational r4 = r1-r2;
Rational r5 = r1*r2;
Rational r6 = r1/r2;
cout << "r1 + r2 = " << r3 << endl;
cout << "r1 - r2 = " << r4 << endl;
cout << "r1 * r2 = " << r5 << endl;
cout << "r1 / r2 = " << r6 << endl;
//测试比较运算符
if(r1>r2) cout << "r1大于r2" << endl;
if(r1<r2) cout << "r1小于r2" << endl;
if(r1==r2) cout << "r1等于r2" << endl;
if(r1<=r2) cout << "r1小于等于r2" << endl;
if(r1>=r2) cout << "r1大于等于r2" << endl;
return 0;
}
测试1
输入:
1
3
2
3
输入:
8
9
5
7
**方法2:使用友元函数实现 **
#include
#include
using namespace std;
class Rational{
public:
Rational(int num=1,int denomi=1){ //构造函数,带默认参数
numerator = num;
denominator = denomi;
normalize();
}
friend Rational operator + (Rational &,Rational &); //重载加法运算
friend Rational operator - (Rational &,Rational &); //重载减法运算
friend Rational operator * (Rational &,Rational &); //重载乘法运算
friend Rational operator / (Rational &,Rational &); //重载除法运算
friend bool operator > (Rational &,Rational &); //重载">"运算符
friend bool operator < (Rational &,Rational &); //重载"<"运算符
friend bool operator >= (Rational &,Rational &); //重载">="运算符
friend bool operator <= (Rational &,Rational &); //重载"<="运算符
friend bool operator == (Rational &,Rational &); //重载"=="运算符
friend istream& operator >>(istream&,Rational&);
friend ostream& operator <<(ostream&,Rational &); //重载输出运算符
int gcd(int a,int b){
return b?gcd(b,a%b):a;
}
void normalize(); //将分数化为最简式函数
private:
int numerator; //分子
int denominator; //分母
};
void Rational::normalize(){
if(denominator<0){ //保证分母大于0
numerator *= -1;
denominator *= -1;
}
int a = abs(numerator);
int b = abs(denominator);
int gcd_ab = gcd(a,b); //求分子、分母的最大公约数
//分子、分母分别除以最大公约数,得到最简式
numerator /= gcd_ab;
denominator /=gcd_ab;
}
Rational operator + (Rational &x,Rational &y){
int a = x.numerator;
int b = x.denominator;
int c = y.numerator;
int d = y.denominator;
int e = a*d+c*b;
int f = b*d;
return Rational(e,f);
}
Rational operator - (Rational &x,Rational &y){
int a = x.numerator;
int b = x.denominator;
int c = y.numerator;
int d = y.denominator;
int e = a*d-c*b;
int f = b*d;
return Rational(e,f);
}
Rational operator * (Rational &x,Rational &y){
int a = x.numerator;
int b = x.denominator;
int c = y.numerator;
int d = y.denominator;
int e = a*c;
int f = b*d;
return Rational(e,f);
}
Rational operator / (Rational &x,Rational &y){
int a = x.numerator;
int b = x.denominator;
int c = y.numerator;
int d = y.denominator;
int e = a*d;
int f = b*c;
return Rational(e,f);
}
bool operator > (Rational &x,Rational &y){
Rational p = x-y;
return p.numerator > 0;
}
bool operator < (Rational &x,Rational &y){
Rational p = x-y;
return p.numerator < 0;
}
bool operator >= (Rational &x,Rational &y){
Rational p = x-y;
return p.numerator >= 0;
}
bool operator <= (Rational &x,Rational &y){
Rational p = x-y;
return p.numerator <= 0;
}
bool operator != (Rational &x,Rational &y){
Rational p = x-y;
return p.numerator <= 0;
bool operator == (Rational &x,Rational &y){
Rational p = x-y;
return p.numerator == 0;
}
istream& operator >> (istream& in,Rational &obj){
cout << "输入分子:";
in >> obj.numerator;
cout << "输入分母:";
in >> obj.denominator;
return in;
}
ostream& operator << (ostream& out,Rational &obj){
if(obj.numerator==0)
out << obj.numerator;
if(obj.denominator==1)
out << obj.numerator;
else
out << obj.numerator << '/' << obj.denominator;
return out;
}
int main(){
Rational r1(3,5),r2(2,5);
cout << "输入r1:" << endl;
cin >> r1;
cout << "r1 = " << r1 << endl;
cout << "输入r2:" << endl;
cin >> r2;
cout << "r2 = " << r2 << endl;
Rational r3 = r1+r2;
Rational r4 = r1-r2;
Rational r5 = r1*r2;
Rational r6 = r1/r2;
cout << "r1 + r2 = " << r3 << endl;
cout << "r1 - r2 = " << r4 << endl;
cout << "r1 * r2 = " << r5 << endl;
cout << "r1 / r2 = " << r6 << endl;
return 0;
}