/
dolpement
/
programming-and-algorithmization
Обзор
Документация
Войти
/
dolpement
/
programming-and-algorithmization
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
rational_prj/rational/rational.cpp
222 строки
6 KB
dolpement
programming
13 мар 2025, 21:11
13 мар 2025, 21:11
24f0ba5
Код
Авторство
О чём код?
#include "rational.hpp" #include <iostream> #include <numeric> Rational::Rational(const std::int64_t num, const std::int64_t den) { if (den == 0) { throw std::invalid_argument("Знаменатель равен 0"); } num_ = num; den_ = den; } std::int64_t Rational::num() const noexcept { return num_; } std::int64_t Rational::den() const noexcept { return den_; } void Rational::reduce() { std::int64_t gcd = std::gcd(num_, den_); num_ /= gcd; den_ /= gcd; if (den_ < 0) { num_ = -num_; den_ = -den_; } } bool Rational::operator==(const Rational& rhs) const noexcept { if ((this->num_ == rhs.num_) && (this->den_ == rhs.den_)) { return true; } else { return false; } } bool Rational::operator!=(const Rational &rhs) const noexcept { if ((this->num_ != rhs.num_) || (this->den_ != rhs.den_)) { return true; } else { return false; } } bool Rational::operator<(const Rational& rhs) const noexcept { if ((this->den_*rhs.den_ > 0) && (this->num_*rhs.den_ - rhs.num_*den_ < 0)) { return true; } else if ((this->den_*rhs.den_ < 0) && (this->num_*rhs.den_ - rhs.num_*(this->den_) > 0)) { return true; } else { return false; } } bool Rational::operator<=(const Rational& rhs) const noexcept { if ((this->den_*rhs.den_ > 0) && (this->num_*rhs.den_ - rhs.num_*(this->den_) <= 0)) { return true; } else if ((this->den_*rhs.den_ < 0) && (this->num_*rhs.den_ - rhs.num_*(this->den_) >= 0)) { return true; } else { return false; } } bool Rational::operator>(const Rational& rhs) const noexcept { if ((this->num_*rhs.den_ > rhs.num_*(this->den_)) && (this->den_*rhs.den_ > 0)) { return true; } else if ((this->num_*rhs.den_ < rhs.num_*(this->den_)) && (this->den_*rhs.den_ < 0)) { return true; } else { return false; } } bool Rational::operator>=(const Rational& rhs) const noexcept { if ((this->num_*rhs.den_ >= rhs.num_*(this->den_)) && (this->den_*rhs.den_ > 0)) { return true; } else if ((this->num_*rhs.den_ <= rhs.num_*(this->den_)) && (den_*rhs.den_ < 0)) { return true; } else { return false; } } Rational Rational::operator-() const noexcept { return Rational(-num_, den_); } Rational& Rational::operator+=(const Rational& rhs) noexcept { this->num_ = this->num_*rhs.den_ + rhs.num_*(this->den_); this->den_ = this->den_*rhs.den_; return *this; } Rational& Rational::operator-=(const Rational& rhs) noexcept { this->num_ = this->num_*rhs.den_ - rhs.num_*(this->den_); this->den_ = this->den_*rhs.den_; return *this; } Rational& Rational::operator*=(const Rational& rhs) noexcept { this->num_ = this->num_*rhs.num_; this->den_ = this->den_*rhs.den_; return *this; } Rational& Rational::operator/=(const Rational& rhs) { if (rhs.num_ != 0) { this->num_ = this->num_*rhs.den_; this->den_ = this->den_*rhs.num_; return *this; } else { throw std::invalid_argument("Знаменатель равен 0"); } } Rational& Rational::operator+=(const int64_t rhs) noexcept { this->num_ = this->num_ + den_*rhs; return *this; } Rational& Rational::operator-=(const int64_t rhs) noexcept { this->num_ = this->num_ - den_*rhs; return *this; } Rational& Rational::operator*=(const int64_t rhs) noexcept { this->num_ = this->num_*rhs; return *this; } Rational& Rational::operator/=(const int64_t rhs) { this->den_ = this->den_*rhs; return *this; } std::ostream& Rational::WriteTo(std::ostream& ostrm) const noexcept { int64_t num_ (0); int64_t den_ (0); ostrm << num_ << "/" << den_; return ostrm; } std::istream& Rational::ReadFrom(std::istream& istrm) noexcept { int64_t num_ (0); int64_t den_ (0); char slash { '/' }; istrm >> num_ >> slash >> den_; return istrm; } Rational operator+(const Rational& lhs, const Rational& rhs) noexcept { Rational new_rational = lhs; return new_rational += rhs; } Rational operator-(const Rational& lhs, const Rational& rhs) noexcept { Rational new_rational = lhs; return new_rational -= rhs; } Rational operator*(const Rational& lhs, const Rational& rhs) noexcept { Rational new_rational = lhs; return new_rational *= rhs; } Rational operator/(const Rational& lhs, const Rational& rhs) { Rational new_rational = lhs; return new_rational /= rhs; } Rational operator+(const Rational& lhs, const int64_t rhs) noexcept { Rational new_rational = lhs; return new_rational += rhs; } Rational operator-(const Rational& lhs, const int64_t rhs) noexcept { Rational new_rational = lhs; return new_rational -= rhs; } Rational operator*(const Rational& lhs, const int64_t rhs) noexcept { Rational new_rational = lhs; return new_rational *= rhs; } Rational operator/(const Rational& lhs, const int64_t rhs) { Rational new_rational = lhs; return new_rational /= rhs; } Rational operator+(const int64_t lhs, const Rational& rhs) noexcept { Rational new_rational = rhs; return new_rational += lhs; } Rational operator-(const int64_t lhs, const Rational& rhs) noexcept { Rational new_rational = -rhs; return new_rational += lhs; } Rational operator*(const int64_t lhs, const Rational& rhs) noexcept { Rational new_rational = rhs; return new_rational *= lhs; } Rational operator/(const int64_t lhs, const Rational& rhs) { Rational new_rational (lhs, 1); return new_rational /= rhs; } std::ostream& operator<<(std::ostream& ostrm, const Rational& rhs) noexcept { return rhs.WriteTo(ostrm); } std::istream& operator>>(std::istream& istrm, Rational& rhs) noexcept { return rhs.ReadFrom(istrm); }