问题及代码:
/*
*Copyright (c)2015,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:cfraction.cpp
*作 者:赵敏
*完成日期:2015年5月14日
*版 本 号:v1.0
*
*问题描述:1)定义分数的一目运算+和-,分别代表分数取正和求反,将“按位取反运算符”~重载为分数的求倒数运算。
(2)定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。
*/
#include <iostream>
#include <Cmath>
using namespace std;
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
CFraction(int nu=0,int de=1); //构造函数,初始化用
void simplify();
//构造函数及运算符重载的函数声明
int gcd(int,int);
CFraction operator+(const CFraction &c);
CFraction operator-(const CFraction &c);
CFraction operator*(const CFraction &c);
CFraction 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);
bool operator<=(const CFraction &c);
friend istream &operator>>(istream &input,CFraction &x);
friend ostream &operator<<(ostream &output,CFraction x);
CFraction operator+(); //取正
CFraction operator-(); //取反
CFraction operator~(); //取倒数
};
//重载函数的实现及用于测试的main()函数
CFraction::CFraction(int nu,int de)
{
if(de!=0)
{
nume=nu;
deno=de;
}
}
void CFraction::simplify()
{
int a=gcd(nume,deno);
nume=nume/a;
deno=deno/a;
}
int CFraction::gcd(int m, int n)
{
int r;
do
{
r=m%n;
m=n;
n=r;
}while(r!=0);
return m;
}
CFraction CFraction::operator+(const CFraction &c)
{
CFraction t;
t.nume=nume*c.deno+deno*c.nume;
t.deno=deno*c.deno;
t.simplify();
return t;
}
CFraction CFraction::operator-(const CFraction &c)
{
CFraction t;
t.nume=nume*c.deno-deno*c.nume;
t.deno=deno*c.deno;
t.simplify();
return t;
}
CFraction CFraction::operator*(const CFraction &c)
{
CFraction t;
t.nume=nume*c.nume;
t.deno=deno*c.deno;
t.simplify();
return t;
}
CFraction CFraction::operator/(const CFraction &c)
{
CFraction t;
if (!c.nume) return *this;
t.nume=nume*c.deno;
t.deno=deno*c.nume;
t.simplify();
return t;
}
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)||(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)||(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 false;
return true;
}
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;
}
istream &operator>>(istream &input,CFraction &x)
{
char c;
do{
cout<<"input cfraction: ";
input>>x.nume>>c>>x.deno;
}while(!(c=='/'));
return input;
}
ostream &operator<<(ostream &output,CFraction x)
{
output<<x.nume<<"/"<<x.deno;
return output;
}
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;
if(x.deno<0)
{
x.deno=-x.deno;
x.nume=-x.nume;
}
return x;
}
int main()
{
CFraction x,y,s;
cin>>x>>y;
cout<<"分数x=1/3 y=-5/10"<<endl;
s=x+y;
cout<<"x+y=";
cout<<s<<endl;
s=x-y;
cout<<"x-y=";
cout<<s<<endl;;
s=x*y;
cout<<"x*y=";
cout<<s<<endl;
s=x/y;
cout<<"x/y=";
cout<<s<<endl;
cout<<x<<endl;
if (x>y) cout<<"大于"<<endl;
if (x<y) cout<<"小于"<<endl;
if (x==y) cout<<"等于"<<endl;
cout<<y;
cout<<endl;
return 0;
}
运行结果:
知识点总结:
重载单目运算符