Advertisement
Guest User

Untitled

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