C++ 有理数类

C++只提供了整数类和浮点数类,但是没有有理数类,所以需要自己写一个有理数类。

我们将使用分数来表示一个有理数。即Rational类有两个数据域,分子叫做 numerator,分母叫做denominator,且分母不能为0。

同时,一个有理数可能又很多表现形式,比如1/3可以表示为2/6,3/9等,我们统一用化简后的形式表示这个数,比如1/3。

Rational.h

#ifndef RATIONAL_H
#define RATIONAL_H
#include <string>
using namespace std;

class Rational{
public:
    //无参构造函数
    Rational();
    //有参构造函数,两个参数分别是分子和分母
    Rational(int numerator, int denominator);
    //获取分子
    int getNumerator() const;
    //获取分母
    int getDenominator() const;
    //两数求和,返回Rational类对象
    Rational add(const Rational& secondRational) const;
    //两数求差,返回Rational类对象
    Rational subtract(const Rational& secondRational) const;
    //两数相乘,返回Rational类对象
    Rational multiply(const Rational& secondRational) const;
    //两数相除,返回Rational类对象
    Rational divide(const Rational& secondRational) const;
    //比较两个有理数
    int compareTo(const Rational& secondRational) const;
    //若两个有理数相等,则返回true;否则,返回false
    bool equals(const Rational& secondRational) const;
    //返回一个整型数
    int intValue() const;
    //返回一个浮点数
    double doubleValue() const;
    //返回一个字符串
    string toString() const;

private:
    //分子
    int numerator;
    //分母
    int denominator;
    //求公因数,用于约分
    static int gcd(int n, int d);
};

#endif // RATIONAL_H

Rational.cpp

#include "Rational.h"
#include <sstream>
#include <cstdlib>

Rational::Rational() {
    //构造一个值为0的对象
    this->numerator = 0;
    this->denominator = 1;
}

Rational::Rational(int numerator, int denominator) {
    int temp = gcd(numerator, denominator);
    this->numerator = ((denominator > 0) ? 1 : -1) * numerator / temp;
    this->denominator = abs(denominator) / temp;
}

int Rational::getNumerator() const {
    return this->numerator;
}

int Rational::getDenominator() const{
    return this->denominator;
}

int Rational::gcd(int n, int d) {
    int n1 = abs(n);
    int n2 = abs(d);
    int gcd = 1;

    for (int i = 1; i <= n1 && i <= n2; i ++) {
        if (n1 % i == 0 && n2 % i == 0) {
            gcd = i;
        }
    }

    return gcd;
}

Rational Rational::add(const Rational& secondRational) const {
    int n = numerator * secondRational.getDenominator() + denominator * secondRational.getNumerator();
    int d = denominator * secondRational.getDenominator();

    return Rational(n, d);
}

Rational Rational::subtract(const Rational& secondRational) const {
    int n = numerator * secondRational.getDenominator() - denominator * secondRational.getNumerator();
    int d = denominator * secondRational.getDenominator();

    return Rational(n, d);
}

Rational Rational::multiply(const Rational& secondRational) const {
    int n = numerator * secondRational.getNumerator();
    int d = denominator * secondRational.getDenominator();

    return Rational(n, d);
}

Rational Rational::divide(const Rational& secondRational) const {
    int n = numerator * secondRational.getDenominator();
    int d = denominator * secondRational.getNumerator();

    return Rational(n, d);
}

int Rational::compareTo(const Rational& secondRational) const {
    //作差法比较大小
    Rational temp = subtract(secondRational);

    if (temp.getNumerator() < 0) {
        //小于
        return -1;
    } else if (temp.getNumerator() == 0) {
        //等于
        return 0;
    } else {
        //大于
        return 1;
    }
}

bool Rational::equals(const Rational& secondRational) const {
    if(compareTo(secondRational) == 0) {
        return true;
    } else {
        return false;
    }
}

int Rational::intValue() const {
    return getNumerator()/getDenominator();
}

double Rational::doubleValue() const {
    return 1.0 * getNumerator()/getDenominator();
}

string Rational::toString() const {
    stringstream ss;
    ss << numerator;

    if(denominator > 1) {
        ss << "/" <<denominator;
    }

    return ss.str();
}

main.cpp

#include <iostream>
#include "Rational.h"

using namespace std;

int main()
{
    Rational r1(4, 2);
    Rational r2(2, 3);

    cout << "r1的值为" << r1.toString() << endl << "r2的值为" << r2.toString() << endl;

    cout << "-------------------------------" << endl;
    cout << r1.toString() << " + " << r2.toString() << " = " << r1.add(r2).toString() << endl;
    cout << r1.toString() << " - " << r2.toString() << " = " << r1.subtract(r2).toString() << endl;
    cout << r1.toString() << " * " << r2.toString() << " = " << r1.multiply(r2).toString() << endl;
    cout << r1.toString() << " / " << r2.toString() << " = " << r1.divide(r2).toString() << endl;

    cout << "-------------------------------" << endl;
    cout << "r2向下取整:"  << r2.intValue() << endl;
    cout << "r2转换为小数:"  << r2.doubleValue() << endl;

    cout << "-------------------------------" << endl;
    cout << "r1.compareTo(r2)的返回值是:" << r1.compareTo(r2) << endl;
    cout << "r2.compareTo(r1)的返回值是:" << r2.compareTo(r1) << endl;
    cout << "r1.compareTo(r1)的返回值是:" << r1.compareTo(r1) << endl;

    cout << "-------------------------------" << endl;
    cout << "r1.equals(r1)的返回值是:" << r1.equals(r1) << endl;
    cout << "r1.equals(r2)的返回值是:" << r1.equals(r2) << endl;

    cout << "-------------------------------" << endl;
    return 0;
}

运行结果:

posted @ 2020-05-29 15:05  川尘  阅读(1460)  评论(0编辑  收藏  举报
`