/*(文件注释头部开始)
*程序的版权和版本声明部分
*Copyright (c) 2011,烟台大学计算机学院学生
*All rights reserved.
*文件名称:定义分数类中<<和>>运算符重载
*作 者:王智凯
*完成日期:2011年4月16号
*版本号:凯子
* 对任务及求解方法的描述部分
* 输入描术:
* 问题描述:在重载运算符+、-、*、/的基础上,增加重载运算符>> << ,实现输入和输出。
* 程序输出:
* 程序头部的注释结束
*/
#include <iostream>
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&input,CFraction &);
friend ostream&operator <<(ostream&output,CFraction &);
CFraction operator+(const CFraction &c);
CFraction operator-(const CFraction &c);
CFraction operator*(const CFraction &c);
CFraction operator/(const CFraction &c);
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);
};
// 分数化简
void CFraction::simplify() //借鉴的老师的方法;
{
int m,n,r;
m=abs(deno);
n=abs(nume);
while(r=m%n) // 求m,n的最大公约数
{
m=n;
n=r;
}
deno/=n; // 化简
nume/=n;
if (deno<0) // 将分母转化为正数
{
deno=-deno;
nume=-nume;
}
}
istream&operator >>(istream&input,CFraction &c)
{
input >> c.nume >> c.deno ;
return input;
}
ostream&operator <<(ostream&output,CFraction &c)
{
output << "(" <<c.nume << "/" << c.deno <<")";
return output;
}
CFraction CFraction::operator+(const CFraction &c)
{
CFraction t;
t.deno = c.deno * deno;
t.nume = deno * c.nume + c.deno * nume;
t.simplify();
return t;
}
CFraction CFraction::operator-(const CFraction &c)
{
CFraction t;
t.deno = c.deno * deno;
t.nume = c.deno * nume - deno * c.nume;
t.simplify();
return t;
}
CFraction CFraction::operator*(const CFraction &c)
{
CFraction t;
t.deno = c.deno * deno;
t.nume = nume * c.nume;
t.simplify();
return t;
}
CFraction CFraction::operator/(const CFraction &c)
{
CFraction t;
t.deno = c.nume * deno;
t.nume = nume * c.deno;
t.simplify();
return t;
}
CFraction CFraction::operator+()
{
CFraction t;
t.deno = deno;
t.nume = nume;
t.simplify();
return t;
}
CFraction CFraction::operator-()
{
CFraction t;
t.deno = deno;
t.nume = -nume;
t.simplify();
return t;
}
bool CFraction::operator>(const CFraction &c)
{
int t1_nume,t1_deno,t2_nume,t2_deno;
t1_nume = deno * c.nume ;
t1_deno = c.deno * deno;
t2_nume = c.deno * nume ;
t2_deno = c.deno * deno;
if(t1_nume > t2_nume)
return true;
else
return false;
}
bool CFraction::operator<(const CFraction &c)
{
int t1_nume,t1_deno,t2_nume,t2_deno;
t1_nume = deno * c.nume ;
t1_deno = c.deno * deno;
t2_nume = c.deno * nume ;
t2_deno = c.deno * deno;
if(t1_nume < t2_nume)
return true;
else
return false;
}
bool CFraction::operator==(const CFraction &c)
{
int t1_nume,t1_deno,t2_nume,t2_deno;
t1_nume = deno * c.nume ;
t1_deno = c.deno * deno;
t2_nume = c.deno * nume ;
t2_deno = c.deno * deno;
if(t1_nume == t2_nume)
return true;
else
return false;
}
bool CFraction::operator>=(const CFraction &c)
{
int t1_nume,t1_deno,t2_nume,t2_deno;
t1_nume = deno * c.nume ;
t1_deno = c.deno * deno;
t2_nume = c.deno * nume ;
t2_deno = c.deno * deno;
if(t1_nume >= t2_nume)
return true;
else
return false;
}
bool CFraction::operator<=(const CFraction &c)
{
int t1_nume,t1_deno,t2_nume,t2_deno;
t1_nume = deno * c.nume ;
t1_deno = c.deno * deno;
t2_nume = c.deno * nume ;
t2_deno = c.deno * deno;
if( t1_nume <= t2_nume)
return true;
else
return false;
}
int main()
{
CFraction c1,c2,t,t1,t2;
cout<<"请输入分数(格式 xx xx)"<<endl;
cin >> c1;
cout<<"c1 = "<<c1<<endl;
cout<<"请输入分数(格式 xx xx)"<<endl;
cin >> c2;
cout<<"c2 = "<<c2<<endl;
t = c1 + c2;
cout<<"c1 + c2 = "<<t<<endl;
t = c1 - c2;
cout<<"c1 - c2 = "<<t<<endl;
t = c1 * c2;
cout<<"c1 * c2 = "<<t<<endl;
t = c1 / c2;
cout<<"c1 / c2 = "<<t<<endl;
t = c1;
cout<<"t = "<<t<<endl;
t = -c1;
cout<<"-t = "<<t<<endl;
cout<<c1;
if(c1 > c2) cout<< " > 并且 ";
if(c1 < c2) cout<<" < 并且";
if(c1 == c2) cout<<" == ";
if(c1 >= c2) cout<<" >= ";
if(c1 <= c2) cout<<" <= ";
cout<<c2;
system("pause");
return 0;
}
上机感言:用了流运算符的重载之后不但方便了而且还纠正了我8-3中(最后一行比较大小)的一个不足之处,很好很强大~~~呵呵