第八周任务三

实现分数类中的运算符重载,在分数类中可以完成分数的加减乘除(运算后再化简)、求反、比较(6种关系)的运算。

class CFraction
{
private:
	int nume;  // 分子
	   int deno;  // 分母
public: 
	//构造函数及运算符重载的函数声明

};
//重载函数的实现及用于测试的main()函数


解答:

#include <iostream>  
using namespace std;  
class CFraction  
{  
private:  
	int nume;  // 分子  
	int deno;  // 分母  
public:  
	CFraction(int nu=0,int de=1):nume(nu),deno(de){}  
	void simplify();  
	void display();  
	CFraction operator+(const CFraction &c);  
	CFraction operator-(const CFraction &c);  
	CFraction operator*(const CFraction &c);  
	CFraction operator/(const CFraction &c);  
	CFraction operator+();  
	CFraction operator-(); 
	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);  
};  
// 分数化简  
void CFraction::simplify()
{


	for ( int i = nume; i > 0; i-- )
	{
		if ( nume % i == 0)

			if ( deno % i == 0 )
			{
				nume /=  i;
				deno /=  i;
				break;
			}

	}

}


void CFraction::display()
{
	cout<<nume<<"/"<<deno<<endl;
}
CFraction CFraction::operator+(const CFraction &c)
{
	CFraction  t;
	t.deno = c.deno * deno;
	t.nume = deno * c.nume + c.deno * nume;
	t.simplify();
	return t;
}

CFraction CFraction::operator-(const CFraction &c)
{
	CFraction  t;
	t.deno = c.deno * deno;
	t.nume = c.deno * nume - deno * c.nume;
	t.simplify();
	return t;
}

CFraction CFraction::operator*(const CFraction &c)
{
	CFraction  t;
	t.deno = c.deno * deno;
	t.nume = nume * c.nume;
	t.simplify();
	return t;
}

CFraction CFraction::operator/(const CFraction &c)
{
	CFraction  t;
	t.deno = c.nume * deno;
	t.nume = nume * c.deno;
	t.simplify();
	return t;
}

CFraction CFraction::operator+()
{
	CFraction t;
	t.deno = deno;
	t.nume = nume;
	t.simplify();
	return t;
}
CFraction CFraction::operator-() 
{
	CFraction t;
	t.deno = deno;
	t.nume = -nume;
	t.simplify();
	return t;
}

bool CFraction::operator>(const CFraction &c) 
{
	int t1_nume,t2_nume;
	t1_nume = deno * c.nume ;
	t2_nume = c.deno * nume ;
	if(t1_nume  < t2_nume)
		return true;
	else 
		return false;
}
bool CFraction::operator<(const CFraction &c)  
{
	int t1_nume ,t2_nume ;
	t1_nume = deno * c.nume ;
	t2_nume = c.deno * nume ;
	if(t1_nume > t2_nume)
		return true;
	else 
		return false;
}

bool CFraction::operator==(const CFraction &c)
{
	int t1_nume ,t2_nume ;
	t1_nume = deno * c.nume ;
	t2_nume = c.deno * nume ;
	if(t1_nume == t2_nume)
		return true;
	else 
		return false;
}

bool CFraction::operator>=(const CFraction &c)
{
	int t1_nume ,t2_nume ;
	t1_nume = deno * c.nume ;
	t2_nume = c.deno * nume ;
	if(t1_nume < t2_nume )
		return true;
	else 
		return false;
}
bool CFraction::operator<=(const CFraction &c)  
{
	int t1_nume ,t2_nume ;
	t1_nume = deno * c.nume ;
	t2_nume = c.deno * nume ;
	if( t1_nume >= t2_nume)
		return true;
	else 
		return false;
}
int main()
{
	CFraction c1(1,2),c2(1,3),t;
	t = c1 + c2;
	cout<<"c1 + c2 = ";
	t.display();

	t = c1 - c2;
	cout<<"c1 - c2 = ";
	t.display();

	t = c1 * c2;
	cout<<"c1 * c2 = ";
	t.display();

	t = c1 / c2;
	cout<<"c1 / c2 = ";
	t.display();

	cout << "取反" << endl;
	t = c1;
	cout<<"t = ";
	t.display();

	t = -c1;
	cout<<"-t = ";
	t.display();

	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;

	system("pause");

	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值