diff options
Completed ex2 from yesterdays oop practice
1 files changed, 80 insertions, 9 deletions
| -rw-r--r-- | Year 1/Object-oriented Programming/06-Practice-29.03.2022/ex2/Rational.cpp | 89 |
diff --git a/Year 1/Object-oriented Programming/06-Practice-29.03.2022/ex2/Rational.cpp b/Year 1/Object-oriented Programming/06-Practice-29.03.2022/ex2/Rational.cpp index cc28545..ea0e3a1 100644 --- a/Year 1/Object-oriented Programming/06-Practice-29.03.2022/ex2/Rational.cpp +++ b/Year 1/Object-oriented Programming/06-Practice-29.03.2022/ex2/Rational.cpp @@ -1,6 +1,42 @@ +#include <iostream> #include "Rational.h" +bool Rational::gt(const Rational& other) const { + // x a x*b a*y + // - ? - <=> --- ? --- <=> x*b ? a*y + // y b y*b y*b + return numerator * other.denumerator > other.numerator * denumerator; +} + +bool Rational::lt(const Rational& other) const { + // x a x*b a*y + // - ? - <=> --- ? --- <=> x*b ? a*y + // y b y*b y*b + return numerator * other.denumerator < other.numerator * denumerator; +} + +int Rational::gcd() const { + int a = numerator, b = denumerator; + if (a < b) { + int temp = a; + a = b; + b = temp; + } + + while (b != 0) { + int r = a % b; + a = b; + b = r; + } + return a; +} + +Rational::Rational() : Rational(1, 1) +{} + Rational::Rational(int numerator, int denumerator) { + if (denumerator == 0) numerator = denumerator = 1; + set_numerator(numerator); set_denumerator(denumerator); } @@ -16,32 +52,67 @@ int Rational::get_denumerator() const { return denumerator; } void Rational::set_denumerator(int denumerator) { + if (denumerator == 0) denumerator = 1; + this->denumerator = denumerator; } -void canonize() { +void Rational::canonize() { + int gcdND = abs(gcd()); + set_numerator(numerator / gcdND); + set_denumerator(denumerator / gcdND); +} + +// Not needed, kept for fun +void Rational::canonizeRec() { + if (denumerator == 1) + return; + int whole = numerator / denumerator, leftover = numerator % denumerator; + if (leftover == 0) { + numerator = whole; + denumerator = 1; + return; + } + + Rational temp(denumerator, leftover); + temp.canonize(); + numerator = whole * temp.numerator + temp.denumerator; + denumerator = temp.numerator; } -Rational Rational::operator+(const Rational& other) { +Rational Rational::operator+(const Rational& other) const { + // x a x*b + a*y + // - + - = --------- + // y b y*b return Rational(numerator * other.denumerator + other.numerator * denumerator, denumerator * other.denumerator); } -//x a = x*b + y*a -//y b y*b - -Rational Rational::operator-(const Rational& other) { +Rational Rational::operator-(const Rational& other) const { + // x a x*b - a*y + // - - - = --------- + // y b y*b return Rational(numerator * other.denumerator - other.numerator * denumerator, denumerator * other.denumerator); } -Rational Rational::operator*(const Rational& other) { +Rational Rational::operator*(const Rational& other) const { + // x a x*b + // - / - <=> --- + // y b y*a return Rational(numerator * other.numerator, denumerator * other.denumerator); } -Rational Rational::operator/(const Rational& other) { - return Rational(numerator / other.numerator, denumerator / other.denumerator); +Rational Rational::operator/(const Rational& other) const { + // x a x b + // - / - <=> - * - + // y b y a + return Rational(numerator * other.denumerator, denumerator * other.numerator); } int Rational::numcmp(const Rational& arg) const { + return gt(arg) - lt(arg); +} +void Rational::print() const { + std::cout << numerator << "/" << denumerator; } |
