Advertisement
Vladislav8653

laba 3_3_java

Dec 4th, 2022
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.70 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.*;
  3. class SelectionSort {
  4.     private static final Scanner scanner = new Scanner(System.in);
  5.     static Scanner fileScanner;
  6.  
  7.     public static void main(String[] args) {
  8.         int[] arr = input();
  9.         arr = sortArrayBySelectionSort(arr);
  10.         output(arr);
  11.     }
  12.  
  13.     private static int inputData() {
  14.         int n = 0;
  15.         boolean isIncorrect;
  16.         do {
  17.             isIncorrect = false;
  18.             try {
  19.                 n = Integer.parseInt(scanner.nextLine());
  20.             } catch (Exception e) {
  21.                 isIncorrect = true;
  22.                 System.out.println("Please, enter a integer number:");
  23.             }
  24.         } while (isIncorrect);
  25.         return n;
  26.     }
  27.     private static boolean choose() {
  28.         int inputNumber;
  29.         boolean isIncorrect;
  30.         final int MIN_NUM = 0;
  31.         final int MAX_NUM = 1;
  32.         do {
  33.             isIncorrect = false;
  34.             inputNumber = inputData();
  35.             if (inputNumber < MIN_NUM || inputNumber > MAX_NUM) {
  36.                 System.out.println("You are out of input range!:");
  37.                 isIncorrect = true;
  38.             }
  39.         } while (isIncorrect);
  40.         if (inputNumber == 1)
  41.             return true;
  42.         else
  43.             return false;
  44.     }
  45.     private static int inputArraySize() {
  46.         boolean isIncorrect;
  47.         final int MIN_SIZE = 2;
  48.         int num;
  49.         do {
  50.             num = inputData();
  51.             isIncorrect = false;
  52.             if (num < MIN_SIZE) {
  53.                 System.out.println("Please, enter a number > 2:");
  54.                 isIncorrect = true;
  55.             }
  56.         } while (isIncorrect);
  57.         return num;
  58.     }
  59.     private static int[] inputArray(int num) {
  60.         int[] arr = new int[num];
  61.         for (int i = 0; i < arr.length; i++)
  62.             arr[i] = inputData();
  63.         return arr;
  64.     }
  65.     private static int[] sortArrayBySelectionSort(int[] arr) {
  66.         System.out.println("Sort stages:");
  67.         int min;
  68.         for (int i = 0; i < arr.length - 1; i++){
  69.             for (int j = i + 1; j < arr.length; j++)
  70.                 if (arr[j] < arr[i]) {
  71.                     min = arr[j];
  72.                     arr[j] = arr[i];
  73.                     arr[i] = min;
  74.                 }
  75.             System.out.print("Stage " + (i + 1) + ": ");
  76.             for (int j : arr) System.out.print(j + " ");
  77.             System.out.println();
  78.         }
  79.         return arr;
  80.     }
  81.     private static void consoleOutput(int[] arr) {
  82.         for (int j : arr) System.out.print(j + " ");
  83.         System.out.println();
  84.     }
  85.     //files
  86.     private static String inputFilePath() {
  87.         String path;
  88.         boolean isIncorrect;
  89.         do {
  90.             isIncorrect = false;
  91.             System.out.println("Input file path:");
  92.             path = scanner.nextLine();
  93.             File file = new File(path);
  94.             if (!file.exists()) {
  95.                 System.out.println("Wrong way to file.");
  96.                 isIncorrect = true;
  97.             }
  98.             if (!file.canRead() && file.exists()) {
  99.                 System.out.println("Impossible to read a file.");
  100.                 isIncorrect = true;
  101.             }
  102.             if (!file.canWrite() && file.canRead()) {
  103.                 System.out.println("Impossible to write a file.");
  104.                 isIncorrect = true;
  105.             }
  106.         } while (isIncorrect);
  107.         return path;
  108.     }
  109.     private static int inputSizeOfArrayFromFile(String path) {
  110.         int num = 0;
  111.         boolean isIncorrect;
  112.         final int MIN = 1;
  113.         do {
  114.             isIncorrect = false;
  115.             try {
  116.                 fileScanner = new Scanner(new File(path));
  117.                 num = fileScanner.nextInt();
  118.             } catch (Exception q) {
  119.                 isIncorrect = true;
  120.                 System.out.println("Check file");
  121.                 path = inputFilePath();
  122.             }
  123.             if (!isIncorrect && (num < MIN)) {
  124.                 isIncorrect = true;
  125.                 System.out.println("Matrix size should be at least 2");
  126.             }
  127.         } while (isIncorrect);
  128.         return num;
  129.     }
  130.     private static int[]inputArrayFile(String path, int num) {
  131.         boolean isIncorrect;
  132.         int[] arr = new int[num];
  133.         do {
  134.             isIncorrect = false;
  135.             try {
  136.                 fileScanner = new Scanner(new File(path));
  137.                 fileScanner.nextLine();
  138.                 for (int i = 0; (i < arr.length); i++) {
  139.                     arr[i] = fileScanner.nextInt();
  140.  
  141.                 }
  142.             } catch (Exception q) {
  143.                 isIncorrect = true;
  144.                 System.out.println("Reading of array elements failed.");
  145.                 path = inputFilePath();
  146.             }
  147.         } while (isIncorrect);
  148.         return arr;
  149.     }
  150.     private static void fileOutput(int[] arr, String path) {
  151.         boolean isIncorrect;
  152.         do {
  153.             isIncorrect = false;
  154.             try {
  155.                 FileWriter writer = new FileWriter(path);
  156.                 writer.write("Sorted array: \n");
  157.                 for (int j : arr) {
  158.                     writer.write(j + " ");
  159.                 }
  160.                 writer.close();
  161.             } catch (IOException e) {
  162.                 isIncorrect = true;
  163.                 System.out.println("Mistake of output in file. Input path.");
  164.                 path = inputFilePath();
  165.             }
  166.         } while(isIncorrect);
  167.         System.out.println("Successful output in file.");
  168.     }
  169.     private static int[] input () {
  170.         System.out.println("Selection sort. Demonstration.");
  171.         System.out.println("Enter type of input: " + '\n' + "1 is console input, 0 is file input.");
  172.         int num;
  173.         int[] arr;
  174.         String path;
  175.         boolean chose = choose();
  176.         if (chose) {
  177.             System.out.println("Input size of array:");
  178.             num = inputArraySize();
  179.             System.out.println("Input array elements:");
  180.             arr = inputArray(num);
  181.             System.out.println("Unsorted array:");
  182.             consoleOutput(arr);
  183.         } else {
  184.             path = inputFilePath();
  185.             num = inputSizeOfArrayFromFile(path);
  186.             arr = inputArrayFile(path, num);
  187.         }
  188.         return arr;
  189.     }
  190.     private static void output (int[] arr){
  191.         System.out.println("Enter type of output: " + '\n' + "1 is console output, 0 is file output.");
  192.         String path;
  193.         boolean chose = choose();
  194.         if (chose) {
  195.             System.out.println("Sorted array:");
  196.             consoleOutput(arr);
  197.         } else {
  198.             path = inputFilePath();
  199.             fileOutput(arr, path);
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement