编写一个表示有理数的类Rational。(有理数就是分数,包含分子与分母,均为整数)。要求:
注:1)上述友元函数都定义在Rational空间;
2)随机数通过std::rand()函数生成,可以调用std::srand(time(0)); 来设置随机数种子。
主函数的实现
//main.cpp
#include
#include "Rational.h"
using namespace std;
using namespace Numeric;
#include
#include
#include
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
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_