Advertisement
venik2405

LAB2_2_0

Oct 28th, 2020
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. import java.util.function.Predicate;
  4.  
  5. public class Main {
  6.  
  7.     public static int inputVariables(int x) {
  8.         boolean isNotCorrect;
  9.         int l = 0;
  10.         Scanner sc = new Scanner(System.in);
  11.         do {
  12.             isNotCorrect = false;
  13.             try {
  14.                 l = sc.nextInt();
  15.             } catch (Exception e) {
  16.                 System.out.println("Введите целое число");
  17.                 isNotCorrect = true;
  18.             }
  19.             if ((x == 2) & (l == 0)) {
  20.                 System.out.println("Знаменатель не может быть равен нулю.");
  21.                 isNotCorrect = true;
  22.             }
  23.         } while (isNotCorrect);
  24.         return l;
  25.     }
  26.  
  27.     public static int FindNumerator(int m, int n, int p, int q) {
  28.         int a;
  29.         a = (m * q) + (p * n);
  30.         return a;
  31.     }
  32.  
  33.     public static int FindDenumerator(int n, int q) {
  34.         int b;
  35.         b = n * q;
  36.         return b;
  37.     }
  38.  
  39.     public static int Temp1(int a) {
  40.         int c;
  41.         c = a;
  42.         return c;
  43.     }
  44.  
  45.     public static int Temp2(int b) {
  46.         int d;
  47.         d = b;
  48.         return d;
  49.     }
  50.  
  51.     public static int ReduceFunction(int c, int d, int x) {
  52.         do {
  53.             if (c > d) {
  54.                 c = (c - d);
  55.             } else
  56.                 d = (d - c);
  57.         } while (c != d);
  58.         if (x == 1) {
  59.             return c;
  60.         }
  61.         if (x == 2) {
  62.             return d;
  63.         }
  64.         return 1;
  65.     }
  66.  
  67.  
  68.     static void PrintFraction(int a, int b, int c, int d) {
  69.         System.out.println((a / c) + "/" + (b / d ));
  70.     }
  71.  
  72.     public static void main(String[] args) {
  73.         System.out.println("Данная программа находит сумму двух несократимых дробей.");
  74.         System.out.println("Введите дроби");
  75.         int m = inputVariables(1);
  76.         int n = inputVariables(2);
  77.         int p = inputVariables(1);
  78.         int q = inputVariables(2);
  79.         int a = FindNumerator(m, n, p, q);
  80.         int b = FindDenumerator(n, q);
  81.         int c = Temp1(a);
  82.         int d = Temp2(b);
  83.         c = ReduceFunction(c, d, 1);
  84.         d = ReduceFunction(c, d, 2);
  85.         System.out.println("Сумма равна:");
  86.         PrintFraction(a, b, c, d);
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement