定义分数的一目运算+和-,分别代表分数取正和求反,将“按位取反运算符”~重载为分数的求倒数运算。

问题及代码:

/*
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:main.cpp
*作    者:李磊涛
*完成时间:2016年5月28日
*版 本 号:v1.0
*
*问题描述:定义分数的一目运算+和-,分别代表分数取正和求反,将“按位取反运算符”~重载为分数的求倒数运算。 
*输入描述:无。
*程序输出:分数。
*/
#include<iostream>
using namespace std;
class CFraction
{
private:
	int nume;//fenzi
	int deno;//fenmu
public:
	CFraction(int a=0,int b=0);
	
	void show();
	CFraction operator+(double c);
	CFraction operator-(double c);
	CFraction operator*(double c);
	CFraction operator/(double 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);
	CFraction operator+();
	CFraction operator-();
	CFraction operator~();
};
CFraction CFraction::operator~()
{
	int a=nume,b=deno,t;
	t=a;
	a=b;
	b=t;
	CFraction c(a,b);
	return c;
}
CFraction CFraction::operator+()
{
	return *this;
}
CFraction CFraction::operator-()
{
	int mu,zi;
	mu=deno;
	zi=-nume;
	CFraction t(zi,mu);
	return t;
}
bool  CFraction::operator>(CFraction &c)
{
	int mu,zi1,zi2;
	mu=deno*c.deno;
	zi1=nume*c.deno;
	zi2=c.nume*deno;
	if(zi1>zi2)
		return true;
	else 
		return false;

		
}
bool CFraction::operator<(CFraction &c)
{
	int mu,zi1,zi2;
	mu=deno*c.deno;
	zi1=nume*c.deno;
	zi2=c.nume*deno;
	if(zi1< zi2)
		return true;
	else 
		return false;

		
}
bool CFraction::operator==(CFraction &c)
{
	int mu,zi1,zi2;
	mu=deno*c.deno;
	zi1=nume*c.deno;
	zi2=c.nume*deno;
	if(zi1==zi2)
		return true;
	else 
		return false;

		
}
bool  CFraction::operator<=(CFraction &c)
{
	int mu,zi1,zi2;
	mu=deno*c.deno;
	zi1=nume*c.deno;
	zi2=c.nume*deno;
	if(zi1<=zi2)
		return true;
	else 
		return false;

		
}
bool  CFraction::operator>=(CFraction &c)
{
	int mu,zi1,zi2;
	mu=deno*c.deno;
	zi1=nume*c.deno;
	zi2=c.nume*deno;
	if(zi1>=zi2)
		return true;
	else 
		return false;		
}
bool  CFraction::operator!=(CFraction &c)
{
	int mu,zi1,zi2;
	mu=deno*c.deno;
	zi1=nume*c.deno;
	zi2=c.nume*deno;
	if(zi1!=zi2)
		return true;
	else 
		return false;		
}
CFraction::CFraction(int a,int b)
{
	nume=a;
	deno=b;
}
CFraction CFraction::operator+(double c)
{
	int zi;
	
	zi=nume+c*deno;
	CFraction t(zi,deno);
	return t;
}
CFraction CFraction::operator-(double c)
{
		int zi;
	
	zi=nume-c*deno;
	CFraction t(zi,deno);
	return t;
}
CFraction CFraction::operator*(double c)
{
		int zi;
	
	zi=nume*c;
	CFraction t(zi,deno);
	return t;
}
CFraction CFraction::operator/(double c)
{
		int mu;
	
	mu=deno*c;
	CFraction t(nume,mu);
	return t;
}
void CFraction::show()
{int t,m,r,n;
	m=deno;
	n=nume;
	if(deno<nume)
	{
		t=m;
		m=n;
		n=t;
	}
	
	while(r=m%n)  
    {
        m=n;
        n=r;
    }
	deno=deno/n;
	nume=nume/n;
	if(deno==1)
		cout<<nume<<endl;
	else
	cout<<nume<<"/"<<deno<<endl;

}
int main()
{
	CFraction  c1(1,2),c2(2,3),c3;
	c3=c1+2;
	c3.show();
	c3=c1-2;
	c3.show();
	c3=c1*2;
	c3.show();
	c3=c1/2;
	c3.show();
	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;
	
	c3=c1.operator -();
	cout<<"-c1=";
	c3.show();
	cout<<endl;
	c3=c1.operator +();
   	cout<<"+c1=";
	c3.show();
	cout<<endl;
	cout<<"x的倒数: ";
	c3=c1.operator ~();
	c3.show();
return 0;
}


运行结果:


知识点总结:
通过该程序,强化了我对的运算符重载的认识。
学习心得:
期间有很多小错误,要继续写程序争取早日掌握的运算符重载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值