Advertisement
Vanya_Shestakov

laba4.2 (Java)

Dec 5th, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.76 KB | None | 0 0
  1. package com.company;
  2. import java.io.*;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     public static Scanner sysScan = new Scanner(System.in);
  7.     static final int FILE = 1;
  8.     static final int CONSOLE = 2;
  9.  
  10.     public static void main(String[] args) {
  11.         System.out.println("""
  12.        The program calculates the value of the function f(n).
  13.        The function is defined on the set of natural numbers as follows:
  14.        
  15.        If n = 1, then f(n) = 1
  16.        
  17.                               n
  18.        If n >= 2, then f(n) = ∑ (n div i)
  19.                              i=2""");
  20.  
  21.         System.out.println("Choose way of input");
  22.         int wayOfInput = chooseWayOfInputOutput();
  23.         int argument = getArgument(wayOfInput);
  24.         int result = findValueOfFunction(argument, argument);
  25.         System.out.println("Choose way of output");
  26.         int wayOfOutput = chooseWayOfInputOutput();
  27.         outputResult(wayOfOutput, result);
  28.         sysScan.close();
  29.     }
  30.  
  31.     public static int chooseWayOfInputOutput() {
  32.         System.out.println("Enter 1 or 2:\n 1.File\n 2.Console");
  33.         Scanner scan = new Scanner(System.in);
  34.         int choice = 0;
  35.         boolean isIncorrect;
  36.         do {
  37.             isIncorrect = false;
  38.             try {
  39.                 choice = Integer.parseInt(scan.nextLine());
  40.             } catch (Exception e) {
  41.                 System.out.println("Enter an integer!");
  42.                 isIncorrect = true;
  43.             }
  44.             if (!isIncorrect && choice != FILE && choice != CONSOLE) {
  45.                 System.out.println("Enter 1 or 2!");
  46.                 isIncorrect = true;
  47.             }
  48.         } while (isIncorrect);
  49.         return choice;
  50.     }
  51.  
  52.     public static int getArgument(int way) {
  53.         int argument = 0;
  54.         switch (way) {
  55.             case FILE:
  56.                 System.out.println("Enter the absolute link to the input file");
  57.                 String pathOfInput = getPath();
  58.                 argument = getArgumentFromFile(pathOfInput);
  59.                 break;
  60.             case CONSOLE:
  61.                 argument = getArgumentFromConsole();
  62.                 break;
  63.         }
  64.         return argument;
  65.     }
  66.  
  67.     public static String getPath() {
  68.         Scanner scan = new Scanner(System.in);
  69.         boolean isIncorrect;
  70.         String path;
  71.         do {
  72.             isIncorrect = false;
  73.             path = scan.nextLine();
  74.             File file = new File(path);
  75.  
  76.             if (!file.exists()) {
  77.                 System.err.println("File not found!\nEnter the absolute link to the file");
  78.                 isIncorrect = true;
  79.             }
  80.         } while (isIncorrect);
  81.         System.out.println();
  82.         return path;
  83.     }
  84.  
  85.     public static int getArgumentFromFile(String path) {
  86.         System.out.println("Argument is reading from the file...");
  87.         File fileOfInput = new File(path);
  88.         String line;
  89.         int argument = 0;
  90.         boolean isIncorrect = false;
  91.         if (fileOfInput.length() != 0) {
  92.             try (BufferedReader reader = new BufferedReader(new FileReader(fileOfInput))) {
  93.                 line = reader.readLine();
  94.                 argument = Integer.parseInt(line);
  95.             } catch (IOException e) {
  96.                 System.out.println("Input/output error!\nEnter the argument from console");
  97.                 isIncorrect = true;
  98.             } catch (NumberFormatException e) {
  99.                 System.out.println("Incorrect data in the file!\nEnter the argument from console");
  100.                 isIncorrect = true;
  101.             }
  102.             if (!isIncorrect && argument < 1 || argument > 15000) {
  103.                 System.out.println("Incorrect data in the file!\nEnter the argument from console");
  104.                 isIncorrect = true;
  105.             }
  106.         } else {
  107.             System.out.println("File is empty!\nEnter the argument from console");
  108.             isIncorrect = true;
  109.         }
  110.         if (isIncorrect) {
  111.             argument = getArgumentFromConsole();
  112.         }
  113.         return argument;
  114.     }
  115.  
  116.     public static int getArgumentFromConsole() {
  117.         System.out.println("Enter the argument of the function:");
  118.         int argument = 0;
  119.         boolean isIncorrect;
  120.         do {
  121.             isIncorrect = false;
  122.             try {
  123.                 argument = Integer.parseInt(sysScan.nextLine());
  124.             } catch (NumberFormatException e) {
  125.                 System.err.println("Enter the integer number!");
  126.                 isIncorrect = true;
  127.             }
  128.             if (!isIncorrect && (argument < 1 || argument > 15000)) {
  129.                 System.err.println("Enter the number in range [0; 15000]!");
  130.                 isIncorrect = true;
  131.             }
  132.         } while (isIncorrect);
  133.         return argument;
  134.     }
  135.  
  136.     public static void outputResult(int way, int result) {
  137.         switch (way) {
  138.             case FILE:
  139.                 System.out.println("Enter the absolute link to the output file");
  140.                 String pathOfOutput = getPath();
  141.                 try {
  142.                     FileWriter writer = new FileWriter(pathOfOutput);
  143.                     writer.write("The value of the function: " + result);
  144.                     writer.close();
  145.                 } catch (IOException e) {
  146.                     System.err.println("I/O Error!");
  147.                 }
  148.                 break;
  149.             case CONSOLE:
  150.                 System.out.println("The value of the function: " + result);
  151.                 break;
  152.         }
  153.     }
  154.  
  155.     public static int findValueOfFunction(int n, int i) {
  156.         if (n != 1) {
  157.             if (i > 1) {
  158.                 return n/i + findValueOfFunction(n, i - 1 );
  159.             } else {
  160.                 return 0;
  161.             }
  162.         } else {
  163.             return 1;
  164.         }
  165.     }
  166. }
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement