Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class RationalNumber
- {
- /*
- This class is designed to represent Rational Number objects.
- A rational number is a number of the form a/b with integers
- a and b such that b is different from 0.
- */
- private:
- int a, b;
- public:
- //Constructors
- RationalNumber();
- RationalNumber(const int&, const int&);
- //Getters
- int getNumerator() const;
- int getDenominator() const;
- //Setters
- void setNumerator(const int &num);
- void setDenominator(const int &denom);
- //Additional member functions
- double toDouble() const;
- void standardize();
- void reduce();
- void print() const;
- bool isEqual(const RationalNumber &r ) const;
- RationalNumber operator+( const RationalNumber &r) const;
- RationalNumber operator+( const int &x) const;
- friend RationalNumber operator+ ( const int&x, const RationalNumber &r);
- };
- //Constructors
- RationalNumber::RationalNumber()
- {
- //As a default object, let us construct 0/1 rational number
- a = 0;
- b = 1;
- }
- RationalNumber::RationalNumber(const int &num,const int &denom)
- {
- //If the denominator parameter is 0, ignore it and use 1
- a = num;
- b = denom != 0 ? denom : 1;
- //Now that the object is created, standardize and reduce it
- standardize();
- reduce();
- }
- //Getters
- int RationalNumber::getNumerator() const
- {
- return a;
- }
- int RationalNumber::getDenominator() const
- {
- return b;
- }
- //Setters
- void RationalNumber::setNumerator(const int &num)
- {
- a = num;
- //Now that numerator of an existing object is modified,
- //standardize it and reduce it
- standardize();
- reduce();
- }
- void RationalNumber::setDenominator(const int &denom)
- {
- //If the denominator parameter is 0, ignore it and use 1
- b = denom != 0 ? denom : 1;
- //Now that denominator of an existing object is modified,
- //standardize it and reduce it
- standardize();
- reduce();
- }
- //Additional member functions
- double RationalNumber::toDouble() const
- {
- return static_cast<double>(a)/b;
- }
- void RationalNumber::standardize()
- {
- if (b < 0)
- {
- a *= -1;
- b *= -1;
- }
- }
- void RationalNumber::reduce()
- {
- if (a == 0)
- {
- b = 1;
- return;
- }
- else
- {
- //Remeber that the denominator is NEVER zero by design
- //Therefore here both numerator and denominator are non-zero.
- int m = abs(a);
- int n = abs(b);
- int gcd = m < n ? m : n;
- while (gcd > 0)
- {
- if (m % gcd == 0 && n % gcd == 0)
- break;
- gcd--;
- }
- a /= gcd;
- b /= gcd;
- }
- }
- void RationalNumber::print() const
- {
- cout << a << "/" << b;
- }
- bool RationalNumber::isEqual (const RationalNumber &r)const
- {
- int x=r.a;
- int y=r.getDenominator();
- if(a*y==b*x)
- return true;
- else
- return false;
- }
- RationalNumber RationalNumber::operator+ ( const RationalNumber &r)const
- {
- int a1=this->a;
- int b1=this->b;
- int a2=r.a;
- int b2=r.b;
- // we would like to add(a1/b1) + (a2/b2) which is equal to (a1 b2+a2 b1)/b1b2
- RationalNumber answer(a1*b2+a2*b1,b1*b2);
- answer.standardize();
- answer.reduce();
- return answer;
- }
- RationalNumber RationalNumber::operator+( const int &x)const
- {
- RationalNumber temp(x,1);
- return *this+temp;
- }
- RationalNumber operator+( const int &x, const RationalNumber &r)
- {
- RationalNumber temp(r.a,r.b);
- return temp+x;
- }
- int main()
- {
- //Test constructors
- RationalNumber *r1=new RationalNumber(2,3);
- RationalNumber r2=5+ *r1;
- cout<<" r1= ";r1->print();cout<<endl;
- cout<<" r2= ";r2.print();cout<<endl;
- system("Pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement