MeehoweCK

Untitled

May 29th, 2020
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.51 KB | None | 0 0
  1. // main.cpp
  2. #include <iostream>
  3. #include "Rational.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     Rational r2(2, 11), r3(1, -3), r5(18, 6);
  10.     Rational res1 = 3 + r2 * r3;
  11.     Rational res2 = (3 + r2) * r3;
  12.     Rational res3 = 3 + r3 * (r2 + 2) / (r5 - r3);
  13.  
  14.     cout << 3 << " + " << r2 << " * " <<  r3 << " = " << res1 << endl;
  15.     cout << "(" << 3 << " + " << r2 << ")" << " * " <<  r3 << " = " << res2 << endl;
  16.     cout << 3 << " + " << r3 << " * " << "(" << r2 << " + " << 2 << ")" << "/"
  17.          << "(" << r5 << " - " << r3 << ")" << " = " << res3 << endl;
  18.  
  19.     return 0;
  20. }
  21.  
  22. // Rational.h
  23. #ifndef RATIONAL_H
  24. #define RATIONAL_H
  25.  
  26. #include <iostream>
  27.  
  28. using namespace std;
  29.  
  30. class Rational
  31. {
  32. public:
  33.    Rational(int numerator);
  34.    Rational(int numerator, int denominator);
  35.    ~Rational() {}
  36.    Rational(const Rational& copy);
  37.  
  38.    Rational& operator=(const Rational& other);
  39.    Rational& operator=(int i);
  40.  
  41.    int numerator() const { return numer; }
  42.    int denominator() const { return denom; }
  43.  
  44.    Rational operator++() const;
  45.    Rational operator--() const;
  46.  
  47.    bool operator==(const Rational& other) const;
  48.    bool operator>=(const Rational& other) const;
  49.    bool operator<=(const Rational& other) const;
  50.    bool operator>(const Rational& other) const;
  51.    bool operator<(const Rational& other) const;
  52.  
  53.    double value() const;
  54.  
  55. private:
  56.    int numer;
  57.    int denom;
  58.  
  59.    void initialize(int numerator, int denominator);
  60.  
  61.    friend ostream& operator<<(std::ostream &, const Rational &);
  62.    friend istream& operator>>(std::istream &, Rational &);
  63.    friend int nwd(int, int);
  64.  
  65.    friend Rational operator+(const Rational&, const Rational&);
  66.    friend Rational operator-(const Rational&, const Rational&);
  67.    friend Rational operator*(const Rational&, const Rational&);
  68.    friend Rational operator/(const Rational&, const Rational&);
  69. };
  70.  
  71.  
  72. #endif // RATIONAL_H
  73.  
  74. // Rational.cpp
  75. #include "Rational.h"
  76.  
  77. Rational::Rational(int numerator) : numer(numerator), denom(1)
  78. {
  79. }
  80.  
  81. Rational::Rational(int numerator, int denominator) : numer(numerator), denom(denominator)
  82. {
  83. }
  84.  
  85. Rational::Rational(const Rational& copy) : numer(copy.numer), denom(copy.denom)
  86. {
  87. }
  88.  
  89. Rational& Rational::operator=(const Rational& other)
  90. {
  91.     numer = other.numer;
  92.     denom = other.denom;
  93. }
  94.  
  95. Rational& Rational::operator=(int i)
  96. {
  97.     numer = i;
  98.     denom = 1;
  99. }
  100.  
  101. int nwd(int a, int b)
  102. {
  103.     if(a * b == 0)
  104.         return 0;
  105.     a = abs(a);
  106.     b = abs(b);
  107.     while(a != b)
  108.     {
  109.         if(a > b)
  110.             a -= b;
  111.         if(b > a)
  112.             b -= a;
  113.     }
  114.     return a;
  115. }
  116.  
  117. Rational Rational::operator++() const
  118. {
  119.     int a = numer + denom;
  120.     Rational product(a, denom);
  121.     return product;
  122. }
  123.  
  124. Rational Rational::operator--() const
  125. {
  126.     int a = numer - denom;
  127.     Rational product(a, denom);
  128.     return product;
  129. }
  130.  
  131. bool Rational::operator==(const Rational& other) const
  132. {
  133.     if(value() == other.value())
  134.         return true;
  135.     return false;
  136. }
  137.  
  138. bool Rational::operator>=(const Rational& other) const
  139. {
  140.     if(value() >= other.value())
  141.         return true;
  142.     return false;
  143. }
  144.  
  145. bool Rational::operator<=(const Rational& other) const
  146. {
  147.     if(value() <= other.value())
  148.         return true;
  149.     return false;
  150. }
  151.  
  152. bool Rational::operator>(const Rational& other) const
  153. {
  154.     if(value() > other.value())
  155.         return true;
  156.     return false;
  157. }
  158.  
  159. bool Rational::operator<(const Rational& other) const
  160. {
  161.     if(value() < other.value())
  162.         return true;
  163.     return false;
  164. }
  165.  
  166. double Rational::value() const
  167. {
  168.     return 1.0 * numer / denom;
  169. }
  170.  
  171. void Rational::initialize(int numerator, int denominator)
  172. {
  173.     numer = numerator;
  174.     denom = denominator;
  175. }
  176.  
  177. ostream& operator<<(std::ostream & str, const Rational & ob)
  178. {
  179.     if(ob.denom == 1)
  180.     {
  181.         str << ob.numer;
  182.         return str;
  183.     }
  184.     if(ob.denom == -1)
  185.     {
  186.         str << "(-" << abs(ob.numer) << ")";
  187.         return str;
  188.     }
  189.     if(ob.numer * ob.denom < 0)
  190.         str << "(-" << abs(ob.numer) << "/" << abs(ob.denom) << ")";
  191.     else
  192.         str << abs(ob.numer) << "/" << abs(ob.denom);
  193.     return str;
  194. }
  195.  
  196. istream& operator>>(std::istream & str, Rational & ob)
  197. {
  198.     str >> ob.numer >> ob.denom;
  199.     return str;
  200. }
  201.  
  202. Rational operator+(const Rational& A, const Rational& B)
  203. {
  204.     int a = A.numer * B.denom + B.numer * A.denom;
  205.     int b = A.denom * B.denom;
  206.     int n = nwd(a,b);
  207.     if(n == 0)
  208.     {
  209.         Rational product(0);
  210.         return product;
  211.     }
  212.     a /= n;
  213.     b /= n;
  214.     Rational product(a,b);
  215.     return product;
  216. }
  217.  
  218. Rational operator-(const Rational& A, const Rational& B)
  219. {
  220.     int a = A.numer * B.denom - B.numer * A.denom;
  221.     int b = A.denom * B.denom;
  222.     int n = nwd(a,b);
  223.     if(n == 0)
  224.     {
  225.         Rational product(0);
  226.         return product;
  227.     }
  228.     a /= n;
  229.     b /= n;
  230.     Rational product(a,b);
  231.     return product;
  232. }
  233.  
  234. Rational operator*(const Rational& A, const Rational& B)
  235. {
  236.     int a = A.numer * B.numer;
  237.     if(a == 0)
  238.     {
  239.         Rational product(0,1);
  240.         return product;
  241.     }
  242.     int b = A.denom * B.denom;
  243.     int n = nwd(a,b);
  244.     a /= n;
  245.     b /= n;
  246.     Rational product(a,b);
  247.     return product;
  248. }
  249.  
  250. Rational operator/(const Rational& A, const Rational& B)
  251. {
  252.     int a = A.numer * B.denom;
  253.     if(a == 0)
  254.     {
  255.         Rational product(0,1);
  256.         return product;
  257.     }
  258.     int b = A.denom * B.numer;
  259.     int n = nwd(a,b);
  260.     a /= n;
  261.     b /= n;
  262.     Rational product(a,b);
  263.     return product;
  264. }
Advertisement
Add Comment
Please, Sign In to add comment