第九周任务三

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

#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();  

	friend ostream& operator<<(ostream &, CFraction &);
	friend istream& operator>>(istream &, CFraction &);

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

	}

}


ostream& operator<<(ostream &output, CFraction &t)
{
	output << t.nume << "/" << t.deno << endl;
	return output;
}
istream& operator>>(istream &input, CFraction &t)
{
	char a,b,c;
	input >> a >> t.nume >> b >> t.deno >> c;
	while(1)
	{
		if ( a == '(' && b== ',' && c == ')' )
			 break;
		else
			cout << " 输入错误,请重新输入" << endl;
	}
	return input;
}

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,t;

	cout <<"please input c2 (X,X)"<< endl;
	cin >> c2;

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

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

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

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

	cout << "取反" << endl;

	t = c1;
	cout<<"t = "<<t;

	t = -c1;
	cout<<"-t = "<<t;

	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、付费专栏及课程。

余额充值