Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.cpp
- #include <iostream>
- #include "Rational.h"
- using namespace std;
- int main()
- {
- Rational r2(2, 11), r3(1, -3), r5(18, 6);
- Rational res1 = 3 + r2 * r3;
- Rational res2 = (3 + r2) * r3;
- Rational res3 = 3 + r3 * (r2 + 2) / (r5 - r3);
- cout << 3 << " + " << r2 << " * " << r3 << " = " << res1 << endl;
- cout << "(" << 3 << " + " << r2 << ")" << " * " << r3 << " = " << res2 << endl;
- cout << 3 << " + " << r3 << " * " << "(" << r2 << " + " << 2 << ")" << "/"
- << "(" << r5 << " - " << r3 << ")" << " = " << res3 << endl;
- return 0;
- }
- // Rational.h
- #ifndef RATIONAL_H
- #define RATIONAL_H
- #include <iostream>
- using namespace std;
- class Rational
- {
- public:
- Rational(int numerator);
- Rational(int numerator, int denominator);
- ~Rational() {}
- Rational(const Rational& copy);
- Rational& operator=(const Rational& other);
- Rational& operator=(int i);
- int numerator() const { return numer; }
- int denominator() const { return denom; }
- Rational operator++() const;
- Rational operator--() const;
- bool operator==(const Rational& other) const;
- bool operator>=(const Rational& other) const;
- bool operator<=(const Rational& other) const;
- bool operator>(const Rational& other) const;
- bool operator<(const Rational& other) const;
- double value() const;
- private:
- int numer;
- int denom;
- void initialize(int numerator, int denominator);
- friend ostream& operator<<(std::ostream &, const Rational &);
- friend istream& operator>>(std::istream &, Rational &);
- friend int nwd(int, int);
- 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&);
- };
- #endif // RATIONAL_H
- // Rational.cpp
- #include "Rational.h"
- Rational::Rational(int numerator) : numer(numerator), denom(1)
- {
- }
- Rational::Rational(int numerator, int denominator) : numer(numerator), denom(denominator)
- {
- }
- Rational::Rational(const Rational& copy) : numer(copy.numer), denom(copy.denom)
- {
- }
- Rational& Rational::operator=(const Rational& other)
- {
- numer = other.numer;
- denom = other.denom;
- }
- Rational& Rational::operator=(int i)
- {
- numer = i;
- denom = 1;
- }
- int nwd(int a, int b)
- {
- if(a * b == 0)
- return 0;
- a = abs(a);
- b = abs(b);
- while(a != b)
- {
- if(a > b)
- a -= b;
- if(b > a)
- b -= a;
- }
- return a;
- }
- Rational Rational::operator++() const
- {
- int a = numer + denom;
- Rational product(a, denom);
- return product;
- }
- Rational Rational::operator--() const
- {
- int a = numer - denom;
- Rational product(a, denom);
- return product;
- }
- bool Rational::operator==(const Rational& other) const
- {
- if(value() == other.value())
- return true;
- return false;
- }
- bool Rational::operator>=(const Rational& other) const
- {
- if(value() >= other.value())
- return true;
- return false;
- }
- bool Rational::operator<=(const Rational& other) const
- {
- if(value() <= other.value())
- return true;
- return false;
- }
- bool Rational::operator>(const Rational& other) const
- {
- if(value() > other.value())
- return true;
- return false;
- }
- bool Rational::operator<(const Rational& other) const
- {
- if(value() < other.value())
- return true;
- return false;
- }
- double Rational::value() const
- {
- return 1.0 * numer / denom;
- }
- void Rational::initialize(int numerator, int denominator)
- {
- numer = numerator;
- denom = denominator;
- }
- ostream& operator<<(std::ostream & str, const Rational & ob)
- {
- if(ob.denom == 1)
- {
- str << ob.numer;
- return str;
- }
- if(ob.denom == -1)
- {
- str << "(-" << abs(ob.numer) << ")";
- return str;
- }
- if(ob.numer * ob.denom < 0)
- str << "(-" << abs(ob.numer) << "/" << abs(ob.denom) << ")";
- else
- str << abs(ob.numer) << "/" << abs(ob.denom);
- return str;
- }
- istream& operator>>(std::istream & str, Rational & ob)
- {
- str >> ob.numer >> ob.denom;
- return str;
- }
- Rational operator+(const Rational& A, const Rational& B)
- {
- int a = A.numer * B.denom + B.numer * A.denom;
- int b = A.denom * B.denom;
- int n = nwd(a,b);
- if(n == 0)
- {
- Rational product(0);
- return product;
- }
- a /= n;
- b /= n;
- Rational product(a,b);
- return product;
- }
- Rational operator-(const Rational& A, const Rational& B)
- {
- int a = A.numer * B.denom - B.numer * A.denom;
- int b = A.denom * B.denom;
- int n = nwd(a,b);
- if(n == 0)
- {
- Rational product(0);
- return product;
- }
- a /= n;
- b /= n;
- Rational product(a,b);
- return product;
- }
- Rational operator*(const Rational& A, const Rational& B)
- {
- int a = A.numer * B.numer;
- if(a == 0)
- {
- Rational product(0,1);
- return product;
- }
- int b = A.denom * B.denom;
- int n = nwd(a,b);
- a /= n;
- b /= n;
- Rational product(a,b);
- return product;
- }
- Rational operator/(const Rational& A, const Rational& B)
- {
- int a = A.numer * B.denom;
- if(a == 0)
- {
- Rational product(0,1);
- return product;
- }
- int b = A.denom * B.numer;
- int n = nwd(a,b);
- a /= n;
- b /= n;
- Rational product(a,b);
- return product;
- }
Advertisement
Add Comment
Please, Sign In to add comment