Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.io.*;
- public class Main {
- private static final Scanner scanner = new Scanner(System.in);
- private static Scanner fileScanner;
- private static final int MAX_N = 12;
- private static final int MAX_M = 3;
- private static final int MIN = 0;
- public static void main(String[] args) throws java.io.FileNotFoundException, IOException {
- control();
- scanner.close();
- }
- static void control () throws java.io.FileNotFoundException, IOException {
- int m = 0;
- int n = 0;
- int res;
- String path;
- info();
- if (selectToOpen() == 1) {
- path = getPath();
- fileScanner = new Scanner(new File(path));
- m = getFirstDataFromFile(m, path);
- n = getSecondDataFromFile(n, path);
- fileScanner.close();
- }
- else {
- m = inputM(m);
- n = inputN(n);
- }
- res = recurs(m, n);
- outputToConsole(res);
- if (selectToSave() == 1) {
- path = getPath();
- outputToFile(res, path);
- }
- }
- static void info() {
- System.out.println("Разработать рекурсивную функцию нахождения значения функции Аккермана");
- }
- static int selectToOpen () {
- int choice = 0;
- boolean isCorrect;
- do {
- System.out.println("Если вы хотите считать данные с файла - введите 1, с консоли - введите 0"); // If you want to read data from a file - enter 1, from the console - enter 0
- isCorrect = true;
- try {
- choice = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isCorrect = false;
- System.out.println("Проверьте корректность введенных данных"); //check the correctness of the entered data
- }
- if (isCorrect && (choice != 1 && choice != 0)) {
- isCorrect = false;
- System.out.println("Введите 1 или 0"); //Enter 1 or 0
- }
- } while (!isCorrect);
- return choice;
- }
- static int selectToSave () {
- int choice = 0;
- boolean isCorrect = true;
- System.out.println("Если вы хотите сохранить данные в файл - введите 1, нет - введите 0"); //If you want to save the data to a file - enter 1, if not - enter 0
- do {
- try {
- choice = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isCorrect = false;
- System.out.println("Проверьте корректность введенных данных"); //check the correctness of the entered data
- }
- if (isCorrect && (choice != 1 && choice != 0)) {
- isCorrect = false;
- System.out.println("Введите 1 или 0"); //Enter 1 or 0
- }
- } while (!isCorrect);
- return choice;
- }
- static int inputM(int a) {
- boolean isCorrect;
- System.out.println("Введите значение 'm'"); //Enter the number 'm'
- do {
- try {
- isCorrect = true;
- a = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isCorrect = false;
- System.out.println("Проверьте корректность введенных данных");
- }
- if (a < MIN || a > MAX_M) {
- isCorrect = false;
- System.out.println("Число должно находиться в диапазоне [" + Integer.toString(MIN) + "; " + Integer.toString(MAX_M) + "]"); //The number must be in the range
- }
- } while (!isCorrect);
- return a;
- }
- static int inputN(int a) {
- boolean isCorrect;
- System.out.println("Введите значение 'n'"); //Enter the number
- do {
- isCorrect = true;
- try {
- a = Integer.parseInt(scanner.nextLine());
- } catch (Exception e) {
- isCorrect = false;
- System.out.println("Проверьте корректность введенных данных"); //check the correctness of the entered data
- }
- if (a < MIN || a > MAX_N) {
- isCorrect = false;
- System.out.println("Число должно находиться в диапазоне [" + Integer.toString(MIN) + "; " + Integer.toString(MAX_N) + "]");
- }
- } while (!isCorrect);
- return a;
- }
- static int recurs(int m, int n) {
- if (m == 0)
- return n + 1;
- else
- if (n == 0 && m > 0)
- return recurs(m - 1, 1);
- else
- return recurs(m - 1, recurs(m, n - 1));
- }
- static void outputToConsole(int res) {
- System.out.println("Значение функции = " + res); //Function value
- }
- static void outputToFile (int res, String path) throws IOException{
- FileWriter writer = new FileWriter(path);
- writer.write("Значение функции = " + res);
- writer.close();
- System.out.println("\n" + "Данные сохранены успешно"); //The data was saved successfully
- }
- static String getPath () {
- boolean isCorrect;
- String path;
- do {
- isCorrect = true;
- System.out.println("Введите полный путь к файлу"); //enter the full path to the file
- path = scanner.nextLine();
- File file = new File(path);
- if (!file.exists()) {
- isCorrect = false;
- System.out.println("Проверьте корректность введенной директории"); //check the correctness of the entered directory
- }
- } while (!isCorrect);
- return path;
- }
- static int getFirstDataFromFile (int dig, String path) {
- boolean isCorrect;
- do {
- isCorrect = true;
- try {
- dig = Integer.parseInt(fileScanner.next());
- } catch (Exception e) {
- isCorrect = false;
- System.out.println("Проверьте корректность данных в файле"); //check the correctness of the data in the file
- path = getPath();
- }
- if (isCorrect && (dig < MIN || dig > MAX_M)) {
- isCorrect = false;
- System.out.println("Проверьте корректность данных в файле"); //"check the correctness of the data in the file
- path = getPath();
- }
- } while (!isCorrect);
- return dig;
- }
- static int getSecondDataFromFile (int dig, String path) {
- boolean isCorrect;
- do {
- isCorrect = true;
- try {
- dig = Integer.parseInt(fileScanner.next());
- } catch (Exception e) {
- isCorrect = false;
- System.out.println("Проверьте корректность данных в файле"); //check the correctness of the data in the file
- path = getPath();
- }
- if (isCorrect && (dig < MIN || dig > MAX_N)) {
- isCorrect = false;
- System.out.println("Проверьте корректность данных в файле"); //check the correctness of the data in the file
- path = getPath();
- }
- } while (!isCorrect);
- return dig;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement