Advertisement
SmnVadik

Lab 4.3 (Java)

Mar 14th, 2023 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.85 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.*;
  3. public class Main {
  4.     private static final Scanner scanner = new Scanner(System.in);
  5.     private static Scanner fileScanner;
  6.     private static final int MAX_N = 12;
  7.     private static final int MAX_M = 3;
  8.     private static final int MIN = 0;
  9.     public static void main(String[] args) throws java.io.FileNotFoundException, IOException {
  10.         control();
  11.         scanner.close();
  12.     }
  13.  
  14.     static void control () throws java.io.FileNotFoundException, IOException {
  15.         int m = 0;
  16.         int n = 0;
  17.         int res;
  18.         String path;
  19.         info();
  20.         if (selectToOpen() == 1) {
  21.             path = getPath();
  22.             fileScanner = new Scanner(new File(path));
  23.             m = getFirstDataFromFile(m, path);
  24.             n = getSecondDataFromFile(n, path);
  25.             fileScanner.close();
  26.         }
  27.         else {
  28.             m = inputM(m);
  29.             n = inputN(n);
  30.         }
  31.         res = recurs(m, n);
  32.         outputToConsole(res);
  33.         if (selectToSave() == 1) {
  34.             path = getPath();
  35.             outputToFile(res, path);
  36.         }
  37.     }
  38.  
  39.     static void info() {
  40.         System.out.println("Разработать рекурсивную функцию нахождения значения функции Аккермана");
  41.     }
  42.  
  43.     static int selectToOpen () {
  44.         int choice = 0;
  45.         boolean isCorrect;
  46.         do {
  47.             System.out.println("Если вы хотите считать данные с файла - введите 1, с консоли - введите 0");  // If you want to read data from a file - enter 1, from the console - enter 0
  48.             isCorrect = true;
  49.             try {
  50.                 choice = Integer.parseInt(scanner.nextLine());
  51.             } catch (Exception e) {
  52.                 isCorrect = false;
  53.                 System.out.println("Проверьте корректность введенных данных"); //check the correctness of the entered data
  54.             }
  55.             if (isCorrect && (choice != 1 && choice != 0)) {
  56.                 isCorrect = false;
  57.                 System.out.println("Введите 1 или 0"); //Enter 1 or 0
  58.             }
  59.         } while (!isCorrect);
  60.         return choice;
  61.     }
  62.  
  63.     static int selectToSave () {
  64.         int choice = 0;
  65.         boolean isCorrect = true;
  66.         System.out.println("Если вы хотите сохранить данные в файл - введите 1, нет - введите 0"); //If you want to save the data to a file - enter 1, if not - enter 0
  67.         do {
  68.             try {
  69.                 choice = Integer.parseInt(scanner.nextLine());
  70.             } catch (Exception e) {
  71.                 isCorrect = false;
  72.                 System.out.println("Проверьте корректность введенных данных"); //check the correctness of the entered data
  73.             }
  74.             if (isCorrect && (choice != 1 && choice != 0)) {
  75.                 isCorrect = false;
  76.                 System.out.println("Введите 1 или 0");  //Enter 1 or 0
  77.             }
  78.         } while (!isCorrect);
  79.         return choice;
  80.     }
  81.     static int inputM(int a) {
  82.         boolean isCorrect;
  83.         System.out.println("Введите значение 'm'"); //Enter the number 'm'
  84.         do {
  85.             try {
  86.                 isCorrect = true;
  87.                 a = Integer.parseInt(scanner.nextLine());
  88.             } catch (Exception e) {
  89.                 isCorrect = false;
  90.                 System.out.println("Проверьте корректность введенных данных");
  91.             }
  92.             if (a < MIN || a > MAX_M) {
  93.                 isCorrect = false;
  94.                 System.out.println("Число должно находиться в диапазоне [" + Integer.toString(MIN) + "; " + Integer.toString(MAX_M) + "]"); //The number must be in the range
  95.             }
  96.         } while (!isCorrect);
  97.         return a;
  98.     }
  99.  
  100.     static int inputN(int a) {
  101.         boolean isCorrect;
  102.         System.out.println("Введите значение 'n'"); //Enter the number
  103.         do {
  104.             isCorrect = true;
  105.             try {
  106.                 a = Integer.parseInt(scanner.nextLine());
  107.             } catch (Exception e) {
  108.                 isCorrect = false;
  109.                 System.out.println("Проверьте корректность введенных данных"); //check the correctness of the entered data
  110.             }
  111.             if (a < MIN || a > MAX_N) {
  112.                 isCorrect = false;
  113.                 System.out.println("Число должно находиться в диапазоне [" + Integer.toString(MIN) + "; " + Integer.toString(MAX_N) + "]");
  114.             }
  115.         } while (!isCorrect);
  116.         return a;
  117.     }
  118.  
  119.     static int recurs(int m, int n) {
  120.         if (m == 0)
  121.             return n + 1;
  122.         else
  123.             if (n == 0 && m > 0)
  124.                 return recurs(m - 1, 1);
  125.             else
  126.                 return recurs(m - 1, recurs(m, n - 1));
  127.     }
  128.  
  129.     static void outputToConsole(int res) {
  130.         System.out.println("Значение функции = " + res); //Function value
  131.     }
  132.  
  133.     static void outputToFile (int res, String path) throws IOException{
  134.         FileWriter writer = new FileWriter(path);
  135.         writer.write("Значение функции = " + res);
  136.         writer.close();
  137.  
  138.         System.out.println("\n" + "Данные сохранены успешно"); //The data was saved successfully
  139.     }
  140.  
  141.     static String getPath () {
  142.         boolean isCorrect;
  143.         String path;
  144.         do {
  145.             isCorrect = true;
  146.             System.out.println("Введите полный путь к файлу"); //enter the full path to the file
  147.             path = scanner.nextLine();
  148.             File file = new File(path);
  149.             if (!file.exists()) {
  150.                 isCorrect = false;
  151.                 System.out.println("Проверьте корректность введенной директории"); //check the correctness of the entered directory
  152.             }
  153.         } while (!isCorrect);
  154.         return path;
  155.     }
  156.  
  157.     static int getFirstDataFromFile (int dig, String path) {
  158.         boolean isCorrect;
  159.         do {
  160.             isCorrect = true;
  161.             try {
  162.                 dig = Integer.parseInt(fileScanner.next());
  163.             } catch (Exception e) {
  164.                 isCorrect = false;
  165.                 System.out.println("Проверьте корректность данных в файле"); //check the correctness of the data in the file
  166.                 path = getPath();
  167.             }
  168.             if (isCorrect && (dig < MIN || dig > MAX_M)) {
  169.                 isCorrect = false;
  170.                 System.out.println("Проверьте корректность данных в файле"); //"check the correctness of the data in the file
  171.                 path = getPath();
  172.             }
  173.         } while (!isCorrect);
  174.         return dig;
  175.     }
  176.  
  177.     static int getSecondDataFromFile (int dig, String path) {
  178.         boolean isCorrect;
  179.         do {
  180.             isCorrect = true;
  181.             try {
  182.                 dig = Integer.parseInt(fileScanner.next());
  183.             } catch (Exception e) {
  184.                 isCorrect = false;
  185.                 System.out.println("Проверьте корректность данных в файле"); //check the correctness of the data in the file
  186.                 path = getPath();
  187.             }
  188.             if (isCorrect && (dig < MIN || dig > MAX_N)) {
  189.                 isCorrect = false;
  190.                 System.out.println("Проверьте корректность данных в файле"); //check the correctness of the data in the file
  191.                 path = getPath();
  192.             }
  193.         } while (!isCorrect);
  194.         return dig;
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement