就是寫得太長(zhǎng)了 可以更精簡(jiǎn)些的
#include <iostream>
using namespace std;
class CFraction



{
private:
int m_fractionSon; //分子
int m_fractionMother; //分母

friend ostream &operator<<(ostream &output,const CFraction &fraction)


{
output<<fraction.m_fractionSon<<"/"<<fraction.m_fractionMother<<endl;
return output;
}
friend istream &operator>>(istream &input, CFraction &fraction)


{
input>>fraction.m_fractionSon;
input>>fraction.m_fractionMother;
return input;
}

public:
CFraction(int=1 ,int=1 );
const CFraction operator+( const CFraction & );
const CFraction operator-( const CFraction & );
const CFraction operator*( const CFraction & );
const CFraction operator/( const CFraction & );
bool operator<( const CFraction & );
bool operator>( const CFraction & );
bool operator==( const CFraction & );
bool operator!=( const CFraction & );
CFraction Easiest(CFraction & ); //約分函數(shù)
int gcd(int a,int b); //求最大公約數(shù)函數(shù)

};


//類成員函數(shù)的定義
CFraction::CFraction(int fractionSon ,int fractionMother )



{
m_fractionSon = fractionSon; //構(gòu)造函數(shù)不能有返回類型,
m_fractionMother = fractionMother;
}

const CFraction CFraction::operator+( const CFraction &rightNumber )



{
CFraction resultValue;
resultValue.m_fractionSon = m_fractionSon * rightNumber.m_fractionMother + m_fractionMother * rightNumber.m_fractionSon;
resultValue.m_fractionMother = m_fractionMother * rightNumber.m_fractionMother;
return Easiest( resultValue );
}

const CFraction CFraction::operator-( const CFraction &rightNumber )



{
CFraction resultValue;
resultValue.m_fractionSon = m_fractionSon * rightNumber.m_fractionMother - m_fractionMother * rightNumber.m_fractionSon;
resultValue.m_fractionMother = m_fractionMother * rightNumber.m_fractionMother;
return Easiest( resultValue );
}

const CFraction CFraction:: operator*(const CFraction &rightNumber )



{
CFraction resultValue;
resultValue.m_fractionSon = m_fractionSon * rightNumber.m_fractionSon;
resultValue.m_fractionMother = m_fractionMother * rightNumber.m_fractionMother;
return Easiest( resultValue );
}

const CFraction CFraction::operator/( const CFraction &rightNumber )



{
CFraction resultValue;
resultValue.m_fractionSon = m_fractionSon * rightNumber.m_fractionMother;
resultValue.m_fractionMother = m_fractionMother * rightNumber.m_fractionSon; //除以一個(gè)數(shù)等價(jià)于乘上這個(gè)數(shù)的倒數(shù)
return Easiest( resultValue );
}

bool CFraction::operator<( const CFraction &rightNumber )



{
int leftNumberFractionSon,rightNumberFractionSon;
leftNumberFractionSon = m_fractionSon * rightNumber.m_fractionMother;
rightNumberFractionSon = rightNumber.m_fractionSon * m_fractionMother;
if ( leftNumberFractionSon < rightNumberFractionSon )


{
return true;
}
else


{
return false;
}
}

bool CFraction::operator>( const CFraction &rightNumber)



{
int leftNumberFractionSon,rightNumberFractionSon;
leftNumberFractionSon = m_fractionSon * rightNumber.m_fractionMother;
rightNumberFractionSon = rightNumber.m_fractionSon * m_fractionMother;
if ( leftNumberFractionSon > rightNumberFractionSon )


{
return true;
}
else


{
return false;
}

}

bool CFraction::operator==( const CFraction &rightNumber )



{
int leftNumberFractionSon,rightNumberFractionSon;
leftNumberFractionSon = m_fractionSon * rightNumber.m_fractionMother;
rightNumberFractionSon = rightNumber.m_fractionSon * m_fractionMother;
if ( leftNumberFractionSon == rightNumberFractionSon )


{
return true;
}
else


{
return false;
}
}

CFraction CFraction::Easiest( CFraction &fraction )



{
int getgcd = gcd(fraction.m_fractionSon, fraction.m_fractionMother);
CFraction value(fraction.m_fractionSon/getgcd, fraction.m_fractionMother/getgcd);
return value;
}

int CFraction::gcd(int a,int b)



{
while(b)


{
int temp = b;
b = a%b;
a = temp;
}
return a;
}
int main()



{
int ason, amom, bson, bmom;
cout<<"請(qǐng)輸入分?jǐn)?shù)a的分子:"<<endl;
cin>>ason;
cout<<"請(qǐng)輸入分?jǐn)?shù)a的分母:"<<endl;
cin>>amom;
if ( amom == 0 )


{
cout<<"分母不能為0!"<<endl;
return 0;
}
cout<<"請(qǐng)輸入分?jǐn)?shù)b的分子:"<<endl;
cin>>bson;
cout<<"請(qǐng)輸入分?jǐn)?shù)b的分母:"<<endl;
cin>>bmom;
if ( bmom == 0 )


{
cout<<"分母不能為0!"<<endl;
return 0;
}
CFraction a(ason,amom);
CFraction b(bson,bmom);

cout<<"分?jǐn)?shù) a = "<< a <<endl;
cout<<"分?jǐn)?shù) b = "<< b <<endl;
cout<<"a+b="<< a+b <<endl;
cout<<"a-b="<< a-b <<endl;
cout<<"a*b="<< a*b <<endl;
cout<<"a/b="<< a/b <<endl;
if(a>b)
cout<<"a大于b"<<endl;
else if (a<b)
cout<<"a小于b."<<endl;
else if(a==b)
cout<<"a等于b."<<endl;
return 0;
}


轉(zhuǎn)自:http://blog.csdn.net/marine_lich/archive/2008/05/15/2449778.aspx