C++:Fraction类实现对分数的+ - * / << >>重载运算

本文详细介绍了如何创建Fraction类,通过重载运算符实现分数的加减乘除比较,并展示了使用样例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Fraction类实现对分数的+ - * / << >>重载运算

Problem description:

create a class called Fraction with 2 private fields Numerator, Denominator. And a public constructor that sets Numerator and Denominator to 1 by default.

class Fraction {
int numerator, denominator;

public:

....

};

1、overload  +  -  *  /

2、overload ==!=<<=>>= 

3、overload  output/input operator  <<  and  >>

后缀代码:	
	
//StudybarCommentBegin
int main(int argc, char *argv[]) {
	
	Fraction a(1),b(1,3);
	
	cin>>a>>b;
	
	cout << "a= " << a << ", b = " << b << endl;
	
	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 == b is " << (a == b) << endl;
	cout << "a != b is " << (a != b) << endl;
	cout << "a <= b is " << (a <= b) << endl;
	cout << "a >= b is " << (a >= b) << endl;
	cout << "a < b is " << (a < b) << endl;
	cout << "a > b is " << (a > b) << endl;
	
    return 1;
}
//StudybarCommentEnd
---------------------------------------------------------------------
Sample input:
1 2
3 4
Sample output
a= 1/2, b = 3/4
1/2 + 3/4 = 5/4
1/2 - 3/4 = -1/4
1/2 * 3/4 = 3/8
1/2 / 3/4 = 2/3
a == b is 0
a != b is 1
a <= b is 1
a >= b is 0
a < b is 1
a > b is 0
Sample input:
2 4
3 4
Sample output:
a= 1/2, b = 3/4
1/2 + 3/4 = 5/4
1/2 - 3/4 = -1/4
1/2 * 3/4 = 3/8
1/2 / 3/4 = 2/3
a == b is 0
a != b is 1
a <= b is 1
a >= b is 0
a < b is 1
a > b is 0

Answer:

/*
----------------------------------------------------
代码思想:
1.将两个分数彻底通分,即两个分母相同(如:1/2与3/4==>4/8与6/8)
2.对分子进行加减乘除
3.最后的结果传入<<重载,通过最大公约数进行化简
*/
#include<iostream>
using namespace std;
class Fraction
{
public:
	Fraction(int aa = 1, int bb = 1) :a(aa), b(bb) {};
	~Fraction();
	Fraction operator+(Fraction x);
	Fraction operator-(Fraction x);
	Fraction operator*(Fraction x);
	Fraction operator/(Fraction x);
	int operator>(Fraction x);
	int operator<(Fraction x);
	int operator==(Fraction x);
	int operator!=(Fraction x);
	int operator>=(Fraction x);
	int operator<=(Fraction x);
	friend istream& operator >>(istream& mycin, Fraction& z);
	friend ostream& operator <<(ostream& mycout, const Fraction& z);
private:
	int a, b;
};

Fraction::~Fraction()
{
}

Fraction Fraction::operator+(Fraction x)
{
	Fraction tmp;
	tmp.a = this->a * x.b + x.a * this->b;
	tmp.b = this->b * x.b;
	return tmp;
}

Fraction Fraction::operator-(Fraction x)
{
	Fraction tmp;
	tmp.a = this->a * x.b - x.a * this->b;
	tmp.b = this->b * x.b;
	return tmp;
}


Fraction Fraction::operator*(Fraction x)
{
	Fraction tmp;
	tmp.a = this->a * x.a;
	tmp.b = this->b * x.b;
	return tmp;
}


Fraction Fraction::operator/(Fraction x)
{
	Fraction tmp;
	tmp.a = this->a * x.b;
	tmp.b = this->b * x.a;
	return tmp;
}

int Fraction::operator>(Fraction x)
{
	if (this->a * x.b > x.a * this->b) return 1;
	else return 0;
}
int Fraction::operator<(Fraction x)
{
	if (this->a * x.b < x.a * this->b) return 1;
	else return 0;
}
int Fraction::operator==(Fraction x)
{
	if (this->a * x.b == x.a * this->b) return 1;
	else return 0;
}
int Fraction::operator!=(Fraction x)
{
	if (this->a * x.b != x.a * this->b) return 1;
	else return 0;
}
int Fraction::operator>=(Fraction x)
{
	if (this->a * x.b >= x.a * this->b) return 1;
	else return 0;
}
int Fraction::operator<=(Fraction x)
{
	if (this->a * x.b <= x.a * this->b) return 1;
	else return 0;
}

istream& operator >>(istream& mycin, Fraction& z)
{
	cin >> z.a >> z.b;
	return mycin;
}

ostream& operator <<(ostream& mycout, const Fraction& z)
{
	int x(z.a), y(z.b);
	int flag(0);
	if (x < 0)
	{
		x = -x; flag = 1;
	}
	for (int i = x; i >= 1; i--)
	{//找最大公约数
		if ((x % i == 0) && (y % i == 0))
		{
			x = x / i;
			y = y / i;
		}
	}
	if (flag == 1) x = -x;
	cout << x << '/' << y;
	return mycout;
}

//StudybarCommentBegin
int main(int argc, char* argv[]) {

	Fraction a(1), b(1, 3);

	cin >> a >> b;

	cout << "a= " << a << ", b = " << b << endl;

	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 == b is " << (a == b) << endl;
	cout << "a != b is " << (a != b) << endl;
	cout << "a <= b is " << (a <= b) << endl;
	cout << "a >= b is " << (a >= b) << endl;
	cout << "a < b is " << (a < b) << endl;
	cout << "a > b is " << (a > b) << endl;

	return 1;
}
//StudybarCommentEnd
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值