The Fourth Dimension Space

枯叶北风寒,忽然年以残,念往昔,语默心酸。二十光阴无一物,韶光贱,寐难安; 不畏形影单,道途阻且慢,哪曲折,如渡飞湍。斩浪劈波酬壮志,同把酒,共言欢! -如梦令

一个简单的分数类

就是写得太长了 可以更精简些的
#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+<<endl;  
 cout
<<"a-b="<< a-<<endl;  
 cout
<<"a*b="<< a*<<endl;  
 cout
<<"a/b="<< a/<<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.aspx

posted on 2010-04-16 18:09 abilitytao 阅读(226) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理