//---------------------------------------------------------------------------
#include<iostream>
#include<cmath.h>
using namespace std;
//---------------------------------------------------------------------------
class fraction
{
int above; //分子
int below; //分母
void reduction(); //约分函数
void makeCommond(fraction&); //通分函数
public:
fraction(int a = 0, int b = 1) //构造函数
{above=a; below=b;}
fraction add(fraction); //两分数相加(本分数加上实参分数)
fraction sub(fraction); //两分数相减(本分数减去实参分数)
fraction mul(fraction); //两分数相乘(本分数乘以实参分数)
fraction div(fraction); //两分数相除(本分数除以实参分数)
fraction reciprocal(); //求倒数
bool equal(fraction); //等于运算(本分数是否等于实参分数)
bool greaterThan(fraction); //大于运算(本分数是否大于实参分数)
bool lessThan(fraction); //小于运算(本分数是否小于实参分数)
void display(); //显示分数
void input(); //输入分数
};
//----------------------------------------------------------------------------
void fraction::reduction() //约分先求最大公约数
{ int a,b,temp;
if(below<0){above=-above;below=-below;}
a=abs(above);
b=abs(below);
while(a%b){
temp=a;
a=b;
b=temp%b;}
above/=b;
below/=b;
}
void fraction ::makeCommond(fraction&b) //通分
{ int temp;
reduction();
b.reduction();
above*=b.below;
b.above*=below;
temp=below*b.below;
below=b.below=temp;}
fraction fraction::add(fraction b) //两分数相加
{
makeCommond( b);
fraction temp;
temp.above=above+b.above;
temp.below=below;
temp.reduction();
return temp;}
fraction fraction::sub(fraction b) //两分数相减
{
makeCommond(b);
fraction temp;
temp.above=above-b.above;
temp.below=below;
temp.reduction();
return temp; }
fraction fraction:: mul(fraction b)//两分数相乘
{
fraction temp;
temp.above=above*b.above;
temp.below=below*b.below;
temp.reduction();
return temp; }
fraction fraction:: div(fraction b)//两分数相除
{
makeCommond(b);
fraction temp;
temp.above=above;
temp.below=b.above;
temp.reduction();
return temp; }
fraction fraction::reciprocal() //求倒数
{ fraction temp;
temp.above=below;
temp.below=above;
temp.reduction();
return temp ;}
bool fraction::equal(fraction b)//等于运算
{ makeCommond(b);
if(above==b.above) return 1 ;
}
bool fraction::greaterThan(fraction b)//大于运算
{ makeCommond(b);
if(above>b.above) return 1 ;
}
bool fraction::lessThan(fraction b) //小于运算
{ makeCommond(b);
if(above<b.above) return 1 ;
}
void fraction::display() //显示分数
{ reduction();
cout<<above<<"/"<<below<<endl;}
void fraction::input() //输入分数
{ cout<<"please input:"<<endl;
cin>>above>>below ;
while(below==0)
{cout<<"输入分母不能为零"<<endl<<"请重新输入:"<<endl;
cin>>above>>below ;}
reduction();
return ;}
//---------------------------------------------------------------------------
int main()
{
fraction f1(-3, -5), f2(-3, 5), f3(3, -7), f4, f5(8);
cout<<"f1 "; f1.display();
cout<<"f2 "; f2.display();
cout<<"f3 "; f3.display();
cout<<"f4 "; f4.display();
cout<<"f5 "; f5.display();
if (f1.greaterThan(f2)) cout<<"f1>f2"<<endl;
if (f2.lessThan(f3)) cout<<"f2<f3"<<endl;
if (f1.equal(f1)) cout<<"f1==f1"<<endl;
f4 = f1.add(f3);
cout<<"f4=f1+f3 "; f4.display();
f4 = f1.sub(f2);
cout<<"f4=f1-f2 "; f4.display();
f4 = f1.mul(f3);
cout<<"f4=f1*f3 "; f4.display();
f4 = f1.div(f3);
cout<<"f4=f1/f3 "; f4.display();
f4 = f2.reciprocal();
cout<<"f4=1/f2 "; f4.display();
f4.input();
cout<<"f4 "; f4.display();
return 0;}