Advertisement
venik2405

lab2_2_0

Oct 28th, 2020
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int inputVariables() {
  5.     int name;
  6.     cin >> name;
  7.     return name;
  8. }
  9.  
  10. int FindNumerator(int m, int n, int p, int q) {
  11.     int a;
  12.     a = ((m * q) + (p * n));
  13.     return a;
  14. }
  15.  
  16. int FindDenumerator(int n, int q) {
  17.     int b = 0;
  18.     b = n * q;
  19.     return b;
  20. }
  21.  
  22. int ReduceFunction(int c, int d, int x) {
  23.     do {
  24.         if (c > d) {
  25.             c = (c - d);
  26.         }
  27.         else
  28.             d = (d - c);
  29.     } while (c != d);
  30.     if (x == 1) {
  31.         return c;
  32.     }
  33.     if (x == 2) {
  34.         return d;
  35.     }
  36.     return ;
  37. }
  38.  
  39. static void PrintFraction(int a, int b, int c, int d) {
  40.     cout << (a / c) << "/" << (b / d);
  41. }
  42.  
  43.  
  44. int main()
  45. {
  46.     setlocale(LC_ALL, "Russian");
  47.     cout << "Данная программа находит сумму двух несократимых дробей." << endl;
  48.     cout << "Введите дроби" << endl;
  49.     int m = inputVariables();
  50.     int n = inputVariables();
  51.     int p = inputVariables();
  52.     int q = inputVariables();
  53.     int a = FindNumerator(m, n, p, q);
  54.     int b = FindDenumerator(n, q);
  55.     int c = a;
  56.     int d = b;
  57.     c = ReduceFunction(c, d, 1);
  58.     d = ReduceFunction(c, d, 2);
  59.     cout << "Сумма равна:" << endl;
  60.     PrintFraction(a, b, c, d);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement