/*
*Copyright(C) 2016,计算机与控制工程学院
*All rights reserved.
*文件名:zhang.cpp
*作者:张志新
*完成日期:2016年5月30日
*版本号:v1.0*
*问题描述:实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)
* 比较(6种关系)的运算
*/
#include <iostream>
#include <Cmath>
using namespace std;
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
CFraction(int nu=0,int de=0):nume(nu),deno(de) {}
void simplify();
friend istream &operator>>(istream &in,CFraction &x);
friend ostream &operator<<(ostream &out,CFraction x);
CFraction operator+(const CFraction &c);
CFraction operator-(const CFraction &c);
CFraction operator*(const CFraction &c);
CFraction operator/(const CFraction &c);
CFraction operator+();
CFraction operator-();
CFraction operator~();
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);
};
void CFraction::simplify()
{
int m,n,r;
n=fabs(deno);
m=fabs(nume);
while(r=n%m) // 求m,n的最大公约数
{
m=n;
n=r;
}
deno/=n; // 化简
nume/=n;
if (deno<0) // 将分母转化为正数
{
deno=-deno;
nume=-nume;
}
}
istream &operator>>(istream &in,CFraction &x)
{
char c;
while(1)
{
cin>>x.nume>>c>>x.deno;
if(x.deno==0)
cerr<<"分母为零请重新输入:";//不加endl
else if(c!='/')
cerr<<"格式错误请重新输入:";
else
break;
}
return cin;
}
ostream &operator<<(ostream &out,CFraction x)
{
cout<<x.nume<<"/"<<x.deno;
return cout;
}
CFraction CFraction::operator+(const CFraction &c)
{
CFraction c0;
c0.nume=nume*c.deno+c.nume*deno;
c0.deno=deno*c.deno;
c0.simplify();
return c0;
}
CFraction CFraction::operator-(const CFraction &c)
{
CFraction c0;
c0.nume=nume*c.deno-c.nume*deno;
c0.deno=deno*c.deno;
c0.simplify();
return c0;
}
CFraction CFraction::operator*(const CFraction &c)
{
CFraction c0;
c0.nume=nume*c.nume;
c0.deno=deno*c.deno;
c0.simplify();
return c0;
}
CFraction CFraction::operator/(const CFraction &c)
{
CFraction c0;
if (!c.nume) return *this;//还不大明白这里
c0.nume=nume*c.nume;
c0.deno=deno*c.deno;
c0.simplify();
return c0;
}
// 分数取正号
CFraction CFraction:: operator+()
{
return *this;
}
// 分数取负号
CFraction CFraction:: operator-()
{
CFraction x;
x.nume=-nume;
x.deno=deno;
return x;
}
// 分数取倒数
CFraction CFraction:: operator~()
{
CFraction x;
x.nume=deno;
x.deno=nume; //未对原分子为0的情况进行处理
if(x.deno<0) //保证负分数的负号在分子上
{
x.deno=-x.deno;
x.nume=-x.nume;
}
return x;
}
// 分数比较大小
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 x,y,s;
cout<<"输入x: ";
cin>>x;
cout<<"输入y: ";
cin>>y;
s=x+y;
cout<<"x+y="<<s<<endl;
s=x-y;
cout<<"x-y="<<s<<endl;
s=x*y;
cout<<"x*y="<<s<<endl;
s=x/y;
cout<<"x/y="<<s<<endl;
cout<<"-x="<<-x<<endl;
cout<<"+x="<<+x<<endl;
cout<<"x的倒数: "<<~x<<endl;
cout<<x;
if (x>y)
cout<<"大于";
if (x<y)
cout<<"小于";
if (x==y)
cout<<"等于";
cout<<y<<endl;
return 0;
}
学习心得:
这个程序是运用的是运算符重载分数的加减乘除,还有要注意的是在计算时算法,在比较分数的大小时,还有要注意熟悉cin与cout的运算的重载(注意再写重载函数是不写endl),还是不要太熟悉,在就是比较大小的函数要已经有了>的重载时在写<=时就是比较简单了。