问题及代码:
/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:Project.cpp
*作 者:chenqin.
*完成日期:2015年5月20日
*版 本 号: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):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=m%n)
{
m=n;
n=r;
}
deno/=n;
nume/=n;
if(deno<0)
{
deno=-deno;
nume=-nume;
}
}
//重载运算符“>>”“<<”
istream &operator>>(istream &in,CFraction &x)
{
char ch;
while(1)
{
cin>>x.nume>>ch>>x.deno;
if(x.deno==0)
cerr<<"分母不能为零,请重新输入:";
else if(ch!='/')
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 t;
t.nume=nume*c.deno+c.nume*deno;
t.deno=deno*c.deno;
t.simplify();
return t;
}
CFraction CFraction::operator-(const CFraction&c)
{
CFraction t;
t.nume=nume*c.deno-c.nume*deno;
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;
t.nume=nume*c.deno;
t.deno=deno*c.nume;
t.simplify();
return t;
}
//一目运算符
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)
cout<<"分母不能为零"<<endl;
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;
c_nume=c.nume*deno;
common_deno=deno*c.deno;
if((this_nume-c_nume)*common_deno>0)
return true;
else
return false;
}
bool CFraction::operator<(const CFraction &c)
{
int this_nume,c_nume,common_deno;
this_nume=nume*c.deno;
c_nume=deno*c.nume;
common_deno=deno*c.deno;
if((this_nume-c_nume)*common_deno<0)
return true;
else
return false;
}
bool CFraction::operator==(const CFraction &c)
{
if(*this!=c)
return false;
else
return
true;
}
bool CFraction::operator!=(const CFraction &c)
{
if(*this>c||*this<c)
return true;
else
return false;
}
bool CFraction::operator>=(const CFraction &c)
{
if(*this<c)
return false;
else
return true;
}
bool CFraction::operator<=(const CFraction &c)
{
if(*this>c)
return false;
else
return true;
}
int main()
{
CFraction x,y,z;
cout<<"请输入x:";
cin>>x;
cout<<"请输入y:";
cin>>y;
z=x+y;
cout<<"x+y="<<z<<endl;
z=x-y;
cout<<"x-y="<<z<<endl;
z=x*y;
cout<<"x*y="<<z<<endl;
z=x/y;
cout<<"x/y="<<z<<endl;
cout<<"-x="<<-x<<endl;
cout<<"+x="<<+x<<endl;
cout<<"x的倒数="<<~x<<endl;
cout<<x;
if(x>y)
cout<<"大于"<<endl;
if(x<y)
cout<<"小于"<<endl;
if(x==y)
cout<<"等于"<<endl;
cout<<y;
cout<<endl;
return 0;
}
运行结果:
学习心得:
注意friend ostream &operator<<(ostream &out,CFraction x)这一行,若在x前加上引用符号则会出现大量编译错误,导致我整个人无语了好大半天。