Advertisement
kasougi

Untitled

Dec 22nd, 2021
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.66 KB | None | 0 0
  1. #include "rational.hpp"
  2.  
  3. #include <cmath>
  4.  
  5.  
  6. int Rational::num() const { return _num;}
  7. int Rational::denum() const { return _denum;}
  8.  
  9. int Rational::gcd(int a, int b) {
  10.     if(b > a) return Rational::gcd(b, a);
  11.     if(b == 0) return a;
  12.     return Rational::gcd(b, a % b);
  13. }
  14.  
  15. void Rational::normalize() {
  16.     if (_denum < 0) {
  17.         _denum *= -1;
  18.         _num *= -1;
  19.     }
  20.    
  21.     int mod = Rational::gcd(abs(_num), abs(_denum));
  22.    
  23.     _num /= mod;
  24.     _denum /= mod;
  25. }
  26.  
  27. Rational::Rational(const int num, const int denum)
  28. : _num(num),
  29. _denum(denum)
  30. {
  31.     if(denum == 0) {
  32.         throw std::invalid_argument("Denum = 0");
  33.     }
  34.    
  35.     this->normalize();
  36. }
  37.  
  38. bool Rational::operator==(Rational& rhs) const {
  39.     return _num == rhs.num() && _denum == rhs.denum();
  40. }
  41.  
  42. bool Rational::operator!=(Rational& rhs) const {
  43.     return !operator==(rhs);
  44. }
  45.  
  46. bool Rational::operator>(Rational& rhs) const {
  47.     return (_denum == rhs.denum() && _num > rhs.num()) ||( _num == rhs.num() && _denum < rhs.denum());
  48. }
  49.  
  50. bool Rational::operator>=(Rational& rhs) const {
  51.     return (_denum == rhs.denum() && _num >= rhs.num()) ||( _num == rhs.num() && _denum <= rhs.denum());
  52. }
  53.  
  54. bool Rational::operator<(Rational& rhs) const {
  55.     return (_denum == rhs.denum() && _num < rhs.num()) ||( _num == rhs.num() && _denum > rhs.denum());
  56. }
  57.  
  58. bool Rational::operator<=(Rational& rhs) const {
  59.     return (_denum == rhs.denum() && _num <= rhs.num()) ||( _num == rhs.num() && _denum >= rhs.denum());
  60. }
  61.  
  62.  
  63. Rational& Rational::operator*=(const Rational& rhs) {
  64.     _num *= rhs.num();
  65.     _denum *= rhs.denum();
  66.     this->normalize();
  67.     return *this;
  68. }
  69.  
  70. Rational& Rational::operator*=(const int& rhs) {
  71.     _num *= rhs;
  72.     this->normalize();
  73.     return *this;
  74. }
  75.  
  76. Rational& Rational::operator+=(const Rational& rhs) {
  77.     _denum *= rhs.denum();
  78.     _num *= rhs.denum();
  79.     _denum += rhs.num();
  80.     this->normalize();
  81.     return *this;
  82. }
  83.  
  84. Rational& Rational::operator+=(const int& rhs) {
  85.     _num += (_denum * rhs);
  86.     this->normalize();
  87.     return *this;
  88. }
  89.  
  90. Rational& Rational::operator-=(const Rational& rhs) {
  91.     _denum *= rhs.denum();
  92.     _num *= rhs.denum();
  93.     _denum -= rhs.num();
  94.     this->normalize();
  95.     return *this;
  96. }
  97.  
  98. Rational& Rational::operator-=(const int& rhs) {
  99.     _num -= (_denum * rhs);
  100.     this->normalize();
  101.     return *this;
  102. }
  103.  
  104. Rational& Rational::operator/=(const Rational& rhs) {
  105.     _num *= rhs.denum();
  106.     _denum *= rhs.num();
  107.     this->normalize();
  108.     return *this;
  109. }
  110.  
  111. Rational& Rational::operator/=(const int& rhs) {
  112.     _denum *= rhs;
  113.     this->normalize();
  114.     return *this;
  115. }
  116.  
  117. std::ostream& Rational::writeTo(std::ostream& ostr) const {
  118.     ostr << _num << sep << _denum;
  119.     return ostr;
  120. }
  121.  
  122. std::istream& Rational::readFrom(std::istream& istr) {
  123.     int numer(0);
  124.     int denumer(1);
  125.     char separator(0);
  126.    
  127.     istr >> numer >> separator >> denumer;
  128.    
  129.     if(istr.good()) {
  130.         if(separator == sep) {
  131.             _num = numer;
  132.             _denum = denumer;
  133.         }
  134.     } else {
  135.         istr.setstate(std::ios_base::failbit);
  136.     }
  137.     return istr;
  138. }
  139.  
  140. std::istream& operator>>(std::istream& istream, Rational& rhs) {
  141.     return rhs.readFrom(istream);
  142. }
  143.  
  144. std::ostream& operator<<(std::ostream& ostream, const Rational& rhs) {
  145.     return rhs.writeTo(ostream);
  146. }
  147.  
  148. Rational operator+(const Rational& lhs, const Rational& rhs) {
  149.     Rational tmp(lhs);
  150.     tmp += rhs;
  151.     return tmp;
  152. }
  153.  
  154. Rational operator-(const Rational& lhs, const Rational& rhs) {
  155.     Rational tmp(lhs);
  156.     tmp -= rhs;
  157.     return tmp;
  158. }
  159.  
  160. Rational operator*(const Rational& lhs, const Rational& rhs) {
  161.     Rational tmp(lhs);
  162.     tmp *= rhs;
  163.     return tmp;
  164. }
  165.  
  166. Rational operator/(const Rational& lhs, const Rational& rhs) {
  167.     Rational tmp(lhs);
  168.     tmp /= rhs;
  169.     return tmp;
  170. }
  171.  
  172. Rational operator+(const Rational& lhs, const int& rhs) {
  173.     Rational tmp(lhs);
  174.     tmp += rhs;
  175.     return tmp;
  176. }
  177.  
  178. Rational operator-(const Rational& lhs, const int& rhs) {
  179.     Rational tmp(lhs);
  180.     tmp -= rhs;
  181.     return tmp;
  182. }
  183.  
  184. Rational operator*(const Rational& lhs, const int& rhs) {
  185.     Rational tmp(lhs);
  186.     tmp *= rhs;
  187.     return tmp;
  188. }
  189.  
  190. Rational operator/(const Rational& lhs, const int& rhs) {
  191.     Rational tmp(lhs);
  192.     tmp /= rhs;
  193.     return tmp;
  194. }
  195.  
  196. Rational operator+(const int& rhs, const Rational& lhs) {
  197.     return (lhs + rhs);
  198. }
  199.  
  200. Rational operator-(const int& rhs, const Rational& lhs) {
  201.     return (lhs - rhs);
  202. }
  203.  
  204. Rational operator*(const int& rhs, const Rational& lhs) {
  205.     return (lhs * rhs);
  206. }
  207.  
  208. Rational operator/(const int& rhs, const Rational& lhs) {
  209.     return (lhs / rhs);
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement