Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "rational.hpp"
- #include <cmath>
- int Rational::num() const { return _num;}
- int Rational::denum() const { return _denum;}
- int Rational::gcd(int a, int b) {
- if(b > a) return Rational::gcd(b, a);
- if(b == 0) return a;
- return Rational::gcd(b, a % b);
- }
- void Rational::normalize() {
- if (_denum < 0) {
- _denum *= -1;
- _num *= -1;
- }
- int mod = Rational::gcd(abs(_num), abs(_denum));
- _num /= mod;
- _denum /= mod;
- }
- Rational::Rational(const int num, const int denum)
- : _num(num),
- _denum(denum)
- {
- if(denum == 0) {
- throw std::invalid_argument("Denum = 0");
- }
- this->normalize();
- }
- bool Rational::operator==(Rational& rhs) const {
- return _num == rhs.num() && _denum == rhs.denum();
- }
- bool Rational::operator!=(Rational& rhs) const {
- return !operator==(rhs);
- }
- bool Rational::operator>(Rational& rhs) const {
- return (_denum == rhs.denum() && _num > rhs.num()) ||( _num == rhs.num() && _denum < rhs.denum());
- }
- bool Rational::operator>=(Rational& rhs) const {
- return (_denum == rhs.denum() && _num >= rhs.num()) ||( _num == rhs.num() && _denum <= rhs.denum());
- }
- bool Rational::operator<(Rational& rhs) const {
- return (_denum == rhs.denum() && _num < rhs.num()) ||( _num == rhs.num() && _denum > rhs.denum());
- }
- bool Rational::operator<=(Rational& rhs) const {
- return (_denum == rhs.denum() && _num <= rhs.num()) ||( _num == rhs.num() && _denum >= rhs.denum());
- }
- Rational& Rational::operator*=(const Rational& rhs) {
- _num *= rhs.num();
- _denum *= rhs.denum();
- this->normalize();
- return *this;
- }
- Rational& Rational::operator*=(const int& rhs) {
- _num *= rhs;
- this->normalize();
- return *this;
- }
- Rational& Rational::operator+=(const Rational& rhs) {
- _denum *= rhs.denum();
- _num *= rhs.denum();
- _denum += rhs.num();
- this->normalize();
- return *this;
- }
- Rational& Rational::operator+=(const int& rhs) {
- _num += (_denum * rhs);
- this->normalize();
- return *this;
- }
- Rational& Rational::operator-=(const Rational& rhs) {
- _denum *= rhs.denum();
- _num *= rhs.denum();
- _denum -= rhs.num();
- this->normalize();
- return *this;
- }
- Rational& Rational::operator-=(const int& rhs) {
- _num -= (_denum * rhs);
- this->normalize();
- return *this;
- }
- Rational& Rational::operator/=(const Rational& rhs) {
- _num *= rhs.denum();
- _denum *= rhs.num();
- this->normalize();
- return *this;
- }
- Rational& Rational::operator/=(const int& rhs) {
- _denum *= rhs;
- this->normalize();
- return *this;
- }
- std::ostream& Rational::writeTo(std::ostream& ostr) const {
- ostr << _num << sep << _denum;
- return ostr;
- }
- std::istream& Rational::readFrom(std::istream& istr) {
- int numer(0);
- int denumer(1);
- char separator(0);
- istr >> numer >> separator >> denumer;
- if(istr.good()) {
- if(separator == sep) {
- _num = numer;
- _denum = denumer;
- }
- } else {
- istr.setstate(std::ios_base::failbit);
- }
- return istr;
- }
- std::istream& operator>>(std::istream& istream, Rational& rhs) {
- return rhs.readFrom(istream);
- }
- std::ostream& operator<<(std::ostream& ostream, const Rational& rhs) {
- return rhs.writeTo(ostream);
- }
- Rational operator+(const Rational& lhs, const Rational& rhs) {
- Rational tmp(lhs);
- tmp += rhs;
- return tmp;
- }
- Rational operator-(const Rational& lhs, const Rational& rhs) {
- Rational tmp(lhs);
- tmp -= rhs;
- return tmp;
- }
- Rational operator*(const Rational& lhs, const Rational& rhs) {
- Rational tmp(lhs);
- tmp *= rhs;
- return tmp;
- }
- Rational operator/(const Rational& lhs, const Rational& rhs) {
- Rational tmp(lhs);
- tmp /= rhs;
- return tmp;
- }
- Rational operator+(const Rational& lhs, const int& rhs) {
- Rational tmp(lhs);
- tmp += rhs;
- return tmp;
- }
- Rational operator-(const Rational& lhs, const int& rhs) {
- Rational tmp(lhs);
- tmp -= rhs;
- return tmp;
- }
- Rational operator*(const Rational& lhs, const int& rhs) {
- Rational tmp(lhs);
- tmp *= rhs;
- return tmp;
- }
- Rational operator/(const Rational& lhs, const int& rhs) {
- Rational tmp(lhs);
- tmp /= rhs;
- return tmp;
- }
- Rational operator+(const int& rhs, const Rational& lhs) {
- return (lhs + rhs);
- }
- Rational operator-(const int& rhs, const Rational& lhs) {
- return (lhs - rhs);
- }
- Rational operator*(const int& rhs, const Rational& lhs) {
- return (lhs * rhs);
- }
- Rational operator/(const int& rhs, const Rational& lhs) {
- return (lhs / rhs);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement