Advertisement
Idanref

Lab 5 - Fractions

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