Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. class RationalNumber
  4. {
  5. /*
  6. This class is designed to represent Rational Number objects.
  7. A rational number is a number of the form a/b with integers
  8. a and b such that b is different from 0.
  9. */
  10. private:
  11. int a, b;
  12. public:
  13. //Constructors
  14. RationalNumber();
  15. RationalNumber(const int&, const int&);
  16.  
  17. //Getters
  18. int getNumerator() const;
  19. int getDenominator() const;
  20.  
  21. //Setters
  22. void setNumerator(const int &num);
  23. void setDenominator(const int &denom);
  24.  
  25. //Additional member functions
  26. double toDouble() const;
  27. void standardize();
  28. void reduce();
  29. void print() const;
  30. bool isEqual(const RationalNumber &r ) const;
  31. RationalNumber operator+( const RationalNumber &r) const;
  32. RationalNumber operator+( const int &x) const;
  33. friend RationalNumber operator+ ( const int&x, const RationalNumber &r);
  34. };
  35.  
  36. //Constructors
  37. RationalNumber::RationalNumber()
  38. {
  39. //As a default object, let us construct 0/1 rational number
  40. a = 0;
  41. b = 1;
  42. }
  43.  
  44. RationalNumber::RationalNumber(const int &num,const int &denom)
  45. {
  46. //If the denominator parameter is 0, ignore it and use 1
  47. a = num;
  48. b = denom != 0 ? denom : 1;
  49. //Now that the object is created, standardize and reduce it
  50. standardize();
  51. reduce();
  52. }
  53.  
  54. //Getters
  55. int RationalNumber::getNumerator() const
  56. {
  57. return a;
  58. }
  59. int RationalNumber::getDenominator() const
  60. {
  61. return b;
  62. }
  63.  
  64. //Setters
  65. void RationalNumber::setNumerator(const int &num)
  66. {
  67. a = num;
  68. //Now that numerator of an existing object is modified,
  69. //standardize it and reduce it
  70. standardize();
  71. reduce();
  72. }
  73. void RationalNumber::setDenominator(const int &denom)
  74. {
  75. //If the denominator parameter is 0, ignore it and use 1
  76. b = denom != 0 ? denom : 1;
  77. //Now that denominator of an existing object is modified,
  78. //standardize it and reduce it
  79. standardize();
  80. reduce();
  81. }
  82.  
  83. //Additional member functions
  84. double RationalNumber::toDouble() const
  85. {
  86. return static_cast<double>(a)/b;
  87. }
  88. void RationalNumber::standardize()
  89. {
  90. if (b < 0)
  91. {
  92. a *= -1;
  93. b *= -1;
  94. }
  95. }
  96. void RationalNumber::reduce()
  97. {
  98. if (a == 0)
  99. {
  100. b = 1;
  101. return;
  102. }
  103. else
  104. {
  105. //Remeber that the denominator is NEVER zero by design
  106. //Therefore here both numerator and denominator are non-zero.
  107. int m = abs(a);
  108. int n = abs(b);
  109. int gcd = m < n ? m : n;
  110. while (gcd > 0)
  111. {
  112. if (m % gcd == 0 && n % gcd == 0)
  113. break;
  114. gcd--;
  115. }
  116. a /= gcd;
  117. b /= gcd;
  118. }
  119. }
  120. void RationalNumber::print() const
  121. {
  122. cout << a << "/" << b;
  123. }
  124. bool RationalNumber::isEqual (const RationalNumber &r)const
  125. {
  126. int x=r.a;
  127. int y=r.getDenominator();
  128.  
  129. if(a*y==b*x)
  130. return true;
  131. else
  132. return false;
  133. }
  134. RationalNumber RationalNumber::operator+ ( const RationalNumber &r)const
  135. {
  136. int a1=this->a;
  137. int b1=this->b;
  138. int a2=r.a;
  139. int b2=r.b;
  140. // we would like to add(a1/b1) + (a2/b2) which is equal to (a1 b2+a2 b1)/b1b2
  141. RationalNumber answer(a1*b2+a2*b1,b1*b2);
  142. answer.standardize();
  143. answer.reduce();
  144. return answer;
  145. }
  146. RationalNumber RationalNumber::operator+( const int &x)const
  147. {
  148. RationalNumber temp(x,1);
  149. return *this+temp;
  150. }
  151. RationalNumber operator+( const int &x, const RationalNumber &r)
  152. {
  153. RationalNumber temp(r.a,r.b);
  154. return temp+x;
  155. }
  156. int main()
  157. {
  158. //Test constructors
  159. RationalNumber *r1=new RationalNumber(2,3);
  160. RationalNumber r2=5+ *r1;
  161.  
  162. cout<<" r1= ";r1->print();cout<<endl;
  163. cout<<" r2= ";r2.print();cout<<endl;
  164.  
  165.  
  166. system("Pause");
  167. return 0;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement