问题及代码:
运行结果:
分数类中的运算符重载实现分数的加减乘除并化简。
/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:Fraction.cpp
*作 者:单昕昕
*完成日期:2015年4月27日
*版 本 号:v1.0
*问题描述:实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、比较(6种关系)的运算。可以在第4周分数类代码的基础上开始工作。
*程序输入:无。
*程序输出:分数。
*/
#include <iostream>
using namespace std;
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
CFraction(int nu=0,int de=1); //构造函数,初始化用
void set(int nu=0,int de=1); //置值,改变值时用
void input(); //按照"nu/de"的格式,如"5/2"的形式输入
void simplify(); //化简(使分子分母没有公因子)
void display();
bool operator > (CFraction &c);
bool operator < (CFraction &c);
bool operator >= (CFraction &c);
bool operator <= (CFraction &c);
bool operator == (CFraction &c);
bool operator != (CFraction &c);
CFraction operator + (const CFraction &c2);
CFraction operator - (const CFraction &c2);
CFraction operator * (const CFraction &c2);
CFraction operator / (const CFraction &c2);
};
int gys(int a,int b)
{
return (a%b!=0?(gys(b,a%b)):b);
}
int gbs(int u,int v)
{
int h;
h=gys(u,v);
return (u*v/h);
}
CFraction::CFraction(int nu,int de)
{
if(de!=0)
{
nume=nu;
deno=de;
}
}
void CFraction::set(int nu,int de)//置值,改变值时用
{
if(de!=0)
{
nume=nu;
deno=de;
}
}
bool CFraction::operator > (CFraction &c)
{
int r;
if (deno!=c.deno) //将分数化成相同分母便于比较
{
r=gbs(deno,c.deno);
if(nume*r/deno>c.nume*r/c.deno)
return true;
else
return false;
}
else
{
if(nume>c.nume)
return true;
else
return false;
}
}
bool CFraction::operator < (CFraction &c)
{
int r;
if (deno!=c.deno) //将分数化成相同分母便于比较
{
r=gbs(deno,c.deno);
if(nume*r/deno<c.nume*r/c.deno)
return true;
else
return false;
}
else
{
if(nume<c.nume)
return true;
else
return false;
}
}
bool CFraction::operator >= (CFraction &c)
{
return !(*this < c);
}
bool CFraction::operator <= (CFraction &c)
{
return !(*this > c);
}
bool CFraction::operator == (CFraction &c)
{
return !((*this > c)||(*this < c));
}
bool CFraction::operator != (CFraction &c)
{
return ((*this > c)||(*this < c));
}
CFraction CFraction::operator + (const CFraction &c2)
{
CFraction c;
int r;
if (deno!=c.deno) //取分母的最大公倍数
{
r=gbs(deno,c2.deno);
c.nume=(nume*r/deno)+(c2.nume*r/c2.deno);
c.deno=r;
}
else
{
c.nume=nume+c2.nume;
c.deno=deno;
}
return c;
}
CFraction CFraction::operator - (const CFraction &c2)
{
CFraction c;
int r;
if (deno!=c.deno) //取分母的最大公倍数
{
r=gbs(deno,c2.deno);
c.nume=(nume*r/deno)-(c2.nume*r/c2.deno);
c.deno=r;
}
else
{
c.nume=nume-c2.nume;
c.deno=deno;
}
return c;
}
CFraction CFraction::operator * (const CFraction &c2)
{
CFraction c;
c.nume=nume*c2.nume;
c.deno=deno*c2.deno;
return c;
}
CFraction CFraction::operator / (const CFraction &c2)
{
CFraction c;
c.nume=nume*c2.deno;
c.deno=deno*c2.nume;
return c;
}
void CFraction::simplify()//化简(使分子分母没有公因子)
{
int r;
r=gys(nume,deno);
nume/=r;
deno/=r;
}
void CFraction::display()
{
simplify();
if(deno!=1)
cout<<nume<<"/"<<deno;
else
cout<<nume;
}
int main()
{
CFraction x(-2,3),y(6,10),s;
cout<<"两个分数是:"<<endl;
x.display();
cout<<'\t';
y.display();
cout<<endl;
cout<<"下面比较两个分数的大小:"<<endl;
if (x>y)
{
x.display();
cout<<">";
y.display();
cout<<endl;
}
if (x<y)
{
x.display();
cout<<"<";
y.display();
cout<<endl;
}
if (x==y)
{
x.display();
cout<<"=";
y.display();
cout<<endl;
}
if (x!=y)
{
x.display();
cout<<"≠";
y.display();
cout<<endl;
}
if (x>=y)
{
x.display();
cout<<"≥";
y.display();
cout<<endl;
}
if (x<=y)
{
x.display();
cout<<"≤";
y.display();
cout<<endl;
}
cout<<"下面对两个分数进行计算:"<<endl;
s=x+y;
cout<<"x+y=";
s.display();
cout<<endl;
s=x-y;
cout<<"x-y=";
s.display();
cout<<endl;
s=x*y;
cout<<"x*y=";
s.display();
cout<<endl;
s=x/y;
cout<<"x/y=";
s.display();
cout<<endl;
return 0;
}
运行结果:
分数类中的运算符重载实现分数的加减乘除并化简。
学习心得:
分数主要是要将分母化成一样的,这样方便比较和计算,所以我定义了求最大公倍数的普通函数用于计算。