第九周实验报告3

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:                             
* 作    者:         赵桐辉                    
* 完成日期:         2012年4  月16    日
* 版 本 号:         

* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述: 接第8周任务3,定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对运算结果显示方式,使程序读起来更自然。

* 程序输出:
* 程序头部的注释结束
*/

#include<iostream>   

using namespace std;  

class CFraction  
{  
private:  
	int n;  // 分子   
	int d;  // 分母   
	int gcd(int nu, int de);  
public:  
	//重载流运算符
	friend istream & operator >> (istream &, CFraction &);
	friend ostream & operator << (ostream &, CFraction &);
	//构造函数及运算符重载的函数声明   
	CFraction(int nu = 0, int de = 1):n(nu), d(de){};         //构造函数,初始化用    
	friend CFraction operator + (CFraction c1,CFraction c2);  
	friend CFraction operator - (CFraction c1,CFraction c2);  
	friend CFraction operator * (CFraction c1,CFraction c2);  
	friend CFraction operator / (CFraction c1,CFraction c2);  
	friend CFraction operator - (CFraction c);  
	friend CFraction operator + (CFraction c, int s);  
	friend CFraction operator - (CFraction c, int s);  
	friend CFraction operator * (CFraction c, int s);  
	friend CFraction operator / (CFraction c, int s);  
	friend CFraction operator + (int s, CFraction c);  
	friend CFraction operator - (int s, CFraction c);  
	friend CFraction operator * (int s, CFraction c);  
	friend CFraction operator / (int s, CFraction c);  
	bool operator > (CFraction &t);  
	bool operator < (CFraction &t);  
	bool operator >= (CFraction &t);  
	bool operator <= (CFraction &t);  
	bool operator == (CFraction &t);  
	bool operator != (CFraction &t);  
	void Simplify();                    //化简(使分子分母没有公因子)   
	void display();  
};  

//重载流插入运算符
istream & operator >> (istream & in, CFraction & c)
{
	char c1;

	cout << "请输入分数值:(形式:n / d)" << endl;

	while(1)
	{
		in >> c.n >> c1 >> c.d;

		if(c1 != '/')
		{
			cout << "格式不正确!请重新输入:" << endl;
		}
		else if(c.d == 0)
		{
			cout << "分母非法!重新输入:" << endl;
		}
		else
		{
			break;
		}
	}

	return in;
}
//重载流提取运算符
ostream & operator << (ostream & out, CFraction & c)
{
	out << c.n << "/" << c.d << endl;

	return out;
}

//重载函数的实现及用于测试的main()函数   
void CFraction::Simplify()   
{    
	int m ;  

	if(n < 0)  
	{  
		m = gcd(-n, d);  
	}  
	else  
	{  
		m = gcd(n, d);  
	}  

	n = n / m;    

	d = d / m;   

}    

int CFraction::gcd(int nu, int de)      
{      
	int t, r, a, b;     

	a = nu;    

	b = de;    

	if (a < b)         
	{      
		t = a;      
		a = b;                                       
		b = t;      
	}       

	while (b != 0)      
	{      
		r = a % b;      
		a = b;      
		b = r;      
	}      

	return a;      
}    

void CFraction::display()  
{  
	cout << n << "/" << d << endl;  
}  

CFraction operator + (CFraction c1,CFraction c2)  
{  
	CFraction c;  

	c.n = c1.n * c2.d + c2.n * c1.d;  

	c.d = c1.d * c2.d;  

	return c;  
}  

CFraction operator - (CFraction c1,CFraction c2)  
{  
	CFraction c;  

	c.n = c1.n * c2.d - c2.n * c1.d;  

	c.d = c1.d * c2.d;  

	return c;  
}  

CFraction operator * (CFraction c1,CFraction c2)  
{  
	CFraction c;  

	c.n = c1.n * c2.n;  

	c.d = c1.d * c2.d;  

	return c;  
}  

