Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Preprocessor Directives
- #include<iostream>
- #include <cstdlib>
- using namespace std;
- void normalize(int &n, int &d);
- class Rational
- {
- public:
- Rational(int numerator, int denominator);
- Rational(int numerator);
- Rational();
- friend Rational operator+(const Rational&, const Rational&);
- friend Rational operator-(const Rational&, const Rational&);
- friend Rational operator*(const Rational&, const Rational&);
- friend Rational operator/(const Rational&, const Rational&);
- friend bool operator<(const Rational&, const Rational&);
- friend bool operator<=(const Rational&, const Rational&);
- friend bool operator>(const Rational&, const Rational&);
- friend bool operator>=(const Rational&, const Rational&);
- friend bool operator==(const Rational&, const Rational&);
- friend ostream& operator <<(ostream&, const Rational&);
- friend istream& operator >>(istream&, Rational&);
- int num() const { return n; };
- int den() const { return d; };
- void set_num(int sn) { n = sn; _normalize(); };
- void set_den(int sd) { d = sd; _normalize(); };
- void set(int sn, int sd) { n = sn; d = sd; _normalize(); };
- void _normalize() { normalize(n , d); }
- private: int n;
- int d;
- };
- Rational::Rational(int numer, int denom)
- {
- normalize(numer, denom);
- n = numer;
- d = denom;
- }
- Rational::Rational(int numer): n(numer), d(1)
- {
- }
- Rational::Rational():n(0), d(1)
- {
- }
- Rational operator +(const Rational& left, const Rational& right)
- {
- int numer = left.num() * right.den() + left.den() * right.num();
- int denom = left.den() * right.den();
- normalize(numer, denom);
- Rational local(numer, denom);
- return local;
- }
- Rational operator -(const Rational& left, const Rational& right)
- {
- int numer = left.num() * right.den() - left.den() * right.num();
- int denom = left.den() * right.den();
- normalize(numer, denom);
- Rational local(numer, denom);
- return local;
- }
- Rational operator *(const Rational& left, const Rational& right)
- {
- Rational product;
- int numer = left.num() * right.num();
- int denom = left.den() * right.den();
- normalize(numer, denom);
- product = Rational(numer, denom);
- return product;
- }
- Rational operator/(const Rational& left, const Rational& right)
- {
- Rational quotient;
- int numer = left.num() * right.den();
- int denom = left.den() * right.num();
- normalize(numer, denom);
- quotient = Rational(numer, denom);
- return quotient;
- }
- bool operator <(const Rational& left, const Rational& right)
- {
- return left.num() * right.den() < right.num() * left.den();
- }
- bool operator >(const Rational& left, const Rational& right)
- {
- return left.num() * right.den() > right.num() * left.den();
- }
- bool operator <=(const Rational& left, const Rational& right)
- {
- return left.num() * right.den() <= right.num() * left.den();
- }
- bool operator>=(const Rational& left, const Rational& right)
- {
- return left.num() * right.den() >= right.num() * left.den();
- }
- istream& operator>>(istream& in_str, Rational& right)
- {
- char ch;
- int numer, denom;
- in_str >> numer >> ch >> denom;
- right.set(numer, denom);
- if (ch != '/')
- {
- cout << "bad input format for operator >>. Aborting!"
- << endl;
- exit (1);
- }
- right._normalize();
- return in_str;
- }
- ostream& operator << (ostream& out_str, const Rational& right)
- {
- char ch;
- out_str << right.num() << '/' << right.den();
- return out_str;
- }
- int gcd(int m, int n)
- {
- int t;
- m = abs (m);
- n = abs (n);
- if(n < m)
- {
- t = m;
- m = n;
- n = t;
- }
- int r;
- r = m % n;
- while(r != 0)
- {
- r = m % n;
- m = n;
- n = r;
- }
- return m;
- }
- void normalize (int& n, int& d)
- {
- int g = gcd(n, d);
- n = n/g;
- d = d/g;
- if(n > 0 && d < 0 || n < 0 && d < 0)
- {
- n = -n;
- d = -d;
- }
- }
- //Main Function
- int main()
- {
- cout << "Testing declarations" << endl;
- cout << "Rational x, y (2), z(-5, -6), w(1,-3);" << endl;
- Rational x, y(2), z(-5, -6), w(1, -3);
- cout << "z = " << z << ", y = " << y << ", z = " << z << ", w = " << w << endl;
- cout << "Testing >> overloading: \nEnter " << "a fraction in the format "
- << "integer_numerator/integer_denominator"
- << endl;
- cin >> x;
- cout << "You entered the equivalent of: " << x << endl;
- cout << z << " - (" << w << ") = " << z - w << endl;
- cout << "Testing the constructor and normalization routines: " << endl;
- y = Rational (-128, -48); cout << "y = Rational(-128, -48) outputs as " << y << endl;
- y = Rational (-128, 48); cout << "y = Rational(-128, 48) outputs as " << y << endl;
- y = Rational (128, -48);
- cout << "y = Rational(128, -48) outputs as " << y << endl;
- Rational a(1,1);
- cout << "Rational a(1,1); a outputs as: " << a << endl;
- Rational ww = y*a;
- cout << y << " * " << a << " = " << ww << endl;
- w = Rational (25, 9);
- z = Rational (3, 5);
- cout << "Testing arithmetic and relational " << " operator overloading" << endl;
- cout << w << " * " << z << " = " << w * z << endl;
- cout << w << " + " << z << " = " << w + z << endl;
- cout << w << " - " << z << " = " << w - z << endl;
- cout << w << " / " << z << " = " << w / z << endl;
- cout << w << " < " << z << " = " << (w < z) << endl;
- cout << w << " < " << w << " = " << (w < z) << endl;
- cout << w << " <= " << z << " = " << (w <= z) << endl;
- cout << w << " <= " << w << " = " << (w <= z) << endl;
- cout << w << " > " << z << " = " << (w > z) << endl;
- cout << w << " > " << w << " = " << (w > z) << endl;
- cout << w << " >= " << z << " = " << (w >= z) << endl;
- cout << w << " >= " << w << " = " << (w >= z) << endl;
- w = Rational(-21, 9);
- z = Rational (3, 5);
- cout << w << " * " << z << " = " << w * z << endl;
- cout << w << " + " << z << " = " << w + z << endl;
- cout << w << " - " << z << " = " << w - z << endl;
- cout << w << " / " << z << " = " << w / z << endl;
- cout << w << " < " << z << " = " << (w < z) << endl;
- cout << w << " < " << w << " = " << (w < z) << endl;
- cout << w << " <= " << z << " = " << (w <= z) << endl;
- cout << w << " <= " << w << " = " << (w <= z) << endl;
- cout << w << " > " << z << " = " << (w > z) << endl;
- cout << w << " > " << w << " = " << (w > z) << endl;
- cout << w << " >= " << z << " = " << (w >= z) << endl;
- cout << w << " >= " << w << " = " << (w >= z) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement