Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.function.Predicate;
- public class Main {
- public static int inputVariables(int x) {
- boolean isNotCorrect;
- int l = 0;
- Scanner sc = new Scanner(System.in);
- do {
- isNotCorrect = false;
- try {
- l = sc.nextInt();
- } catch (Exception e) {
- System.out.println("Введите целое число");
- isNotCorrect = true;
- }
- if ((x == 2) & (l == 0)) {
- System.out.println("Знаменатель не может быть равен нулю.");
- isNotCorrect = true;
- }
- } while (isNotCorrect);
- return l;
- }
- public static int FindNumerator(int m, int n, int p, int q) {
- int a;
- a = (m * q) + (p * n);
- return a;
- }
- public static int FindDenumerator(int n, int q) {
- int b;
- b = n * q;
- return b;
- }
- public static int Temp1(int a) {
- int c;
- c = a;
- return c;
- }
- public static int Temp2(int b) {
- int d;
- d = b;
- return d;
- }
- public static int ReduceFunction(int c, int d, int x) {
- do {
- if (c > d) {
- c = (c - d);
- } else
- d = (d - c);
- } while (c != d);
- if (x == 1) {
- return c;
- }
- if (x == 2) {
- return d;
- }
- return 1;
- }
- static void PrintFraction(int a, int b, int c, int d) {
- System.out.println((a / c) + "/" + (b / d ));
- }
- public static void main(String[] args) {
- System.out.println("Данная программа находит сумму двух несократимых дробей.");
- System.out.println("Введите дроби");
- int m = inputVariables(1);
- int n = inputVariables(2);
- int p = inputVariables(1);
- int q = inputVariables(2);
- int a = FindNumerator(m, n, p, q);
- int b = FindDenumerator(n, q);
- int c = Temp1(a);
- int d = Temp2(b);
- c = ReduceFunction(c, d, 1);
- d = ReduceFunction(c, d, 2);
- System.out.println("Сумма равна:");
- PrintFraction(a, b, c, d);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement