一個簡單的分數類
就是寫得太長了 可以更精簡些的
#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 & ); //約分函數
int gcd(int a,int b); //求最大公約數函數
}; 

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


{
m_fractionSon = fractionSon; //構造函數不能有返回類型,
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; //除以一個數等價于乘上這個數的倒數
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<<"請輸入分數a的分子:"<<endl;
cin>>ason;
cout<<"請輸入分數a的分母:"<<endl;
cin>>amom;
if ( amom == 0 )

{
cout<<"分母不能為0!"<<endl;
return 0;
}
cout<<"請輸入分數b的分子:"<<endl;
cin>>bson;
cout<<"請輸入分數b的分母:"<<endl;
cin>>bmom;
if ( bmom == 0 )

{
cout<<"分母不能為0!"<<endl;
return 0;
}
CFraction a(ason,amom);
CFraction b(bson,bmom); 
cout<<"分數 a = "<< a <<endl;
cout<<"分數 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;
} 

轉自:http://blog.csdn.net/marine_lich/archive/2008/05/15/2449778.aspxposted on 2010-04-16 18:09 abilitytao 閱讀(254) 評論(0) 編輯 收藏 引用

