C++用运算符重载实现有理数类,进行分数的加减乘除输入输出,完整过程和代码

第一次发文章哈哈哈就是我(相)最(爱)喜(相)欢(杀)的C++。C艹虐我千百遍,我待C艹如初恋……

以防万一有比我这种小白还白的小小白,我们从新建项目开始,想要代码的话在后面可以直接跳过前面。代码是超级完整的代码嗷,可能没有大佬的优化好,但是初学者(像我)应该挺好理解的!求个赞赞嘛

 首先我们点击最下面的新建项目。并选择空项目(右边上面第一个)。给他一个你喜欢的名字,这里是有咯书我们就叫Rational好了。

然后右键头文件如图,新建两个文件:一个是Rational.h用于编写类的内容,一个是Rational.cpp用于类的成员函数的实现(还有友元函数和其他函数)。同样右键源文件添加一个test.cpp用于写main函数(也可以在这里定义其他的函数,不过我习惯放在Rational.cpp)

好,让我们上代码!

有一些变量名很奇怪(捂脸)是因为翻译过来实在有点长,然后拼音又感觉不够专业,结果最后还不如拼音……就凑合看看吧!如果要直接copy我的代码记得改一个好一点的变量名!注释都有解释说是什么的。

首先是Rational.h 

//Ratinal.h

#ifndef RATIONAL_H
#define RATIONAL_H
#include <iostream>
#include<cstring>
using namespace std;

class Rational {
public:
	Rational();
	Rational(int);
	Rational(int, int);
	~Rational();

	void simplify();										//约分
	friend void sameDenomination(Rational a, Rational b);	//通分

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

	bool operator == (const Rational&);
	bool operator <  (const Rational&);
	bool operator <= (const Rational&);
	bool operator >  (const Rational&);
	bool operator >= (const Rational&);

	friend Rational operator + (Rational&, Rational&);		//因为要通分所以不能const
	friend Rational operator - (Rational&, Rational&);		//因为要通分所以不能const
	friend Rational operator * (const Rational&, const Rational&);
	friend Rational operator / (const Rational&, const Rational&);

private:
	int numerator;		//分子
	int denominator;	//分母
};


#endif

 然后是Rational.cpp,其中最大公约数是欧几里得算法,参考连接如下,这里有证明和解释大家可以看看:C++求最大公约数(欧几里得算法) - Forceco - 博客园 (cnblogs.com)

#include <iostream>
#include"rational.h"
#include<cstring>
using namespace std;


Rational::Rational() {
	numerator = 0;
	denominator = 1;
}

Rational::Rational(int a) {
	numerator = a;
	denominator = 1;
}

Rational::Rational(int a, int b) {
	numerator = a;
	denominator = b;
}

Rational::~Rational(){}

//求最大公约数
int gcd(int a, int b) {
	return b ? gcd(b, a % b) : a;
}

//约分
void Rational::simplify() {
	int maxSim = gcd(max(denominator, numerator), min(denominator, numerator));		//最大公约数
	if (maxSim > 1) {
		numerator = numerator / maxSim;
		denominator = denominator / maxSim;
	}
}

//通分
void sameDenomination(Rational a, Rational  b) {
	if (a.denominator != b.denominator) {
		int maxSim = gcd(max(a.denominator, b.denominator), min(a.denominator, b.denominator));		//最大公约数
		int minSame = a.denominator * b.denominator / maxSim;										//最小公倍数
		a.numerator = a.numerator * (minSame / a.denominator);
		b.numerator = b.numerator * (minSame / b.denominator);
		a.denominator = minSame;
		b.denominator = minSame;
	}
}

//输出(含约分)
ostream& operator << (ostream& os, Rational& a) {
	a.simplify();
	if (a.numerator == a.denominator)
		cout << 1 << endl;
	else if (a.numerator == 0)
		cout << 0 << endl;
	else
		os << a.numerator << "/" << a.denominator << endl;
	return os;
};

