Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. /**
  8. * Gives the greatest common divisor of two integers or returns 0 if the
  9. * gcd is undefined. Uses the Euclidean GCD algorithm
  10. * @param a an integer
  11. * @param b an integer
  12. * @return the gcd of the specified parameter or 0 if both a and b are 0
  13. */
  14.  
  15. /**
  16. * Computes the sum of two fractions
  17. * @param n1 the numerator of the first fraction
  18. * @param d1 the denominator of the first fraction
  19. * @param n2 the numerator of the second fraction
  20. * @param d2 the denominator of the second fraction
  21. * @param num the numerator of the sum
  22. * @param den the denominator of the sum
  23. */
  24.  
  25. void add(int n1, int d1, int n2, int d2, int& num, int& den)
  26. {
  27. num = (n1 * d2 + n2 * d1) / gCD(n1 * d2 + n2 * d1, d1 * d2);
  28. den = (d1 * d2) / gCD(n1 * d2 + n2 * d1, d1 * d2);
  29. }
  30.  
  31. /**
  32. * Computes the difference of two fractions
  33. * @param n1 the numerator of the minuend
  34. * @param d1 the denominator of the minuend
  35. * @param n2 the numerator of the subtrahend
  36. * @param d2 the denominator of the subtrahend
  37. * @param num the numerator of the dufferebce
  38. * @param den the denominator of the difference
  39. */
  40. void sub(int n1, int d1, int n2, int d2, int& num, int& den)
  41. {
  42. num = (n1 * d2 - n2 * d1) / gCD(n1 * d2 - n2 * d1, d1 * d2);
  43. den = (d1 * d2) / gCD(n1 * d2 + n2 * d1, d1 * d2);
  44. }
  45.  
  46. /**
  47. * Computes the product of two fractions
  48. * @param n1 the numerator of the multiplier
  49. * @param d1 the denominator of the multiplier
  50. * @param n2 the numerator of the multiplicand
  51. * @param d2 the denominator of the multiplicand
  52. * @param num the numerator of the product
  53. * @param den the denominator of the product
  54. */
  55. void mult(int n1, int d1, int n2, int d2, int& num, int& den)
  56. {
  57. num = (n1 * n2 / gCD(n1 * n2, d2 * d1));
  58. den = (d1 * d2 / gCD(n1 * n2, d1 * d2));
  59. }
  60.  
  61. /**
  62. * Computes the quotient of two fractions, if defined
  63. * @param n1 the numerator of the dividend
  64. * @param d1 the denominator of the dividend
  65. * @param n2 the numerator of the divisor
  66. * @param d2 the denominator of the divisor
  67. * @param num the numerator of the quotient
  68. * @param den the numerator of the quotient
  69. * or 0 if the quotient is undefinded
  70. */
  71. void divd(int n1, int d1, int n2, int d2, int& num, int& den)
  72. {
  73. num = (n1 * d2) / gCD(n1 * d2, n2 * d1);
  74. den = (n2 * d1) / gCD(n1 * d2, n2 * d1);
  75. }
  76.  
  77. /**
  78. * Gives the string representation of a fraction
  79. * @param n the numerator of the fraction
  80. * @param d the denominator of the fraction
  81. * @param normalize gives a string represnetation
  82. * of a fraction in normalized form where the
  83. * numerator and denominator are relatively prime
  84. * and n/d when normalized is false.
  85. * @return n/d when normalized is false; otherwise,
  86. * 1. d = 0 -> nan
  87. * 2. n = 0 -> 0
  88. * 3. d = 1 -> n
  89. * 4. d = -1 -> -n
  90. * 5. n and d have the same sign -> |n|/|d|
  91. * 6. n and d have different signs -> -|n|/|d|
  92. */
  93. string str(int n, int d, bool normalize)
  94. {
  95. if (normalize == false)
  96. return to_string(n) + "/" + to_string(d);
  97. else if (d == 0)
  98. return "nan";
  99. else if (n == 0)
  100. return "0";
  101. else if (d == 1)
  102. return to_string(n);
  103. else if (d == -1)
  104. return "-" + to_string(n);
  105. else if ((d < 0 && n < 0) || (d > 0 && n > 0))
  106. return to_string(abs(n)) + "/" + to_string(abs(d));
  107. else if ((d < 0 && n > 0) || (d > 0 && n < 0))
  108. return "-" + to_string(abs(n)) + "/" + to_string(abs(d));
  109. }
  110.  
  111. int gCD(int a, int b)
  112. {
  113. a = abs(a);
  114. b = abs(b);
  115. if (a == 0 && b == 0)
  116. return 0;
  117. else if (a == 0 || b == 0)
  118. return abs(a + b);
  119. else
  120. {
  121. int r = a % b;
  122. while (r != 0)
  123. {
  124. a = b;
  125. b = r;
  126. r = a % b;
  127. }
  128. return b;
  129. }
  130. }
  131.  
  132. int main()
  133. {
  134. int f1n, f1d, f2n, f2d, f3n, f3d, numerator, denominator, addCacheNum, addCacheDen, multiplyCacheNum, multiplyCacheDen, divideCacheNum, divideCacheDen;
  135. cout << "Enter the numerator and denominator of fraction 1 -> ";
  136. cin >> f1n >> f1d;
  137. cout << "Enter the numerator and denominator of fraction 2 -> ";
  138. cin >> f2n >> f2d;
  139. cout << "Enter the numerator and denominator of fractoin 3 -> ";
  140. cin >> f3n >> f3d;
  141. if (f1d == 0 || f2d == 0 || f3d == 0)
  142. cout << endl << "ERROR: The denominator of a fraction cannot be equal to 0." << endl;
  143. else
  144. {
  145. cout << endl << "F1 = " << str(f1n, f1d, false) << endl;
  146. cout << "F2 = " << str(f2n, f2d, false) << endl;
  147. cout << "F3 = " << str(f3n, f3d, false) << endl << endl;
  148.  
  149. add(f1n, f1d, f2n, f2d, numerator, denominator);
  150. cout << "F1 + F2 = " << str(numerator, denominator, true) << endl;
  151.  
  152. sub(f1n, f1d, f2n, f2d, numerator, denominator);
  153. cout << "F1 - F2 = " << str(numerator, denominator, true) << endl;
  154.  
  155. mult(f1n, f1d, f2n, f2d, numerator, denominator);
  156. cout << "F1 x F2 = " << str(numerator, denominator, true) << endl;
  157.  
  158. if (gCD(f1n * f2d, f1d * f2n) == 0)
  159. cout << "F1 / F2 = nan" << endl;
  160. else
  161. {
  162. divd(f1n, f1d, f2n, f2d, numerator, denominator);
  163. cout << "F1 / F2 = " << str(numerator, denominator, true) << endl;
  164. }
  165.  
  166. if (gCD(f1n * f2d, f1d * f2n) == 0)
  167. cout << "F1 x (F2 + F3) / (F1 + F3) = nan";
  168. else
  169. {
  170. add(f2n, f2d, f3n, f3d, addCacheNum, addCacheDen);
  171.  
  172. mult(f1n, f1d, addCacheNum, addCacheDen, multiplyCacheNum, multiplyCacheDen);
  173.  
  174. add(f1n, f1d, f3n, f3d, addCacheNum, addCacheDen);
  175.  
  176. divd(multiplyCacheNum, multiplyCacheDen, addCacheNum, addCacheDen, divideCacheNum, divideCacheDen);
  177.  
  178. cout << "F1 x (F2 + F3) / (F1 + F3) = " << str(divideCacheNum, divideCacheDen, true) << endl;
  179. }
  180. }
  181. return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement