Last active
March 16, 2022 16:06
-
-
Save DragonOsman/ca59e789ecc3862a13256d372401c9e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module rational; | |
import <iostream>; | |
import <numeric>; | |
int gcd(int a, int b) | |
{ | |
for (;;) | |
{ | |
if (a == 0) | |
{ | |
return b; | |
} | |
b %= a; | |
if (b == 0) | |
{ | |
return a; | |
} | |
a %= b; | |
} | |
} | |
int lcm(int a, int b) | |
{ | |
int temp{ gcd(a, b) }; | |
return temp ? (a / temp * b) : 0; | |
} | |
bool Rational::operator==(const int other_value) const | |
{ | |
return get_rational_value() == other_value; | |
} | |
Rational Rational::operator+(const Rational& rational) const | |
{ | |
Rational copy{ *this }; | |
copy += rational; | |
return copy; | |
} | |
Rational Rational::operator-(const Rational& rational) const | |
{ | |
Rational copy{ *this }; | |
copy -= rational; | |
return copy; | |
} | |
Rational Rational::operator*(const Rational& rational) const | |
{ | |
Rational copy{ *this }; | |
copy *= rational; | |
return copy; | |
} | |
Rational Rational::operator/(const Rational& rational) const | |
{ | |
Rational copy{ *this }; | |
copy /= rational; | |
return copy; | |
} | |
Rational Rational::operator+(const int other_value) const | |
{ | |
Rational copy{ *this }; | |
copy += other_value; | |
return copy; | |
} | |
Rational Rational::operator-(const int other_value) const | |
{ | |
Rational copy{ *this }; | |
copy -= other_value; | |
return copy; | |
} | |
Rational Rational::operator*(const int other_value) const | |
{ | |
Rational copy{ *this }; | |
copy *= other_value; | |
return copy; | |
} | |
Rational Rational::operator/(const int other_value) const | |
{ | |
Rational copy{ *this }; | |
copy /= other_value; | |
return copy; | |
} | |
Rational operator+=(const Rational& rational) | |
{ | |
if (m_denominator == rational.m_denominator) | |
{ | |
m_numerator += rational.m_numerator; | |
return *this; | |
} | |
int lcm_val{ lcm(m_numerator, rational.m_numerator) }; | |
int new_numerator1{ m_numerator * lcm_val }; | |
int new_denominator1{ m_denominator * lcm_val }; | |
int new_numerator2{ rational.m_numerator * lcm_val }; | |
int new_denominator2{ rational.m_denominator * lcm_val }; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export module rational; | |
import <ostream>; | |
import <compare>; | |
export class Rational | |
{ | |
public: | |
Rational(const int numerator = 0, const int denominator = 1) | |
: m_numerator{ numerator }, m_denominator{ denominator } {} | |
void set_numerator(const int value) { m_numerator = value; } | |
void set_denominator(const int value) { m_denominator = value; } | |
double get_rational_value() const { return m_numerator / m_denominator; } | |
bool operator==(const Rational& rational) const = default; | |
bool operator==(int other_value) const; | |
std::partial_ordering operator<=>(const Rational& rational) const | |
{ | |
return get_rational_value() <=> rational.get_rational_value(); | |
} | |
std::partial_ordering operator<=>(int other_value) const | |
{ | |
return get_rational_value() <=> other_value; | |
} | |
Rational operator+(const Rational& rational) const; | |
Rational operator-(const Rational& rational) const; | |
Rational operator*(const Rational& rational) const; | |
Rational operator/(const Rational& rational) const; | |
Rational operator+(int other_value) const; | |
Rational operator-(int other_value) const; | |
Rational operator*(int other_value) const; | |
Rational operator/(int other_value) const; | |
Rational operator+=(const Rational& rational); | |
Rational operator-=(const Rational& rational); | |
Rational operator*=(const Rational& rational); | |
Rational operator/=(const Rational& rational); | |
Rational operator+=(int other_value); | |
Rational operator-=(int other_value); | |
Rational operator*=(int other_value); | |
Rational operator/=(int other_value); | |
Rational operator++(); | |
Rational operator++(int); | |
Rational operator--(); | |
Rational operator--(int); | |
double operator()() const { return get_rational_value(); } | |
explicit operator bool() const | |
{ | |
if (static_cast<double>(m_numerator / m_denominator) >= 1.0) | |
{ | |
return true; | |
} | |
return false; | |
} | |
private: | |
int m_numerator; | |
int m_denominator; | |
}; | |
export std::ostream& operator<<(const Rational& rational); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment