/**********************************
fraction.cpp
功能:实现fraction类的基本功能
***********************************/
#include "fraction.h"
#include <string>
#include <iostream>
using namespace std;
//----------------------------------------------------
//-----构造函数 (从分数的输入形式中提取分子和分母)--
fraction::fraction(const string & s)
{
string m;
string n;
char ss;
int i;
for(i=0;i<s.size(); i++)
{
ss=s.at(i);
if(ss!='/')
m+=ss;
else
break;
}
for(i=i+1; i<s.size(); i++)
{ ss=s.at(i);
n+=ss;
}
fractions=atoi(m.c_str());
numerator=atoi(n.c_str());
}
//------------------------------------------------------
//-----初始化函数(用于构造函数以外的变量初始化)-------
bool fraction::set()
{
bool ret = true;
string s;
string m;
string n;
char ss;
int i;
int flag=0; //用于判断是否输入值为负数
cin>>s;
for(i=0;i<s.size(); i++)
{
ss=s.at(i);
if(ss=='-')
{
flag=1; //falg为 1 时表示输入为负数
}
else
{
if(ss!='/')
m+=ss;
else
break;
}
}
for(i=i+1; i<s.size(); i++)
{
ss=s.at(i);
n+=ss;
}
if(n == "") n="1"; //当输入的值为整数时,自动设分母为1
fractions=atoi(m.c_str());
numerator=atoi(n.c_str());
try{
if(numerator == 0)
throw "0";
}
catch(...)
{
ret = false;
cout<<"分母不能为零,请重新输入"<<endl;
set();
}
if(flag)
{
fractions=-fractions;
}
return ret;
}
//--------------------------------------------------
//------输出 ---------------------------------------
ostream & operator<<(ostream & o, const fraction & f )
{
if(f.numerator!=1)
{
o<<f.fractions<<'/';
return o<<f.numerator;
}
else
{
return o<<f.fractions;
}
}
//-----------------------------------------------------
//------约分-------------------------------------------
void fraction::reduction()
{
int x;
int y;
int temp_x;
int temp_y;
int gcd;
temp_x=fractions;
temp_y=numerator;
x=abs(fractions);
y=abs(numerator);
if(x<y)
{
gcd=y;
while(x>0)
{ gcd=x;
x=y%x;
y=gcd;
}
}
else
{
gcd=x;
while(y>0)
{ gcd=y;
y=x%y;
x=gcd;
}
}
if(temp_x>0)
{
fractions=temp_x/gcd;
numerator=temp_y/gcd;
}
else
{
fractions=(-temp_x)/gcd;
numerator=temp_y/gcd;
fractions=-fractions;
}
}
//-----------------------------------------------------
//------求倒数-----------------------------------------
void fraction::reciprocal()
{
int temp;
if(fractions<0)
{
temp=-numerator;
numerator=-fractions;
fractions=temp;
}
else
{
temp=numerator;
numerator=fractions;
fractions=temp;
}
}
//------分数间的运算------------------------------------
//------------------------------------------------------
//------加法---------------------------------------
fraction fraction :: operator+(const fraction a)
{
int x;
int y; //a的分子和分母
int m;
int n; //b的分子和分母
int temp1;
int temp2;
fraction result;
x=fractions;
y=numerator;
m=a.fractions;
n=a.numerator;
temp2=y*n;//result的分母
temp1=x*n+m*y;//result的分子
result.fractions=temp1;
result.numerator=temp2;
result.reduction(); //约分
return result;
}
//------------------------------------------------------
//------减法--------------------------------------------
fraction fraction :: operator-(const fraction a)
{
int x;
int y; //a的分子和分母
int m;
int n; //b的分子和分母
int temp1;
int temp2;
fraction result;
x=fractions;
y=numerator;
m=a.fractions;
n=a.numerator;
temp2=y*n;//result的分母
temp1=x*n-m*y;//result的分子
result.fractions=temp1;
result.numerator=temp2;
result.reduction(); //约分
return result;
}
//------------------------------------------------------
//------乘法--------------------------------------------
fraction fraction :: operator*(const fraction a)
{
int x;
int y; //a的分子和分母
int m;
int n; //b的分子和分母
int temp1;
int temp2;
fraction result;
x=fractions;
y=numerator;
m=a.fractions;
n=a.numerator;
if( x < 0 && m < 0) //判断当乘数和被乘数同为负数时,结果为正
{
x = -x;
m = -m;
}
temp2=y*n;//result的分母
temp1=x*m;//result的分子
result.fractions=temp1;
result.numerator=temp2;
result.reduction(); //约分
return result;
}
//------------------------------------------------------
//------除法--------------------------------------------
fraction fraction :: operator/(const fraction a)
{
int x;
int y; //a的分子和分母
int m;
int n; //b的分子和分母
int temp1;
int temp2;
fraction result;
x=fractions;
y=numerator;
m=a.fractions;
n=a.numerator;
if( x < 0 && m < 0) //判断当除数和被除数同为负数时,结果为正
{
x = -x;
m = -m;
}
temp2=y*m;//result的分母
temp1=x*n;//result的分子
result.fractions=temp1;
result.numerator=temp2;
result.reduction(); //约分
return result;
}
//--------------------------------------------------------
//------" > "--------------------------------------------
bool fraction::operator>( const fraction a)
{
fraction b;
fraction c;
b.fractions=fractions;
b.numerator=numerator;
c=b-a;
if(c.fractions>0)
return 1;
else
return 0;
}
//--------------------------------------------------------
//------" < "--------------------------------------------
bool fraction::operator<( const fraction a)
{
fraction b;
fraction c;
b.fractions=fractions;
b.numerator=numerator;
c=b-a;
if(c.fractions<0)
return 1;
else
return 0;
}
//--------------------------------------------------------
//------" >= "--------------------------------------------
bool fraction::operator>=( const fraction a)
{
fraction b;
fraction c;
b.fractions=fractions;
b.numerator=numerator;
c=b-a;
if(c.fractions>=0)
return 1;
else
return 0;
}
//--------------------------------------------------------
//------" <= "--------------------------------------------
bool fraction::operator<=( const fraction a)
{
fraction b;
fraction c;
b.fractions=fractions;
b.numerator=numerator;
c=b-a;
if(c.fractions<=0)
return 1;
else
return 0;
}
//--------------------------------------------------------
//------" == "--------------------------------------------
bool fraction::operator==( const fraction a)
{
fraction b;
fraction c;
b.fractions=fractions;
b.numerator=numerator;
c=b-a;
if(c.fractions==0)
return 1;
else
return 0;
}
//--------------------------------------------------------
//------" != "--------------------------------------------
bool fraction::operator!=( const fraction a)
{
fraction b;
fraction c;
b.fractions=fractions;
b.numerator=numerator;
c=b-a;
if(c.fractions!=0)
return 1;
else
return 0;
}
//---------------------------------------------------------------------------------------------------------------------
//------分数与整数间的运算---------------------------------------------------------------------------------------------
//------加法---------------------------------------
fraction fraction :: operator+(const int k)
{
int x;
int y; //a的分子和分母
int m;
int n; //b的分子和分母
int temp1;
int temp2;
fraction result;
x=fractions;
y=numerator;
m=k;
n=1;
temp2=y*n;//result的分母
temp1=x*n+m*y;//result的分子
result.fractions=temp1;
result.numerator=temp2;
result.reduction(); //约分
return result;
}
//------------------------------------------------------
//------减法--------------------------------------------
fraction fraction :: operator-(const int k)
{
int x;
int y; /