Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.11 KB | None | 0 0
  1. //Preprocessor Directives
  2. #include<iostream>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6.  
  7. void normalize(int &n, int &d);
  8.  
  9. class Rational
  10. {
  11. public:
  12.     Rational(int numerator, int denominator);
  13.     Rational(int numerator);
  14.     Rational();
  15.     friend Rational operator+(const Rational&, const Rational&);
  16.     friend Rational operator-(const Rational&, const Rational&);
  17.     friend Rational operator*(const Rational&, const Rational&);
  18.     friend Rational operator/(const Rational&, const Rational&);
  19.     friend bool operator<(const Rational&, const Rational&);
  20.     friend bool operator<=(const Rational&, const Rational&);
  21.     friend bool operator>(const Rational&, const Rational&);
  22.     friend bool operator>=(const Rational&, const Rational&);
  23.     friend bool operator==(const Rational&, const Rational&);
  24.     friend ostream& operator <<(ostream&, const Rational&);
  25.     friend istream& operator >>(istream&, Rational&);
  26.     int num() const { return n; };
  27.     int den() const { return d; };
  28.     void set_num(int sn) { n = sn; _normalize(); };
  29.     void set_den(int sd) { d = sd; _normalize(); };
  30.     void set(int sn, int sd) { n = sn; d = sd; _normalize(); };
  31.     void _normalize() { normalize(n , d); }
  32. private: int n;
  33.          int d;
  34. };
  35.  
  36. Rational::Rational(int numer, int denom)
  37. {
  38.     normalize(numer, denom);
  39.     n = numer;
  40.     d = denom;
  41. }
  42. Rational::Rational(int numer): n(numer), d(1)
  43. {
  44.  
  45. }
  46. Rational::Rational():n(0), d(1)
  47. {
  48.  
  49. }
  50. Rational operator +(const Rational& left, const Rational& right)
  51. {
  52.     int numer = left.num() * right.den() + left.den() * right.num();
  53.     int denom = left.den() * right.den();
  54.     normalize(numer, denom);
  55.     Rational local(numer, denom);
  56.     return local;
  57. }
  58. Rational operator -(const Rational& left, const Rational& right)
  59. {
  60.     int numer = left.num() * right.den() - left.den() * right.num();
  61.     int denom = left.den() * right.den();
  62.     normalize(numer, denom);
  63.     Rational local(numer, denom);
  64.     return local;
  65. }
  66. Rational operator *(const Rational& left, const Rational& right)
  67. {
  68. Rational product;
  69.  
  70. int numer = left.num() * right.num();
  71. int denom = left.den() * right.den();
  72. normalize(numer, denom);
  73. product = Rational(numer, denom);
  74. return product;
  75. }
  76. Rational operator/(const Rational& left, const Rational& right)
  77. {
  78.     Rational quotient;
  79.     int numer = left.num() * right.den();
  80.     int denom = left.den() * right.num();
  81.     normalize(numer, denom);
  82.     quotient = Rational(numer, denom);
  83.     return quotient;
  84. }
  85. bool operator <(const Rational& left, const Rational& right)
  86. {
  87.     return left.num() * right.den() < right.num() * left.den();
  88. }
  89. bool operator >(const Rational& left, const Rational& right)
  90. {
  91.     return left.num() * right.den() > right.num() * left.den();
  92. }
  93. bool operator <=(const Rational& left, const Rational& right)
  94. {
  95.     return left.num() * right.den() <= right.num() * left.den();
  96. }
  97. bool operator>=(const Rational& left, const Rational& right)
  98. {
  99.     return left.num() * right.den() >= right.num() * left.den();
  100. }
  101. istream& operator>>(istream& in_str, Rational& right)
  102. {
  103.     char ch;
  104.     int numer, denom;
  105.     in_str >> numer >> ch >> denom;
  106.     right.set(numer, denom);
  107.     if (ch != '/')
  108.     {
  109.         cout << "bad input format for operator >>. Aborting!"
  110.              << endl;
  111.         exit (1);
  112.     }
  113.     right._normalize();
  114.     return in_str;
  115. }
  116. ostream& operator << (ostream& out_str, const Rational& right)
  117. {
  118.     char ch;
  119.     out_str << right.num() << '/' << right.den();
  120.     return out_str;
  121. }
  122. int gcd(int m, int n)
  123. {
  124.     int t;
  125.     m = abs (m);
  126.     n = abs (n);
  127.     if(n < m)
  128.     {
  129.         t = m;
  130.         m = n;
  131.         n = t;
  132.     }
  133.     int r;
  134.     r = m % n;
  135.     while(r != 0)
  136.     {
  137.         r = m % n;
  138.         m = n;
  139.         n = r;
  140.     }
  141.     return m;
  142. }
  143. void normalize (int& n, int& d)
  144. {
  145.     int g = gcd(n, d);
  146.     n = n/g;
  147.     d = d/g;
  148.     if(n > 0 && d < 0 || n < 0 && d < 0)
  149.     {
  150.         n = -n;
  151.         d = -d;
  152.     }
  153. }
  154. //Main Function
  155. int main()
  156. {
  157.     cout << "Testing declarations" << endl;
  158.     cout << "Rational x, y (2), z(-5, -6), w(1,-3);" << endl;
  159.     Rational x, y(2), z(-5, -6), w(1, -3);
  160.     cout << "z = " << z << ", y = " << y << ", z = " << z << ", w = " << w << endl;
  161.     cout << "Testing >> overloading: \nEnter " << "a fraction in the format "
  162.         << "integer_numerator/integer_denominator"
  163.         << endl;
  164.     cin >> x;
  165.     cout << "You entered the equivalent of: " << x << endl;
  166.     cout << z << " - (" << w << ") = " << z - w << endl;
  167.     cout << "Testing the constructor and normalization routines: " << endl;
  168.     y = Rational (-128, -48); cout << "y = Rational(-128, -48) outputs as " << y << endl;
  169.     y = Rational (-128, 48); cout << "y = Rational(-128, 48) outputs as " << y << endl;
  170.     y = Rational (128, -48);
  171.     cout << "y = Rational(128, -48) outputs as " << y << endl;
  172.     Rational a(1,1);
  173.     cout << "Rational a(1,1); a outputs as: " << a << endl;
  174.     Rational ww = y*a;
  175.     cout << y << " * " << a << " = " << ww << endl;
  176.     w = Rational (25, 9);
  177.     z = Rational (3, 5);
  178.     cout << "Testing arithmetic and relational " << " operator overloading" << endl;
  179.     cout << w << " * " << z << " = " << w * z << endl;
  180.     cout << w << " + " << z << " = " << w + z << endl;
  181.     cout << w << " - " << z << " = " << w - z << endl;
  182.     cout << w << " / " << z << " = " << w / z << endl;
  183.     cout << w << " < " << z << " = " << (w < z) << endl;
  184.     cout << w << " < " << w << " = " << (w < z) << endl;
  185.     cout << w << " <= " << z << " = " << (w <= z) << endl;
  186.     cout << w << " <= " << w << " = " << (w <= z) << endl;
  187.     cout << w << " > " << z << " = " << (w > z) << endl;
  188.     cout << w << " > " << w << " = " << (w > z) << endl;
  189.     cout << w << " >= " << z << " = " << (w >= z) << endl;
  190.     cout << w << " >= " << w << " = " << (w >= z) << endl;
  191.     w = Rational(-21, 9);
  192.     z = Rational (3, 5);
  193.     cout << w << " * " << z << " = " << w * z << endl;
  194.     cout << w << " + " << z << " = " << w + z << endl;
  195.     cout << w << " - " << z << " = " << w - z << endl;
  196.     cout << w << " / " << z << " = " << w / z << endl;
  197.     cout << w << " < " << z << " = " << (w < z) << endl;
  198.     cout << w << " < " << w << " = " << (w < z) << endl;
  199.     cout << w << " <= " << z << " = " << (w <= z) << endl;
  200.     cout << w << " <= " << w << " = " << (w <= z) << endl;
  201.     cout << w << " > " << z << " = " << (w > z) << endl;
  202.     cout << w << " > " << w << " = " << (w > z) << endl;
  203.     cout << w << " >= " << z << " = " << (w >= z) << endl;
  204.     cout << w << " >= " << w << " = " << (w >= z) << endl;
  205.     return 0;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement