Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.*;
- import java.util.Scanner;
- public class Main {
- public static Scanner sysScan = new Scanner(System.in);
- static final int FILE = 1;
- static final int CONSOLE = 2;
- public static void main(String[] args) {
- System.out.println("""
- The program calculates the value of the function f(n).
- The function is defined on the set of natural numbers as follows:
- If n = 1, then f(n) = 1
- n
- If n >= 2, then f(n) = ∑ (n div i)
- i=2""");
- System.out.println("Choose way of input");
- int wayOfInput = chooseWayOfInputOutput();
- int argument = getArgument(wayOfInput);
- int result = findValueOfFunction(argument, argument);
- System.out.println("Choose way of output");
- int wayOfOutput = chooseWayOfInputOutput();
- outputResult(wayOfOutput, result);
- sysScan.close();
- }
- public static int chooseWayOfInputOutput() {
- System.out.println("Enter 1 or 2:\n 1.File\n 2.Console");
- Scanner scan = new Scanner(System.in);
- int choice = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- choice = Integer.parseInt(scan.nextLine());
- } catch (Exception e) {
- System.out.println("Enter an integer!");
- isIncorrect = true;
- }
- if (!isIncorrect && choice != FILE && choice != CONSOLE) {
- System.out.println("Enter 1 or 2!");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return choice;
- }
- public static int getArgument(int way) {
- int argument = 0;
- switch (way) {
- case FILE:
- System.out.println("Enter the absolute link to the input file");
- String pathOfInput = getPath();
- argument = getArgumentFromFile(pathOfInput);
- break;
- case CONSOLE:
- argument = getArgumentFromConsole();
- break;
- }
- return argument;
- }
- public static String getPath() {
- Scanner scan = new Scanner(System.in);
- boolean isIncorrect;
- String path;
- do {
- isIncorrect = false;
- path = scan.nextLine();
- File file = new File(path);
- if (!file.exists()) {
- System.err.println("File not found!\nEnter the absolute link to the file");
- isIncorrect = true;
- }
- } while (isIncorrect);
- System.out.println();
- return path;
- }
- public static int getArgumentFromFile(String path) {
- System.out.println("Argument is reading from the file...");
- File fileOfInput = new File(path);
- String line;
- int argument = 0;
- boolean isIncorrect = false;
- if (fileOfInput.length() != 0) {
- try (BufferedReader reader = new BufferedReader(new FileReader(fileOfInput))) {
- line = reader.readLine();
- argument = Integer.parseInt(line);
- } catch (IOException e) {
- System.out.println("Input/output error!\nEnter the argument from console");
- isIncorrect = true;
- } catch (NumberFormatException e) {
- System.out.println("Incorrect data in the file!\nEnter the argument from console");
- isIncorrect = true;
- }
- if (!isIncorrect && argument < 1 || argument > 15000) {
- System.out.println("Incorrect data in the file!\nEnter the argument from console");
- isIncorrect = true;
- }
- } else {
- System.out.println("File is empty!\nEnter the argument from console");
- isIncorrect = true;
- }
- if (isIncorrect) {
- argument = getArgumentFromConsole();
- }
- return argument;
- }
- public static int getArgumentFromConsole() {
- System.out.println("Enter the argument of the function:");
- int argument = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- argument = Integer.parseInt(sysScan.nextLine());
- } catch (NumberFormatException e) {
- System.err.println("Enter the integer number!");
- isIncorrect = true;
- }
- if (!isIncorrect && (argument < 1 || argument > 15000)) {
- System.err.println("Enter the number in range [0; 15000]!");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return argument;
- }
- public static void outputResult(int way, int result) {
- switch (way) {
- case FILE:
- System.out.println("Enter the absolute link to the output file");
- String pathOfOutput = getPath();
- try {
- FileWriter writer = new FileWriter(pathOfOutput);
- writer.write("The value of the function: " + result);
- writer.close();
- } catch (IOException e) {
- System.err.println("I/O Error!");
- }
- break;
- case CONSOLE:
- System.out.println("The value of the function: " + result);
- break;
- }
- }
- public static int findValueOfFunction(int n, int i) {
- if (n != 1) {
- if (i > 1) {
- return n/i + findValueOfFunction(n, i - 1 );
- } else {
- return 0;
- }
- } else {
- return 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement