《C++第九周实验报告3-1》----接第8周任务3,定义分数类中<<和>>运算符重载,实现分数的输入输出...

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

* 对任务及求解方法的描述部分
* 输入描述:实现分数类中的运算符重载 
* 问题描述:在分数类中可以完成分数的加减乘除(运算后再化简)、求反、比较(6种关系)的运算
			定义分数类中<<和>>运算符重载,实现分数的输入输出,改造原程序中对
			运算结果显示方式,使程序读起来更自然。
* 程序输出:运算后的结果 
* 程序头部的注释结束
*/
#include <iostream>

using namespace std;

class CFraction
{
private:
	int nume;  // 分子
	int deno;  // 分母
	int gcd(int m, int n);
public:
	//构造函数及运算符重载的函数声明
	CFraction(int n = 1, int d = 1){if (d == 0) return;nume = n, deno = d;}
	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);
	bool operator > (CFraction &c);
	bool operator < (CFraction &c);
	bool operator >= (CFraction &c);
	bool operator <= (CFraction &c);
	bool operator == (CFraction &c);
	bool operator != (CFraction &c);
	void simplify();
	friend istream& operator >> (istream&, CFraction&);
	friend ostream& operator << (ostream&, CFraction&);
};
void CFraction::simplify()  
{  
    int n = gcd(deno, nume);  
    deno /= n;     // 化简   
    nume /= n;  
}
int CFraction::gcd(int m, int n)
{
    int r;  
    if (m < n){r = m; m = n; n = r;}  
    while(r = m % n)  // 求m,n的最大公约数   
    {  
        m = n;  
        n = r;  
    }  
    return n; 
}
CFraction operator + (CFraction &c1, CFraction &c2)
{
	CFraction c;

	c.deno = c1.deno * c2.deno;
	c.nume = c1.nume*c2.deno + c2.nume*c1.deno;

	c.simplify();

	return c;
}
CFraction operator - (CFraction &c1, CFraction &c2)
{
	CFraction c;

	c.deno = c1.deno * c2.deno;
	c.nume = c1.nume*c2.deno - c2.nume*c1.deno;

	c.simplify();

	return c;
}
CFraction operator * (CFraction &c1, CFraction &c2)
{
	CFraction c;

	c.deno = c1.deno * c2.deno;
	c.nume = c1.nume * c2.nume;

	c.simplify();

	return c;
}
CFraction operator / (CFraction &c1, CFraction &c2)
{
	CFraction c;

	c.deno = c1.deno * c2.nume;
	c.nume = c1.nume * c2.deno;

	c.simplify();

	return c;
}
CFraction operator - (CFraction &c)
{
	return CFraction(-c.nume, c.deno);
}
bool CFraction::operator > (CFraction &c)
{
	CFraction cf(*this / c);

	if (cf.nume/cf.deno > 1)
		return true;
	else
		return false;
}
bool CFraction::operator < (CFraction &c)
{
	CFraction cf(*this / c);

	if (cf.nume/cf.deno < 1)
		return true;
	else
		return false;
}
bool CFraction::operator >= (CFraction &c)
{
	CFraction cf(*this / c);

	if (cf.nume/cf.deno >= 1)
		return true;
	else
		return false;
}
bool CFraction::operator <= (CFraction &c)
{
	CFraction cf(*this / c);

	if (cf.nume/cf.deno <= 1)
		return true;
	else
		return false;
}
bool CFraction::operator == (CFraction &c)
{
	CFraction cf(*this / c);

	if (cf.nume/cf.deno == 1)
		return true;
	else
		return false;
}
bool CFraction::operator != (CFraction &c)
{
	CFraction cf(*this / c);

	if (cf.nume/cf.deno != 1)
		return true;
	else
		return false;
}
istream& operator >> (istream& input, CFraction& cf)
{
	cout << "请输入分子分母(nume deno):" << endl;
	input >> cf.nume >> cf.deno;

	return input;
}
ostream& operator << (ostream& output, CFraction& cf)
{
	output << cf.nume << "/" << cf.deno;

	return output;
}
//重载函数的实现及用于测试的main()函数
void main()
{
	//CFraction c1(3, 8), c2(1, 8), c3(1, 8);
	CFraction c1, c2, c3;

	cin >> c1 >> c2 >> c3;

	cout << "c1=" << c1 << endl;
	cout << "c2=" << c2 << endl;
	cout << "c3=" << c3 << endl;

	if (c1 > c2) cout << "c1 > c2" << endl;
	if (c2 < c1) cout << "c2 < c1" << endl;
	if (c2 >= c3) cout << "c2 >= c3" << endl;
	if (c2 <= c3) cout << "c2 <= c3" << endl;
	if (c2 == c3) cout << "c2 == c3" << endl;
	if (c1 != c2) cout << "c1 != c2" << endl;

	cout << "c1+c2=" << (c1 + c2) << endl;
	cout << "c1-c2=" << (c1 - c2) << endl;
	cout << "c1*c2=" << (c1 * c2) << endl;
	cout << "c1/c2=" << (c1 / c2) << endl;


	system("PAUSE");
}
/*
程序就是一点一点写出来的,你写的越多你就会越熟练。
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值