DasShelmer

8.1.5(1-4)

Mar 28th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. long EuclidRecursive(long a, long b) {
  6.  
  7.     if (a == b) {
  8.         return a;
  9.     }
  10.     if (a > b) {
  11.         long tmp = a;
  12.         a = b;
  13.         b = tmp;
  14.     }
  15.     return EuclidRecursive(a, b - a);
  16. }
  17.  
  18. long EuclidIterated(long a, long b) {
  19.     long tmp;
  20.     while (a != b) {
  21.         if (a > b) {
  22.             tmp = a;
  23.             a = b;
  24.             b = tmp;
  25.         }
  26.         b = b - a;
  27.     }
  28.     return a;
  29. }
  30.  
  31. void reduceFraction(long& a, long& b, long nod = 0) {
  32.     if (!nod)
  33.         nod = EuclidIterated(a, b);
  34.  
  35.     a = a / nod;
  36.     b = b / nod;
  37. }
  38.  
  39. long nok(long a, long b, long nod = 0) {
  40.     if (!nod)
  41.         nod = EuclidIterated(a, b);
  42.  
  43.     return a / nod * b;
  44. }
  45.  
  46. void solveFractionPlus(long& a, long& b, long c, long d) {
  47.     long nodBC = EuclidIterated(b, c);
  48.     long nokBC = nok(b, c, nodBC);
  49.  
  50.     long mult1 = nokBC / b, mult2 = nokBC / c;
  51.  
  52.     a = a * mult1 + d * mult2;
  53.     b = nokBC;
  54.  
  55.     reduceFraction(a, b);
  56. }
  57.  
  58.  
  59. int main() {
  60.     cout << endl << " a     d ";
  61.     cout << endl << "--- + ---";
  62.     cout << endl << " b     c ";
  63.     cout << endl << "Enter values a, b, c, d: ";
  64.     long a, b, c, d;
  65.     cin >> a >> b >> c >> d;
  66.  
  67.     solveFractionPlus(a, b, c, d);
  68.  
  69.     cout << " " << a << endl;
  70.     cout << "-----" << endl;
  71.     cout << " " << b;
  72.  
  73. }
Add Comment
Please, Sign In to add comment