#include
#include
//using namespace std;
class Rational
{
private:
int numerator , denominator; //分子 分母
public:
Rational()
{ }
Rational(int n, int d) //构造函数
{
numerator=n;
denominator=d;
reduct();
}
void reduct() //约分函数,如将3/6约为1/2
{
int n;
int min=(numerator for(n=min;n>=1;n--)
if(numerator%n==0 && denominator%n==0)
break;
numerator=numerator/n;
denominator =denominator/n;
}
Rational operator +(const Rational &x) // 重载+号 完成两个分数对象的加法
{
Rational n;
int a,b;
a=this->numerator*x.denominator;
b=x.numerator*this->denominator;
n.numerator=a+b;
n.denominator=this->denominator*x.denominator;
n.reduct();
return n;
}
Rational operator -(const Rational &x)
{
Rational n;
int a,b;
a=this->numerator*x.denominator;
b=x.numerator*this->denominator;
n.numerator=a-b;
n.denominator=this->denominator*x.denominator;
n.reduct();
return n;
}
Rational operator *(const Rational &x)
{
Rational n;
n.numerator=this->numerator*x.numerator;
n.numerator=a+b;
n.denominator=this->denominator*x.denominator;
n.reduct();
return n;
}
Rational operator /(const Rational &x)
{
Rational n;
n.numerator=this->numerator*x.denominator;
n.denominator=this->denominator*x.numerator;
n.reduct();
return n;
}
friend ostream & operator<<(ostream &out,Rational &x);
friend istream & operator>>(istream &in,Rational &x);
}
ostream & operator<<(ostream &out,Rational &x)
{
out< return out;
}
istream & operator>>(istream &in,Rational &x)
{
in>>x.numerator;
while(true)
{
in>>x.denominator;
if(x.denominator!=0) break;
cout<<"分母为0,请重新输入分母:";
}
return in;
}