《第九周任务三》定义分数类中《和》运算符重载,实现分数的输入输出,改造源程序中对运算结果显示方式,使程序读起来更自然

/*
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生 
* All rights reserved.
* 文件名称:定义分数类中《和》运算符重载,实现分数的输入输出,改造源程序中对运算结果显示方式,使程序读起来更自然
* 作    者:  张启立                           
* 完成日期:  2012年 4 月 16 日
* 版 本 号: 9-3-1        
 
* 对任务及求解方法的描述部分
* 代码实现
#include<iostream>

using namespace std;

class CFraction
{
private:
	int nume;  // 分子
	int deno;  // 分母
	int gcd(int nu, int de);
public:
	//构造函数及运算符重载的函数声明
	CFraction(int nu = 0, int de = 1):nume(nu), deno(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);

	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();   //化简函数(使分子分母没有公因子)
	friend ostream& operator << (ostream&,CFraction&);
	friend istream& operator >> (istream&,CFraction&);
	
};
//重载函数的实现及用于测试的main()函数
ostream& operator << (ostream&output,CFraction&f)
{
    output << f.nume << "/" << f.deno;

	return output;
}

istream& operator >> (istream&input,CFraction&f)
{
	char c;
	do
	{
	input >> f.nume >> c >> f.deno;
	}while(c != '/');

	return input;
}
void CFraction::Simplify() //化简函数(使分子分母没有公因子)
{  
	int n ;

	if(nume < 0  && deno > 0)
	{
		n = gcd(-nume, deno);
	}
	else if(nume > 0  && deno > 0)
	{
		n = gcd(nume, deno);
	}
	else if(nume > 0  && deno < 0)
	{
		n = gcd(nume, -deno);
	}
	else if(nume < 0  && deno < 0)
	{
		n = -gcd(-nume, -deno);
	}
	nume = nume / n;  

	deno = deno / n; 

}  


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


CFraction operator + (CFraction &c1,CFraction &c2)
{
	CFraction c;//先通分,再相加

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

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

	return c;
}

CFraction operator - (CFraction &c1,CFraction &c2)
{
	CFraction c;//先通分,再相减

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

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

	return c;
}

CFraction operator * (CFraction &c1,CFraction &c2)
{
	CFraction c;//分子分母分别相乘

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

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

	return c;
}

CFraction operator / (CFraction &c1,CFraction &c2)
{
	CFraction c;//相除即乘以倒数

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

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

	return c;
}

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

bool CFraction::operator > (CFraction &t)
{
	if(nume * t.deno > t.nume * deno)//先通分,在比较
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool CFraction::operator < (CFraction &t)
{
	if(nume * t.deno < t.nume * deno)//先通分,在比较
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool CFraction::operator >= (CFraction &t)
{
	if(nume * t.deno >= t.nume * deno)//先通分,在比较
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool CFraction::operator <= (CFraction &t)
{
	if(nume * t.deno <= t.nume * deno)//先通分,在比较
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool CFraction::operator == (CFraction &t)
{
	if(nume * t.deno == t.nume * deno)//先通分,在比较
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool CFraction::operator != (CFraction &t)
{
	if(nume * t.deno != t.nume * deno)//先通分,在比较
	{
		return true;
	}
	else
	{
		return false;
	}
}

void main()
{
	CFraction c1, c2, c;

	cout << "请输入你要输入的分数:(例:3/7)" <<endl;

	cin >> c1 >>c2 ;

	c1.Simplify();

	cout << "c1 =" << c1 << endl;

	c2.Simplify();

	cout << "c2 =" << c2 << endl;

	c = c1 + c2;

	c.Simplify();

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

	c = c1 - c2;

	c.Simplify();

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

	c = c1 * c2;

	c.Simplify();

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

	c = c1 / c2;

	c.Simplify();

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

	c = - c1;

	cout << "-c1 =" << c << 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");
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值