Advertisement
Egor_Vakar

lab3_3(java)

Oct 30th, 2021 (edited)
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.91 KB | None | 0 0
  1. package com.company;
  2. import java.io.*;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5. import java.util.Scanner;
  6. import java.util.regex.PatternSyntaxException;
  7.  
  8. public class lab3_3 {
  9.     static Scanner scan = new Scanner(System.in);
  10.  
  11.     public static void main(String[] args){
  12.         System.out.print("Welcome to the program that will sort an array using cocktail sort\nSelect the source for entering the sequence of numbers:\n1:Console\n2:File\nEnter 1 or 2: ");
  13.         byte inputSource = takeSource();
  14.         int[] array = takeArray(inputSource);
  15.         int[][] answer = findStagesOfCocktailSort(array);
  16.         System.out.print("Select the source for output:\n1:Console\n2:File\nEnter 1 or 2: ");
  17.         byte outputSource = takeSource();
  18.         output(outputSource,answer);
  19.         scan.close();
  20.     }
  21.  
  22.     static int takeInt(int min,int max) {
  23.         int number = 0;
  24.         boolean isIncorrect;
  25.         do {
  26.             isIncorrect = false;
  27.             try {
  28.                 number = Integer.parseInt(scan.nextLine());
  29.             } catch (NumberFormatException e) {
  30.                 System.out.println("Incorrect input!!!");
  31.                 isIncorrect = true;
  32.             }
  33.             if (!isIncorrect && (number < min || number > max)){
  34.                 System.out.println("Incorrect input!!!");
  35.                 isIncorrect = true;
  36.             }
  37.         }while (isIncorrect);
  38.         return number;
  39.     }
  40.  
  41.     static byte takeSource(){
  42.         final byte CONSOLE = 1;
  43.         final byte FILE = 2;
  44.         boolean isIncorrect;
  45.         byte choice = 0;
  46.         do {
  47.             isIncorrect = false;
  48.             try {
  49.                 choice = Byte.parseByte(scan.nextLine());
  50.             } catch (Exception e) {
  51.                 System.out.print("Incorrect input!!! Select the source:\n1:Console\n2:File\nEnter 1 or 2: ");
  52.                 isIncorrect = true;
  53.             }
  54.             if (!isIncorrect && (choice != CONSOLE) && (choice != FILE)) {
  55.                 System.out.print("Incorrect input!!! Select the source:\n1:Console\n2:File\nEnter 1 or 2: ");
  56.                 isIncorrect = true;
  57.             }
  58.         } while (isIncorrect);
  59.         return choice;
  60.     }
  61.  
  62.     static String takePathForInput(){
  63.         String path;
  64.         boolean isIncorrect;
  65.         System.out.print("Enter file path: ");
  66.         do {
  67.             isIncorrect = false;
  68.             path = scan.nextLine();
  69.             if (Files.notExists(Path.of(path))) {
  70.                 System.out.print("File is not found\nEnter file path: ");
  71.                 isIncorrect = true;
  72.             }
  73.             if (!isIncorrect && (!path.endsWith(".txt"))) {
  74.                 System.out.print("File is found, but it is not \".txt\" type file\nEnter file path: ");
  75.                 isIncorrect = true;
  76.             }
  77.         }while (isIncorrect);
  78.         return path;
  79.     }
  80.  
  81.     static String takePathForOutput(){
  82.         String path;
  83.         boolean isIncorrect;
  84.         System.out.print("Enter file path: ");
  85.         do {
  86.             isIncorrect = false;
  87.             path = scan.nextLine();
  88.             if (!path.endsWith(".txt")) {
  89.                 System.out.print("It should be a \".txt\" file\nEnter file path: ");
  90.                 isIncorrect = true;
  91.             }
  92.         }while (isIncorrect);
  93.         return path;
  94.     }
  95.  
  96.     static int[] takeArrayFromConsole() {
  97.         final int MIN_SIZE = 2;
  98.         final int MAX_SIZE = 200;
  99.         final int MIN_ELEMENT = -1000;
  100.         final int MAX_ELEMENT = 1000;
  101.         System.out.print("Enter size: ");
  102.         int size = takeInt(MIN_SIZE, MAX_SIZE);
  103.         int[] arr = new int[size];
  104.         for (int i = 0; i < size; i++){
  105.             System.out.print("Enter element " + (i + 1) + ": ");
  106.                     arr[i] = takeInt(MIN_ELEMENT, MAX_ELEMENT);
  107.         }
  108.         return arr;
  109.     }
  110.  
  111.     static int[] takeArrayFromFile(final String path){
  112.         int size = 0;
  113.         final int MIN_SIZE = 2;
  114.         int lineCounter = 0;
  115.         final int SIZE_LINE_NUMBER = 1;
  116.         final int ARRAY_LINE_NUMBER = 2;
  117.         String line;
  118.         String[] stringArray = null;
  119.         boolean isCorrect = true;
  120.         try(BufferedReader reader = new BufferedReader(new FileReader(path))) {
  121.             while ((isCorrect) && ((line = reader.readLine()) != null)) {
  122.                 lineCounter++;
  123.                 if (lineCounter == SIZE_LINE_NUMBER){
  124.                     try {
  125.                         size = Integer.parseInt(line);
  126.                     } catch (Exception e) {
  127.                         System.out.println("Incorrect file content!!! Incorrect information in the size line!!!");
  128.                         isCorrect = false;
  129.                     }
  130.                     if (isCorrect && (size < MIN_SIZE)) {
  131.                         System.out.println("Incorrect file content!!! The size must be higher than 1!!!");
  132.                         isCorrect = false;
  133.                     }
  134.                 }
  135.                 if (lineCounter == ARRAY_LINE_NUMBER){
  136.                     try {
  137.                         stringArray = line.split(" ");
  138.                     } catch (PatternSyntaxException e){
  139.                         System.out.println("Incorrect file content!!! Incorrect information in the array line!!!");
  140.                         isCorrect = false;
  141.                     }
  142.                 }
  143.             }
  144.         }catch (IOException e) {
  145.             System.out.println("Input/Output error!!!");
  146.             isCorrect = false;
  147.         }
  148.         int[] arrayForReturn = null;
  149.         if (stringArray != null) {
  150.             if ((isCorrect) && ((stringArray.length != size) || (lineCounter > ARRAY_LINE_NUMBER))){
  151.                 System.out.println("Incorrect file content!!!");
  152.                 isCorrect = false;
  153.             }else{
  154.                 arrayForReturn = new int[size];
  155.                 for (int i = 0;i < size;i++){
  156.                     try {
  157.                         arrayForReturn[i] = Integer.parseInt(stringArray[i]);
  158.                     } catch (Exception e) {
  159.                         System.out.println("Incorrect file content!!!");
  160.                         isCorrect = false;
  161.                     }
  162.                 }
  163.             }
  164.         }
  165.         if(isCorrect) {
  166.             return arrayForReturn;
  167.         }else{
  168.             return null;
  169.         }
  170.     }
  171.  
  172.     static int[] takeArray(final byte source){
  173.         String inputPath;
  174.         int[] inputArray;
  175.         if (source == 1) {
  176.             inputArray = takeArrayFromConsole().clone();
  177.         } else {
  178.             inputPath = takePathForInput();
  179.             inputArray = takeArrayFromFile(inputPath);
  180.             while (inputArray == null) {
  181.                 inputPath = takePathForInput();
  182.                 inputArray = takeArrayFromFile(inputPath);
  183.             }
  184.         }
  185.         return inputArray;
  186.     }
  187.  
  188.     static void swapArrayElements(int[] array, int i){
  189.         array[i] ^= array[i + 1];
  190.         array[i + 1] ^= array[i];
  191.         array[i] ^= array[i + 1];
  192.     }
  193.  
  194.     public static int[][] cutArray(int counter, int[][] answer){
  195.         int[][] result = new int[counter][answer[0].length];
  196.         for (int i = 0; i < counter; i++)
  197.             result[i] = answer[i];
  198.         return result;
  199.     }
  200.  
  201.     public static int[][] findStagesOfCocktailSort(int[] array) {
  202.         int[][] answer = new int[array.length][array.length];
  203.         for (int k = 0; k < array.length; k++) {
  204.             answer[0][k] = array[k];
  205.         }
  206.         int counter = 0;
  207.         int leftBorder = 0, rightBorder = array.length - 1;
  208.         boolean wasThereSwap = true;
  209.         while ((leftBorder < rightBorder) && wasThereSwap) {
  210.             wasThereSwap = false;
  211.             for (int i = leftBorder; i < rightBorder; i++) {
  212.                 if (array[i] > array[i + 1]) {
  213.                     swapArrayElements(array, i);
  214.                     wasThereSwap = true;
  215.                 }
  216.             }
  217.             if(wasThereSwap){
  218.                 for (int k = 0; k < array.length; k++){
  219.                     answer[counter + 1][k] = array[k];
  220.                 }
  221.             }
  222.             counter++;
  223.             wasThereSwap = false;
  224.             rightBorder--;
  225.             for (int i = rightBorder; i > leftBorder; i--) {
  226.                 if (array[i - 1] > array[i]) {
  227.                     swapArrayElements(array, i - 1);
  228.                     wasThereSwap = true;
  229.                 }
  230.             }
  231.             leftBorder++;
  232.             if(wasThereSwap){
  233.                 for (int k = 0; k < array.length; k++){
  234.                     answer[counter + 1][k] = array[k];
  235.                 }
  236.             }
  237.             counter++;
  238.         }
  239.         return cutArray(counter, answer);
  240.     }
  241.  
  242.     static void outputToConsole(int[][] answer){
  243.         System.out.println("CocktailSort:");
  244.         for (int i = 0; i < answer.length; i++){
  245.             for (int j = 0; j < answer[0].length; j++)
  246.                 System.out.print(answer[i][j] + " ");
  247.             System.out.println();
  248.         }
  249.     }
  250.  
  251.     static void outputToFile(final String path,final int[][] answer){
  252.         boolean isCorrect = true;
  253.         try(FileWriter fw = new FileWriter(path)) {
  254.             fw.write("CocktailSort:\n");
  255.             for (int i = 0; i < answer.length; i++){
  256.                 for (int j = 0; j < answer[0].length; j++)
  257.                     fw.write((answer[i][j]) + " ");
  258.                 fw.write("\n");
  259.             }
  260.         } catch (Exception e) {
  261.             System.out.println("File error!");
  262.             isCorrect = false;
  263.         }
  264.         if (isCorrect) {
  265.             System.out.println("Done");
  266.         } else {
  267.             System.out.println("End");
  268.         }
  269.     }
  270.  
  271.     static void output(final byte source,final int[][] answer){
  272.         String outPath;
  273.         if (source == 1) {
  274.             outputToConsole(answer);
  275.         } else{
  276.             outPath = takePathForOutput();
  277.             outputToFile(outPath, answer);
  278.         }
  279.     }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement