问题 K: oop实习-11.运算符重载

该代码定义了一个名为R的类,用于表示带分数。类中包含了私有成员变量iUp(分子)和iDown(分母),并重载了各种运算符,如+,-,*,/,++,--以及比较操作符。此外,还包括了友元函数以支持输入输出流操作。在main函数中,展示了类的使用,包括加减乘除、自增自减和比较操作。
摘要由CSDN通过智能技术生成

在这里插入图片描述

#include <iostream>

using namespace std;

class R{
	private:
		int iUp;
		int iDown;
		void Reduce();
		int Gcd(int l,int r);
	public:
		R();
		R(int i,int j=1);
		R& operator-();
		R& operator=(const R&);
		R& operator++();
		R operator++(int);
		R& operator--();
		R operator--(int);
		
	friend 	R operator+(const R&a,const R &b);
	friend 	R operator-(const R&a,const R &b);
	friend 	R operator*(const R&a,const R &b);
	friend 	R operator/(const R&a,const R &b);
	friend 	int operator<(const R&a,const R &b);
	friend 	int operator<=(const R&a,const R &b);
	friend 	int operator>(const R&a,const R &b);
	friend 	int operator>=(const R&a,const R &b);
	friend ostream& operator<<(ostream& os,const R& a);
	friend istream& operator>>(istream& is,R &a);
};

istream& operator>>(istream& is,R &a)
{
	is>>a.iUp>>a.iDown;
}

ostream& operator<<(ostream& os,const R& a)
{
	if(a.iDown==1)
	os<<a.iUp;
	else if((a.iUp>0&&a.iDown>0)||(a.iUp<0&&a.iDown>0))
	os<<a.iUp<<"/"<<a.iDown;
	else
	os<<-a.iUp<<"/"<<-a.iDown; 	   
}

int operator<(const R&a,const R &b)
{
	R c=a-b;
	if(c.iUp<0)
	 return 1;
	 else
	 return 0;
}

int operator<=(const R&a,const R &b)
{
	R c=a-b;
	if(c.iUp<=0)
	 return 1;
	 else
	 return 0;
}

int operator>(const R&a,const R &b)
{
	R c=a-b;
	if(c.iUp>0)
	 return 1;
	 else
	 return 0;
}

int operator>=(const R&a,const R &b)
{
	R c=a-b;
	if(c.iUp>=0)
	 return 1;
	 else
	 return 0;
}

R operator+(const R&a,const R &b)
{
	int x = a.iUp * b.iDown + a.iDown * b.iUp;
	int y = a.iDown * b.iDown;
	return R(x,y);
}

R operator-(const R&a,const R &b)
{
	int x = a.iUp * b.iDown - a.iDown * b.iUp;	
	int y = a.iDown * b.iDown;
	return R(x,y);
}
R operator*(const R&a,const R &b)
{
	int x = a.iUp * b.iUp;
	int y = a.iDown * b.iDown;
	return R(x,y);
}

R operator/(const R&a,const R &b)
{
	int x = a.iUp * b.iDown;
	int y = a.iDown * b.iUp;
	return R(x,y);
}

R& R::operator++()
{
	R b(1,1);
   	 *this = *this + b;
   	return *this;
} 

R R::operator++(int i)
{
	R before(iUp,iDown);
	R b(1,1);
   	*this = *this + b;
	return before;
}

R& R::operator--()
{
	R b(1,1);
   	 *this = *this - b;
   	return *this;
} 

R R::operator--(int i)
{
	R before(iUp,iDown);
	R b(1,1);
   	*this = *this - b;
	return before;
}

R& R::operator-()
{
	iUp=-iUp;
	return *this;
}

R& R::operator=(const R &a)
{
	if(this == &a)
	return *this;
	iUp = a.iUp;
	iDown = a.iDown;
	return *this;
}

R::R() 
{
	iUp=1;
	iDown=1;
} 

R::R(int l,int r) :iUp(l),iDown(r) 
{
    Reduce();
}
void R::Reduce() 
{
	int m;
	if(iUp<0)
	 m=Gcd(-iUp,iDown);
	else
	 m=Gcd(iUp,iDown);
	iUp/=m;
	iDown/=m;
}

int R::Gcd(int l,int r)
{
	int n=1;
	while(r!=0)
	{
		n=l%r;
		l=r;
		r=n;
	}
	return l;
}

int main()
{
	R a,b;
	cin>>a;
	cin>>b;
	cout<<"a+b: "<<a+b<<endl;
	cout<<"a-b: "<<a-b<<endl;
	cout<<"a*b: "<<a*b<<endl;
	cout<<"a/b: "<<a/b<<endl;
	cout<<"-a: "<<-a<<endl;
	-a;
	cout<<"++a: "<<++a<<endl;
	cout<<"--a: "<<--a<<endl;
	cout<<"a++: "<<a++<<endl;
	cout<<"a--: "<<a--<<endl;
	cout<<"a<b: "<<(a<b?"true":"false")<<endl;
    cout<<"a<=b: "<<(a<=b?"true":"false")<<endl;
	cout<<"a>b: "<<(a>b?"true":"false")<<endl;
	cout<<"a>=b: "<<(a>=b?"true":"false")<<endl;
	return 0;
}

代码编写不易,如果对您有帮助,谢谢您的点赞支持~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值