CFraction operator / (CFraction c1,CFraction c2)  
{  
	CFraction c;  

	c.n = c1.n * c2.d;  

	c.d = c1.d * c2.n;  

	return c;  
}  

CFraction operator - (CFraction c)  
{  
	c.n = 0 - c.n;  
	return c;  
}  

bool CFraction::operator > (CFraction &t)  
{  
	if(n * t.d > t.n * d)  
	{  
		return true;  
	}  
	else  
	{  
		return false;  
	}  
}  

bool CFraction::operator < (CFraction &t)  
{  
	if(n * t.d < t.n * d)  
	{  
		return true;  
	}  
	else  
	{  
		return false;  
	}  
}  
bool CFraction::operator >= (CFraction &t)  
{  
	if(n * t.d >= t.n * d)  
	{  
		return true;  
	}  
	else  
	{  
		return false;  
	}  
}  
bool CFraction::operator <= (CFraction &t)  
{  
	if(n * t.d <= t.n * d)  
	{  
		return true;  
	}  
	else  
	{  
		return false;  
	}  
}  
bool CFraction::operator == (CFraction &t)  
{  
	if(n * t.d == t.n * d)  
	{  
		return true;  
	}  
	else  
	{  
		return false;  
	}  
}  
bool CFraction::operator != (CFraction &t)  
{  
	if(n * t.d != t.n * d)  
	{  
		return true;  
	}  
	else  
	{  
		return false;  
	}  

}  

CFraction operator + (CFraction c, int s)  
{  
	CFraction c2 = c;  

	c2.n = c.n + s * c.d;  

	return c2;  
}  

CFraction operator - (CFraction c, int s)  
{  
	CFraction c2 = c;  

	c2.n = c.n - s * c.d;  

	return c2;  
}  

CFraction operator * (CFraction c, int s)  
{  
	CFraction c2 = c;  

	c2.n = c.n * s;  

	return c2;  
}  

CFraction operator / (CFraction c, int s)  
{  
	CFraction c2 = c;  

	c2.d = c.d * s;  

	return c2;  
}  

CFraction operator + (int s, CFraction c)  
{  
	CFraction c2 = c;  

	c2.n = c.n + s * c.d;  

	return c2;  
}  

CFraction operator - (int s, CFraction c)  
{  
	CFraction c2 = c;  

	c2.n = s * c.d - c.n;  

	return c2;  
}  

CFraction operator * (int s, CFraction c)  
{  

	CFraction c2 = c;  

	c2.n = s * c.n;  

	return c2;  
}  

CFraction operator / (int s, CFraction c)  
{  

	CFraction c2 = c;  

	c2.n = s * c.d;  

	c2.d = c.n;  

	return c2;  
}  

void main()  
{  
	CFraction c1(3, 5), c2(2, 3), c;   

	cin >> c1 >> c2;

	c1.Simplify(); 
	c2.Simplify(); 

	cout << "c1 =" << c1;

	cout << "c2 =" << c2;

	c = c1 + c2;  
	c.Simplify();  

	cout << "c1 + c2 =" << c;

	c = c1 - c2;  
	c.Simplify();  

	cout << "c1 - c2 =" << c;

	c = c1 * c2;  
	c.Simplify();  

	cout << "c1 * c2 =" << c;

	c = c1 / c2; 
	c.Simplify();  

	cout << "c1 / c2 =" << c;

	c = c1 + 2; 
	c.Simplify(); 

	cout << "c1 + 2 =" << c;  

	c = 2 - c2;  
	c.Simplify();  

	cout << "2 - c2 =" << c;  

	c = c1 * 2;   
	c.Simplify();  

	cout << "c1 * 2 =" << c; 

	c = 2 / c2;  
	c.Simplify();  

	cout << "2 / c2 =" << c;

	c = - c1;  

	cout << "-c1 =" << c;  

	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");  
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值