C++ 分数类
class fraction
{
private:
int above; //分子
int below; //分母
void reduction(); //约分
fraction 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 i,comdiv,small,max;
if(this->above<this->below){
small=this->above;
max=this->below;
}else {
small=this->below;
max=this->above;
}
for(i=small;i>1;i--){
if(small%i==0 &max%i==0 )
break;
}
comdiv=i;//最大公约数
if(i!=0){
this->above/=i;
this->below/=i;
}
}
fraction fraction::makeCommond(fraction frac)
{
int b1=this->below,b2=frac.below, m,s;
if(b1>b2){
m=b1%b2;
s=b2;
}
else{
m=b2%b1;
s=b1;
}
while(m>0){
int res=s%m;
s=m,m=res;
}
int small=(b1*b2)/s;
this->above=this->above*(small/this->below);
frac.above=frac.above*(small/frac.below);
this->below=small;
frac.below=small;
return frac;
}
fraction fraction::add(fraction fr)
{
fraction myFraction;
myFraction.above=this->above*fr.below+fr.above*this->below;
myFraction.below=this->below*fr.below;
myFraction.reduction();
return myFraction;
}
fraction fraction::sub(fraction fr)
{
fraction myFraction;
myFraction.above=this->above*fr.below-fr.above*this->below;
myFraction.below=this->below*fr.below;
myFraction.reduction();
return myFraction;
}
fraction fraction::mul(fraction fr)
{
fraction myFraction;
myFraction.above=this->above*fr.above;
myFraction.below=this->below*fr.below;
myFraction.reduction();
return myFraction;
}
fraction fraction::div(fraction fr)
{
fraction myFraction;
myFraction.above=this->above*fr.below;
myFraction.below=this->below*fr.above;
myFraction.reduction();
return myFraction;
}
fraction fraction::reciprocal()
{
fraction myFraction(this->above,this->below);
if(this->above!=0){
int temp=myFraction.above;
myFraction.above=myFraction.below;
myFraction.below=temp;
}
return myFraction;
}
bool fraction::equal(fraction frac) //等于运算
{
bool result=false;
if(this->sub(frac).above==0)
result=true;
return result;
}
bool fraction::greaterThan(fraction frac) //大于运算
{
bool result=false;
if(this->sub(frac).above>0)
result=true;
return result;
}
bool fraction::lessThan(fraction frac) //小于运算
{
bool result=false;
if(this->sub(frac).above<0)
result=true;
return result;
}
void fraction::display()
{
cout<<this->above<<endl<<"---"<<endl<<this->below<<endl<<endl<<endl;
}
void fraction::input() //输入分数
{
cout<<"请输入分数的分子: ";
cin>>this->above;
while(true)
{
cout<<"请输入分数的分母(不等于零): ";
cin>>this->below;
if(this->below==0)
{
cout<<"分母不能为零!";
}
else
break;
};
}
int main()
{
//fraction frac(5,24),frac2(7,16);
//fraction frac(7,9),frac2(8,11);
fraction frac(5,20),frac2(50,200);
frac.input();
frac2.input();
frac.display();
frac2.display();
cout<<"分数加法"<<endl;
frac.add(frac2).display();
cout<<"分数减法"<<endl;
frac.sub(frac2).display();
cout<<"分数乘法"<<endl;
frac.mul(frac2).display();
cout<<"分数除法"<<endl;
frac.div(frac2).display();
cout<<"分数倒数"<<endl;
frac.reciprocal().display();
char *str;
cout<<"分数相等比较: ";
if(frac.equal(frac2))
str="相等";
else
str="不相等";
cout<<str<<endl<<endl;
cout<<"分数大于比较: ";
if(frac.greaterThan(frac2))
str="大";
else
str="小";
cout<<str<<endl<<endl;
cout<<"分数小于比较:";
if(frac.lessThan(frac2))
str="小";
else
str="大";
cout<<str<<endl<<endl;
return 0;
}