第九周任务三之流运算符的重载

源程序:

/*(文件注释头部开始) 
*程序的版权和版本声明部分 
*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便,没办法...关于流运算符的重载问题,还是很好用的,只是其他运算符的输出问题嘛...还是有点郁闷(仅对于这个程序来说...)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值