Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1.  
  2. #include "rat.h"
  3. #include <sstream>
  4.  
  5. // These functions can be useful for calculations with fractions
  6.  
  7. rat::rat() : num(0),den(1)
  8. {
  9. }
  10.  
  11. rat::rat(int n):num(n),den(1)
  12. {
  13. }
  14.  
  15. rat::rat(int n, int d):num(n),den(d)
  16. {
  17. if(den==0)
  18. {
  19. num=0;
  20. den=1;
  21.  
  22. }
  23. reduce();
  24. }
  25.  
  26. int gcd(int x, int y)
  27. {
  28. if (y == 0)
  29. return x;
  30. return gcd(y, x % y);
  31. }
  32.  
  33. int lcm(int x, int y)
  34. {
  35. return (x * y) / gcd(x, y);
  36. }
  37.  
  38.  
  39. int rat::getNum() const
  40. {
  41. return num;
  42. }
  43.  
  44. int rat::getDen() const
  45. {
  46. return den;
  47. }
  48.  
  49. void rat::reduce()
  50. {
  51. int g = gcd(num, den);
  52. num /= g;
  53. den /= g;
  54. if(den < 0)
  55. {
  56. num = -num;
  57. den = -den;
  58. }
  59. }
  60.  
  61. rat rat::reciprocal() const
  62. {
  63.  
  64. rat result(den,num);
  65. return result;
  66. }
  67.  
  68. rat rat::operator+(const rat& right) const
  69. {
  70. rat result;
  71. result.den = den * right.den;
  72. result.num = (num * right.den) + den * right.num;
  73. result.reduce();
  74. return result;
  75. }
  76.  
  77. rat rat::operator-(const rat& right) const
  78. {
  79. rat result;
  80. result.den = den * right.den;
  81. result.num = (num * right.den) - den * right.num;
  82. result.reduce();
  83. return result;
  84. }
  85.  
  86. rat rat::operator*(const rat& right) const
  87. {
  88. rat result;
  89. result.den = den * right.den;
  90. result.num = (num * right.num);
  91. result.reduce();
  92. return result;
  93. }
  94.  
  95. rat rat::operator/(const rat& right) const
  96. {
  97. rat result;
  98. result.den = den * right.num;
  99. result.num = num*right.den;
  100. result.reduce();
  101. return result;
  102. }
  103.  
  104.  
  105. bool rat::operator==(const rat& right) const
  106. {
  107. rat r = (*this) - right;
  108. return r.num == 0 && r.den ==1;
  109.  
  110.  
  111. }
  112.  
  113. bool rat::operator!=(const rat& right) const
  114. {
  115. rat r = (*this) - right;
  116. return !((*this)==right);
  117.  
  118.  
  119. }
  120. bool rat::operator<(const rat& right) const
  121. {
  122. rat r = (*this) - right;
  123. r.reduce();
  124. return r.num<0;
  125.  
  126. }
  127. bool rat::operator>(const rat& right) const
  128. {
  129. rat r = (*this) - right;
  130. r.reduce();
  131. return r.num>0;
  132.  
  133. }
  134.  
  135. bool rat::operator<=(const rat& right) const
  136. {
  137.  
  138. return ((*this)<right || (*this) == right);
  139.  
  140. }
  141.  
  142. bool rat::operator>=(const rat& right) const
  143. {
  144.  
  145. return ((*this)>right || (*this) == right);
  146.  
  147. }
  148.  
  149. std::string rat::str() const
  150. {
  151. std::stringstream s;
  152. s<<num<<"/"<<den;
  153. return s.str();
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement