C++面向对象之编写Rational类实现有理数的基本操作

实验要求

编写一个表示有理数的类Rational。(有理数就是分数,包含分子与分母,均为整数)。要求:

    1. 定义一个命名空间Numeric,在该空间中定义类Rational;
    2. 编写默认构造函数,构造一个有理数0;
    3. 编写带参数列表的构造函数Rational (int, int ),要求使用初始化列表
    4. 编写复制构造函数;
    5. 编写赋值操作=;
    6. 编写四个友元函数add、sub、mul、div,对两个Rational对象表示的有理数分别进行相加、相减、相乘、相除运算;(例:Rational x(1,2),y(1,3);分别表示有理数 ,则Rational z = add(x,y);之后,z表示
    7. 重载上述四个函数,实现有理数与整数的相加、相减、相乘、相除运算;(例:Rational x(1,2);表示有理数12 ,则Rational z = add(x,1),之后,z表示
    8. 编写成员函数getValue(),返回用浮点数表示的有理数,要求写成常函数。(例:Rational x(1,2);表示有理数12 ,则x.getValue()返回0.5)
    9. 编写友元函数lessThan,比较两个有理数的大小,返回bool类型。(例:Rational x(1,2),y(1,3);则bool b = lessThan(x,y);之后b为false)
    10. 编写main函数,使用using namespace Numeric;来访问Numeric::Rational类。编写代码测试Rational类。
    11. 在main函数中,随机生成10个有理数,形成一个有理数数组,并利用lessThan函数以及任意一种排序算法,对这10个有理数进行从小到大排序,输出排序结果。

注:1)上述友元函数都定义在Rational空间;

2)随机数通过std::rand()函数生成,可以调用std::srand(time(0)); 来设置随机数种子。

代码

主函数的实现

//main.cpp
#include <iostream>
#include "Rational.h"
using namespace std;
using namespace Numeric;
#include<random>
#include<stdlib.h>
#include<time.h>

int main()
{
	FourOpera FourOpera;
	lessThan lessThan;
	//调试代码
	Rational x(8, 6);
	Rational y(16, 6);
	Rational z = FourOpera.add(x, y);

	z.ReducFac();
	cout << "测试结果为:" << endl;
	cout << z.getmole() <<"      "<< z.getdenom() << "      " << z.getValue() << endl;
	
	//数组
	srand((unsigned)time(0));
	Rational ra[10];
	for (int i = 0; i < 10; i++) {
		ra[i] = Rational(rand(), rand());
		ra[i].ReducFac();
	}
	for (int i = 0; i < 10; i++) {
		for (int j = i; j < 10; j++) {
			if (lessThan.CompSize(ra[i], ra[j]) == true) {
				Rational ce;
				ce = ra[i];
				ra[i] = ra[j];
				ra[j] = ce;
			}
		}
	}
	cout << "输出结果为:" << endl;
	for (int i = 0; i < 10; i++) {
		cout << ra[i].getValue() << endl;
	}
}

Rational头文件,这里写的不规范,将函数声明和操作都放在头文件里面了,仅供参考

//Rational.h
#ifndef _RATIONAL_
#define _RATIONAL_
#include<cmath>
namespace Numeric {
	class Rational {
	private:
		int mole, denom;//分子分母
		friend class FourOpera;
		friend class lessThan;
	public:
		Rational():mole(1),denom(1){}
		Rational(int _mole, int _denom) {
			mole = _mole;
			denom = _denom;
		}
		Rational(const Rational& Ra) {
			if (this != &Ra) {
				this->mole = Ra.mole;
				this->denom = Ra.denom;
			}
		}
		Rational& operator=(const Rational& Ra) {
			if (this != &Ra) {
				this->mole = Ra.mole;
				this->denom = Ra.denom;
			}
			return *this;
		}
		int getmole() { return mole; }
		int getdenom() { return denom; }
		double getValue() { return float(this->mole) / float(this->denom); }
		//转化最简
		int min(int x, int y) {
			if (x > y)return y;
			else return x;
		}
		void ReducFac() {
			int index = min(abs(mole), abs(denom));
			for (int i = 1; i <= index; i++) {
				if (abs(mole) % i == 0 && abs(denom) % i == 0) {
					mole = mole / i;
					denom = denom / i;
				}
			}
		}
	};
	class FourOpera {
	private:
		
	public:
		Rational add(const Rational x,const Rational y) {
			Rational Aftadd;
			if (x.denom == 0 || y.denom == 0)exit(OVERFLOW);
			Aftadd.denom = x.denom * y.denom;
			Aftadd.mole = x.mole * y.denom + y.mole * x.denom;
			return Aftadd;
		}
		Rational sub(const Rational x, const Rational y) {
			Rational Aftsub;
			if (x.denom == 0 || y.denom == 0)exit(OVERFLOW);
			Aftsub.denom = x.denom * y.denom;
			Aftsub.mole = x.mole * y.denom - y.mole * x.denom;
			return Aftsub;
		}
		Rational mul(const Rational x, const Rational y) {
			Rational Aftmul;
			if (x.denom == 0 || y.denom == 0)exit(OVERFLOW);
			Aftmul.denom = x.denom * y.mole;
			Aftmul.mole = x.mole * y.denom;
			return Aftmul;
		}
		Rational div(const Rational x, const Rational y) {
			Rational Aftdiv;
			if (x.denom == 0 || y.denom == 0)exit(OVERFLOW);
			Aftdiv.denom = x.denom * y.denom;
			Aftdiv.mole = x.mole * y.mole;
			return Aftdiv;
		}
		Rational add(const Rational x, const int in) {
			Rational Aftadd;
			Rational  y;
			if (x.denom == 0)exit(OVERFLOW);
			y.denom = 1; y.mole = in;
			Aftadd.denom = x.denom * y.denom;
			Aftadd.mole = x.mole * y.denom + y.mole * x.denom;
			return Aftadd;
		}
		Rational sub(const Rational x, const int in) {
			Rational Aftsub;
			Rational  y;
			if (x.denom == 0)exit(OVERFLOW);
			y.denom = 1; y.mole = in;
			Aftsub.denom = x.denom * y.denom;
			Aftsub.mole = x.mole * y.denom - y.mole * x.denom;
			return Aftsub;
		}
		Rational mul(const Rational x, const int in) {
			Rational Aftmul;
			Rational  y;
			if (x.denom == 0)exit(OVERFLOW);
			y.denom = 1; y.mole = in;
			Aftmul.denom = x.denom * y.mole;
			Aftmul.mole = x.mole * y.denom;
			return Aftmul;
		}
		Rational div(const Rational x, const int in) {
			Rational Aftdiv;
			Rational  y;
			if (x.denom == 0)exit(OVERFLOW);
			y.denom = 1; y.mole = in;
			Aftdiv.denom = x.denom * y.denom;
			Aftdiv.mole = x.mole * y.mole;
			return Aftdiv;
		}
	};
	class lessThan {
	public:
		bool CompSize(const Rational x, const Rational y) {
			int x_mole = x.mole * y.denom;
			int y_mole = y.mole * x.denom;
			if (x_mole >= y_mole)return true;
			return false;
		}
	};
}
#endif // !_RATIONAL.H_

输出结果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Dylan、

耕码不易,白嫖可耻

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

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

打赏作者

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

抵扣说明:

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

余额充值