源程序:
/*(文件注释头部开始)
*程序的版权和版本声明部分
*Copyright (c) 2011,烟台大学计算机学院学生
*All rights reserved.
*文件名称:分数类流运算符的重载
*作 者:2011级计114-3张宗佳
*完成日期:2011年4月17号
*版本号:vc
* 对任务及求解方法的描述部分
* 输入描术:输入两个分数
* 问题描述:定义一个分数类重载运算符+、-、*、/ << >>,使之能用于分数的加减乘除与输入输出。
* 程序输出:输出分数的加减乘除
* 程序头部的注释结束
*/
#include <iostream>
using namespace std;
class CFraction
{private:
int nume; // 分子
int deno; // 分母
public:
CFraction(int nu=0,int de=1); //构造函数,初始化用
void Set(int nu=0,int de=1); //置值,改变值时用
void Simplify(); //化简(使分子分母没有公因子)
CFraction operator+(const CFraction &c);//分数相加
CFraction operator-(const CFraction &c);//分数相减
CFraction operator*(const CFraction &c);//分数相乘
CFraction operator/(const CFraction &c);//分数相除
CFraction operator+();//取正运算
CFraction operator-();//取反运算
friend istream & operator >> (istream &input,CFraction &c);
friend ostream & operator << (ostream &output,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);
};
CFraction::CFraction(int nu,int de)
{
nume=nu;
deno=de;
}
void CFraction::Set(int nu,int de)
{
nume=nu;
deno=de;
}
istream & operator >> (istream &input,CFraction &c)
{
cout << "输入分子和分母:";
input >> c.nume >> c.deno;
return input;
}
ostream & operator << (ostream &output,CFraction &c)
{
output << c.nume << "/" << c.deno << endl;
return output;
}
void CFraction::Simplify()
{
int a,b,c,p;
if(nume<=deno)
{
p=nume;
}
else
p=deno;
for(a=2;a<=p;a++)
{
b=nume%a;
c=deno%a;
if(b==0&&c==0)
{
do
{
nume=nume/a;
deno=deno/a;
b=nume%a;
c=deno%a;
}while(b==0&&c==0);
}
}
}
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 t;
t.nume=-nume;
t.deno=deno;
return t;
}
//比较运算
bool CFraction::operator>(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume >t2.nume )
return true;
else
return false;
}
bool CFraction::operator<(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume <t2.nume )
return true;
else
return false;
}
bool CFraction::operator==(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume ==t2.nume )
return true;
else
return false;
}
bool CFraction::operator!=(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume ==t2.nume )
return false;
else
return true;
}
bool CFraction::operator>=(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume <t2.nume )
return false;
else
return true;
}
bool CFraction::operator<=(const CFraction &c)
{
CFraction t1,t2;
t1.nume=nume*c.deno;
t2.nume=deno*c.nume;
if(t1.nume >t2.nume )
return false;
else
return true;
}
void main()
{
CFraction c1,c2,c3;
cin >> c1 >> c2;
cout << "c1=" << c1;
cout << "c2=" << c2;
c3 = c1 + c2;
cout << "c1+c2=" << c3;
c3 = c1 - c2;
cout << "c1-c2=" << c3;
c3 = c1 * c2;
cout << "c1*c2=" << c3;
c3 = c1 / c2;
cout<<"c1/c2=" << c3;
c3 = -c1;
cout << "-c1=" << c3;
cout << "两分数比较得:" << endl;
if (c1 > c2) cout << "c1 > c2" << endl;
if (c1 < c2) cout << "c1 < c2" << endl;
if (c1 == c2) cout << "c1 = c2" << endl;
if (c1 != c2) cout << "c1 ≠ c2" << endl;
if (c1 >= c2) cout << "c1 ≥ c2" << endl;
if (c1 <= c2) cout << "c1 ≤ c2" << endl;
system("pause");
}
实验结果:
上机感言:
终于传上了,快无语了,改了n便,没办法...关于流运算符的重载问题,还是很好用的,只是其他运算符的输出问题嘛...还是有点郁闷(仅对于这个程序来说...)