*/
* Copyright (c) 2016,烟台大学计算机与控制工程学院
* 文件名:app.cpp
* 作者:杨栋
* 版本号:V1.0
* 问题描述:分数类的重载
*/
#include<iostream>
#include<Cmath>
using namespace std;
class CFraction{
private:
int nume; //分子
int deno; //分母
public:
CFraction(int nu=0,int de=0);
CFraction operator+(const CFraction &n); //分数相加
CFraction operator-(const CFraction &n); //分数相减
CFraction operator*(const CFraction &n); //分数相乘
CFraction operator/(const CFraction &n); //分数相除
void display(); //输出分数
void simplify(); //分数化简
bool operator>(const CFraction &c);
bool operator<(const CFraction &c);
bool operator==(const CFraction &c);
bool operator!=(const CFraction &c);
bool operator>=(const CFraction &c);
bool operator<=(const CFraction &c);
};
CFraction::CFraction(int nu,int de) //构造函数
{
nume=nu;
deno=de;
}
void CFraction::display() //输出函数
{
cout<<nume<<"/"<<deno<<endl;
}
void CFraction::simplify() //分数化简
{
int m,n,r;
n=fabs(deno);
m=fabs(nume);
if(nume==0)
deno=0;
else{
while(r=m%n) // 求m,n的最大公约数
{
m=n;
n=r;
}
deno/=n; // 化简
nume/=n;
if (deno<0) // 将分母转化为正数
{
deno=-deno;
nume=-nume;
}
}
}
CFraction CFraction::operator +(const CFraction &n) //定义分数相加
{
CFraction t;
t.deno=this->deno*n.deno;
t.nume=this->nume*n.deno+n.nume*this->deno;
t.simplify();//化简
return t;
}
CFraction CFraction::operator -(const CFraction &n) //定义分数相减
{
CFraction t;
t.deno=this->deno*n.deno;
t.nume=this->nume*n.deno-n.nume*this->deno;
t.simplify();//化简
return t;
}
CFraction CFraction::operator *(const CFraction &n) //定义分数相乘
{
CFraction t;
t.deno=n.deno*this->deno;
t.nume=n.nume*this->nume;
t.simplify();//化简
return t;
}
CFraction CFraction::operator /(const CFraction &n) //定义分数相除
{
CFraction t;
t.deno=n.nume*this->deno;
t.nume=n.deno*this->nume;
t.simplify();//化简
return t;
}
//比较运算符重载
bool CFraction::operator >(const CFraction &c) // >重载
{
int this_nume,c_nume,common_deno;
this_nume=nume*c.deno; // 计算分数通分后的分子,同分母为deno*c.deno
c_nume=c.nume*deno;
common_deno=deno*c.deno;
if ((this_nume-c_nume)*common_deno>0) return true;
return false;
}
bool CFraction::operator<(const CFraction &c)
{
int this_nume,c_nume,common_deno;
this_nume=nume*c.deno;
c_nume=c.nume*deno;
common_deno=deno*c.deno;
if ((this_nume-c_nume)*common_deno<0) return true;
return false;
}
// 分数比较大小
bool CFraction::operator==(const CFraction &c)
{
if (*this!=c) return false;
return true;
}
// 分数比较大小
bool CFraction::operator!=(const CFraction &c)
{
if (*this>c || *this<c) return true;
return false;
}
// 分数比较大小
bool CFraction::operator>=(const CFraction &c)
{
if (*this<c) return false;
return true;
}
// 分数比较大小
bool CFraction::operator<=(const CFraction &c)
{
if (*this>c) return false;
return true;
}
int main()
{
CFraction a(2,3),b(1,3);
CFraction c;
c=a+b;
cout<<"c=";
c.display();
c=a*b;
cout<<"c=";
c.display();
c=a-b;
cout<<"c=";
c.display();
c=a/b;
cout<<"c=";
c.display();
if(a>b)
cout<<"a>b";
return 0;
}
* Copyright (c) 2016,烟台大学计算机与控制工程学院
* 文件名:app.cpp
* 作者:杨栋
* 版本号:V1.0
* 问题描述:分数类的重载
*/
#include<iostream>
#include<Cmath>
using namespace std;
class CFraction{
private:
int nume; //分子
int deno; //分母
public:
CFraction(int nu=0,int de=0);
CFraction operator+(const CFraction &n); //分数相加
CFraction operator-(const CFraction &n); //分数相减
CFraction operator*(const CFraction &n); //分数相乘
CFraction operator/(const CFraction &n); //分数相除
void display(); //输出分数
void simplify(); //分数化简
bool operator>(const CFraction &c);
bool operator<(const CFraction &c);
bool operator==(const CFraction &c);
bool operator!=(const CFraction &c);
bool operator>=(const CFraction &c);
bool operator<=(const CFraction &c);
};
CFraction::CFraction(int nu,int de) //构造函数
{
nume=nu;
deno=de;
}
void CFraction::display() //输出函数
{
cout<<nume<<"/"<<deno<<endl;
}
void CFraction::simplify() //分数化简
{
int m,n,r;
n=fabs(deno);
m=fabs(nume);
if(nume==0)
deno=0;
else{
while(r=m%n) // 求m,n的最大公约数
{
m=n;
n=r;
}
deno/=n; // 化简
nume/=n;
if (deno<0) // 将分母转化为正数
{
deno=-deno;
nume=-nume;
}
}
}
CFraction CFraction::operator +(const CFraction &n) //定义分数相加
{
CFraction t;
t.deno=this->deno*n.deno;
t.nume=this->nume*n.deno+n.nume*this->deno;
t.simplify();//化简
return t;
}
CFraction CFraction::operator -(const CFraction &n) //定义分数相减
{
CFraction t;
t.deno=this->deno*n.deno;
t.nume=this->nume*n.deno-n.nume*this->deno;
t.simplify();//化简
return t;
}
CFraction CFraction::operator *(const CFraction &n) //定义分数相乘
{
CFraction t;
t.deno=n.deno*this->deno;
t.nume=n.nume*this->nume;
t.simplify();//化简
return t;
}
CFraction CFraction::operator /(const CFraction &n) //定义分数相除
{
CFraction t;
t.deno=n.nume*this->deno;
t.nume=n.deno*this->nume;
t.simplify();//化简
return t;
}
//比较运算符重载
bool CFraction::operator >(const CFraction &c) // >重载
{
int this_nume,c_nume,common_deno;
this_nume=nume*c.deno; // 计算分数通分后的分子,同分母为deno*c.deno
c_nume=c.nume*deno;
common_deno=deno*c.deno;
if ((this_nume-c_nume)*common_deno>0) return true;
return false;
}
bool CFraction::operator<(const CFraction &c)
{
int this_nume,c_nume,common_deno;
this_nume=nume*c.deno;
c_nume=c.nume*deno;
common_deno=deno*c.deno;
if ((this_nume-c_nume)*common_deno<0) return true;
return false;
}
// 分数比较大小
bool CFraction::operator==(const CFraction &c)
{
if (*this!=c) return false;
return true;
}
// 分数比较大小
bool CFraction::operator!=(const CFraction &c)
{
if (*this>c || *this<c) return true;
return false;
}
// 分数比较大小
bool CFraction::operator>=(const CFraction &c)
{
if (*this<c) return false;
return true;
}
// 分数比较大小
bool CFraction::operator<=(const CFraction &c)
{
if (*this>c) return false;
return true;
}
int main()
{
CFraction a(2,3),b(1,3);
CFraction c;
c=a+b;
cout<<"c=";
c.display();
c=a*b;
cout<<"c=";
c.display();
c=a-b;
cout<<"c=";
c.display();
c=a/b;
cout<<"c=";
c.display();
if(a>b)
cout<<"a>b";
return 0;
}