//输入
istream& operator >> (istream& is, Rational& a) {
	int n, m, r = 1;
	while (r) {
		cin >> n >> m;
		if (m != 0) {
			a.numerator = n;
			a.denominator = m;
			r = 0;
		}
		else
			cout << "分母不能为0!请重新输入" << endl;
	}
	return is;
};

//需要在函数体外先约分
bool Rational::operator == (const Rational& b) {
	if (numerator == b.numerator && denominator == b.denominator)
		return 1;
	else
		return 0;
};

//需要先通分
bool Rational::operator <  (const Rational& b) {
	if (numerator < b.numerator)
		return 1;
	else
		return 0;
};
bool Rational::operator <= (const Rational& b) {
	if (numerator <= b.numerator)
		return 1;
	else
		return 0;
};
bool Rational::operator >  (const Rational& b) {
	if (numerator > b.numerator)
		return 1;
	else
		return 0;
};
bool Rational::operator >= (const Rational& b) {
	if (numerator >= b.numerator)
		return 1;
	else
		return 0;
};

Rational operator + (Rational& a, Rational& b) {
	sameDenomination(a, b);
	Rational tmp(a.numerator + b.numerator, a.denominator);//咱统一在输出的时候约分,不然总是存储最简分数没必要
	return tmp;
};
Rational operator - (Rational& a, Rational& b) {
	sameDenomination(a, b);
	Rational tmp(a.numerator - b.numerator, a.denominator);
	return tmp;
};
Rational operator * (const Rational& a, const Rational& b) {
	Rational tmp(a.numerator * b.numerator, a.denominator * b.denominator);
	return tmp;
};
Rational operator / (const Rational& a, const Rational& b) {
	Rational tmp(a.numerator * b.denominator, a.denominator * b.numerator);//并不会出现小数
	return tmp;
};

 最后是test.cpp

#include <iostream>
#include"rational.h"
#include<cstring>
using namespace std;
int main() {
	int n, m, r = 1;
	Rational A;
	cout << "请按照分子、分母的顺序输入第一个分数,分子分母必须分别为整数。示例:1/3应输入1 3" << endl;
	while (r) {
		cin >> n;
		cin >> m;
		if (m != 0) {
			A = Rational(n, m);		//使用构造函数初始化
			r = 0;
		}
		else
			cout << "分母不能为0!请重新输入" << endl;
	}
	cout << "请按照分子、分母的顺序输入第二个分数,分子分母必须分别为整数。示例:1/3应输入1 3" << endl;
	Rational B(0, 1);
	cin >> B;				//使用重载输入运算符

	//输出并判断大小
	cout << "你输入的两个分数分别是:" << endl;
	cout << "A:" << A << "B:" << B << endl;
	if (A == B)										//输出的时候已经约分过了,可以直接判断相等
		cout << "A == B" << endl;
	else {
		sameDenomination(A, B);						//判断大小前先通分
		if (A < B)
			cout << "A < B" << endl;
		if (A <= B)
			cout << "A <= B" << endl;
		else {										//小了就不用再判断大了
			if (A > B)
				cout << "A > B" << endl;
			if (A >= B)
				cout << "A >= B" << endl;
		}
	}

	Rational C(1,1);
	C = A + B;
	cout << "A + B =" << C;
	C = A - B;
	cout << "A - B =" << C;
	C = A * B;
	cout << "A * B =" << C;
	C = A / B;
	cout << "A / B =" << C;

	return 0;
}

 最后是测试

 希望有所帮助!有什么问题欢迎留言!萌新求赞嘻嘻

后续更新Complex复数类、Date日历类、Clock时钟类等,

另外可能还会更新matlab的作业代码、SQL、遥感实验报告(ENVI、Arcgis)、GNSS星历计算卫星轨道什么的,有同专业的同学可以一起学习呀!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陆地桥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值