Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / 06-Practice-29.03.2022 / ex2 / Rational.cpp
diff options
context:
space:
mode:
Commitef6d8e6de6061015e7dd41e2f74b733515b4a2e2 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-03-30 15:17:07 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-03-30 15:17:07 +0300
Files285ccedc5b4296a7022f4de131bbb5ab44bd99bc /Year 1/Object-oriented Programming/06-Practice-29.03.2022/ex2/Rational.cpp
uni-projects-ef6d8e6de6061015e7dd41e2f74b733515b4a2e2.tar
uni-projects-ef6d8e6de6061015e7dd41e2f74b733515b4a2e2.tar.gz
uni-projects-ef6d8e6de6061015e7dd41e2f74b733515b4a2e2.zip
Parent793100813b3c50a1f304904440366a7137ae0992 (diff)
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.cpp89
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;
}