Задача
Рациональная (несократимая) дробь представляется парой целых чисел (а, 6), где а — числитель, Ь — знаменатель. Создать класс Rational для работы с ра-циональными дробями. Обязательно должна быть реализованы операции:
• сложения add, (a, b) + (с, d) = (ad + be, bd)
• вычитания sub, (a, b) - (с, d) = (ad - be, bd)
• умножения mul, (a, b) x (c, d) = (ac, bd)
• деления div, (a>b) / (c, d) = (ad, be);
• сравнения equal, greate, less.
Должна быть реализована приватная функция сокращения дроби reduce
Вопрос: Как можно реализовать приватную функцию сокращения дроби reduce? Код прилагаю, наработки по этому вопросу выделил в комментарии
Rational.h
Код:
#ifndef RATIONAL_H
#define RATIONAL_H
#include <iostream>
#include <sstream>
class Rational {
private:
int Numerator{};
int Denominator{};
/*int GetReduced() {
int nod(int a, int b);
int mod = 1;
if (Numerator == 0 && Denominator == 0) return 0;
Numerator = abs(Numerator);
Denominator = abs(Denominator);
if (Numerator == Denominator) return Numerator;
if (Denominator > Numerator) {
mod = Numerator;
Numerator = Denominator;
Denominator = mod;
}
if (Denominator == 0) return Numerator;
while (mod) {
mod = Numerator % Denominator;
if (mod) {
Numerator = Denominator;
Denominator = mod;
}
}
return Denominator;
}*/
public:
Rational() = default;
Rational(int Numerator, int Denominator);
Rational(const Rational& R);
void SetNumerator();
void SetDenominator();
int GetNumerator() const;
int GetDenominator() const;
friend Rational operator + (const Rational& a, const Rational& b);
friend Rational operator - (const Rational& a, const Rational& b);
friend Rational operator * (const Rational& a, const Rational& b);
friend Rational operator / (const Rational& a, const Rational& b);
friend bool operator > (const Rational& a, const Rational& b);
friend bool operator < (const Rational& a, const Rational& b);
friend bool operator == (const Rational& a, const Rational& b);
friend bool operator != (const Rational& a, const Rational& b);
friend std::ostream& operator << (std::ostream& out, const Rational&);
friend std::istream& operator >> (std::istream& in, Rational&);
};
#endif
Rational.cpp
Код:
#include <iostream>
#include "Rational.h"
Rational::Rational(int Numerator, int Denominator): Numerator(Numerator), Denominator(Denominator) {}
Rational::Rational(const Rational& R): Numerator{ R.Numerator }, Denominator{ R.Denominator } {}
void Rational::SetNumerator(){}
void Rational::SetDenominator(){}
int Rational::GetNumerator() const { return Numerator; }
int Rational::GetDenominator() const { return Denominator; }
/*Rational GetReduced(){
int gcd = GetGCD(GetNumerator(), GetDenominator());
int Numerator = GetNumerator() / gcd;
int Denominator = GetDenominator() / gcd;
if (Denominator < 0) {
Numerator = -Numerator;
Denominator = -Denominator;
}
return Rational(Numerator, Denominator);
}*/
double GetGCD(int& a, int& b) {
int x = std::abs(a);
int y = std::abs(b);
while (1) {
x = x % y;
if (x == 0) {
return y;
}
y = y % x;
if (y == 0) {
return x;
}
}
}
Rational operator + (const Rational& a, const Rational& b) {
return Rational(a.GetNumerator() * b.GetDenominator() + b.GetNumerator() * a.GetDenominator(), a.GetDenominator() * b.GetDenominator());
}
Rational operator - (const Rational& a, const Rational& b) {
return Rational(a.GetNumerator() * b.GetDenominator() - b.GetNumerator() * a.GetDenominator(), a.GetDenominator() * b.GetDenominator());
}
Rational operator * (const Rational& a, const Rational& b) {
return Rational(a.GetNumerator() * b.GetNumerator(), a.GetDenominator() * b.GetDenominator());
}
Rational operator / (const Rational& a, const Rational& b) {
return Rational(a.GetNumerator() * b.GetDenominator(), a.GetDenominator() * b.GetNumerator());
}
/*std::ostream& operator << (std::ostream& out, const Rational& R) {
Rational reduced = R.GetReduced();
return out << reduced.GetNumerator() << "/" << reduced.GetDenominator();
}*/
bool operator > (const Rational& a, const Rational& b) { return (a.GetNumerator()/b.GetNumerator()) > (a.GetDenominator()/b.GetDenominator()); }
bool operator < (const Rational& a, const Rational& b) { return (a.GetNumerator() / b.GetNumerator()) < (a.GetDenominator() / b.GetDenominator()); }
bool operator == (const Rational& a, const Rational& b) { return (a.GetNumerator() == b.GetNumerator() && a.GetDenominator() == b.GetDenominator()); }
bool operator != (const Rational& a, const Rational& b) { return !(a == b); }
std::ostream& operator << (std::ostream& out, const Rational& R) {
out << "Numerator, Denominator: " << R.Numerator << ", " << R.Denominator;
return out;
};
std::istream& operator >> (std::istream& in, Rational& R) {
in >> R.Numerator >> R.Denominator;
return in;
};