Advertisement
Guest User

dota 3

a guest
Nov 20th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. #include "fraction.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. fraction fraction::reducedForm()
  7. {
  8. int num1, denom1, gcd;
  9. bool fully_reduced = false;
  10. fraction reducedFrac;
  11.  
  12. num1 = numerator;
  13. denom1 = denominator;
  14.  
  15. while (fully_reduced == false)
  16. {
  17. gcd = get_gcd(num1, denom1);
  18. if (gcd == 1) {
  19. break;
  20. }
  21. else {
  22. num1 = num1 / gcd;
  23. denom1 = denom1 / gcd;
  24. }
  25. }
  26. return reducedFrac;
  27. }
  28.  
  29. int fraction::get_gcd(int num1, int num2)
  30. {
  31. int gcd, remainder;
  32.  
  33. while (num2 != 0)
  34. {
  35. remainder = num1 % num2;
  36. num1 = num2;
  37. num2 = remainder;
  38. }
  39.  
  40. gcd = num1;
  41.  
  42. return gcd;
  43. }
  44.  
  45. bool fraction::isGreaterThan(fraction otherFraction) const
  46. {
  47. fraction f1;
  48. fraction f2;
  49. fraction result;
  50.  
  51. f1.numerator = numerator * otherFraction.denominator;
  52. f1.denominator = denominator * otherFraction.denominator;
  53. f2.numerator = otherFraction.numerator * denominator;
  54. f2.denominator = otherFraction.denominator * denominator;
  55.  
  56. if (f1.numerator > f2.numerator)
  57. return true;
  58. else
  59. return false;
  60. }
  61. bool fraction::isEqualTo(fraction otherFraction) const
  62. {
  63. fraction f1;
  64. fraction f2;
  65. fraction result;
  66.  
  67. f1.numerator = numerator * otherFraction.denominator;
  68. f1.denominator = denominator * otherFraction.denominator;
  69. f2.numerator = otherFraction.numerator * denominator;
  70. f2.denominator = otherFraction.denominator * denominator;
  71.  
  72. if (f1.numerator == f2.numerator)
  73. return true;
  74. else
  75. return false;
  76. }
  77. fraction fraction::AddedTo(fraction otherFraction) const
  78. {
  79. fraction f1;
  80. fraction f2;
  81. fraction result;
  82.  
  83. f1.numerator = numerator * otherFraction.denominator;
  84. f1.denominator = denominator * otherFraction.denominator;
  85. f2.numerator = otherFraction.numerator * denominator;
  86. f2.denominator = otherFraction.denominator * denominator;
  87.  
  88. result.numerator = f1.numerator + f2.numerator;
  89. result.denominator = f1.denominator;
  90.  
  91. return result.reducedForm();
  92. }
  93. fraction fraction::Subtract(fraction otherFraction) const
  94. {
  95. fraction f1;
  96. fraction f2;
  97. fraction result;
  98.  
  99. f1.numerator = numerator * otherFraction.denominator;
  100. f1.denominator = denominator * otherFraction.denominator;
  101. f2.numerator = otherFraction.numerator * denominator;
  102. f2.denominator = otherFraction.denominator * denominator;
  103.  
  104. result.numerator = f1.numerator - f2.numerator;
  105. result.denominator = f1.denominator;
  106.  
  107. return result.reducedForm();
  108. }
  109. fraction fraction::MultipliedBy(fraction otherFraction) const
  110. {
  111. fraction result;
  112.  
  113. result.numerator = numerator * otherFraction.numerator;
  114. result.denominator = denominator * otherFraction.denominator;
  115.  
  116. return result.reducedForm();
  117. }
  118. fraction fraction::DividedBy(fraction otherFraction) const
  119. {
  120. fraction result;
  121.  
  122. result.numerator = numerator * otherFraction.denominator;
  123. result.denominator = denominator * otherFraction.numerator;
  124.  
  125. return result.reducedForm();
  126. }
  127. void fraction::print() const
  128. {
  129. cout << numerator << "/" << denominator;
  130. }
  131. fraction::fraction(int num, int denom)
  132. {
  133. numerator = num;
  134. denominator = denom;
  135. }
  136. fraction::fraction()
  137. {
  138. numerator = 0;
  139. denominator = 1;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement