Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.22 KB | None | 0 0
  1. package com.company;
  2.  
  3. import com.sun.org.apache.bcel.internal.generic.SWAP;
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.File;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileReader;
  9. import java.util.Scanner;
  10. import java.io.*;
  11. import java.lang.NumberFormatException;
  12.  
  13. public class Main {
  14.  
  15.  
  16.     static Scanner sc = new Scanner(System.in);
  17.  
  18.     static int[] shellSort(int[] arr) {
  19.         int step = arr.length;
  20.         while (step > 0) {
  21.             for (int i = 0; i < arr.length - step; i++) {
  22.                 int j = i;
  23.                 while (j >= 0 && arr[j] > arr[j + step]) {
  24.                     arr[j] += arr[j + step];
  25.                     arr[j + step] = arr[j] - arr[j + step];
  26.                     arr[j] -= arr[j + step];
  27.                     j--;
  28.                 }
  29.             }
  30.             step /= 2;
  31.         }
  32.         return arr;
  33.     }
  34.  
  35.     static void outputArray(int[] arr) {
  36.         System.out.println("Resul array");
  37.         for (int i = 0; i < arr.length; i++) {
  38.             if (i != 0)
  39.                 System.out.print(", ");
  40.             System.out.print(arr[i]);
  41.         }
  42.         System.out.println();
  43.     }
  44.  
  45.     static void outputToFile(int[] arr) {
  46.         boolean incorrect;
  47.         sc.nextLine();
  48.         do {
  49.             incorrect = false;
  50.             System.out.println("Enter new file location:");
  51.             String line = sc.nextLine();
  52.             try (PrintWriter writer = new PrintWriter(line, "UTF-8")) {
  53.                 for (int i = 0; i < arr.length; i++)
  54.                     writer.write(arr[i] + "\n");
  55.             } catch (FileNotFoundException e) {
  56.                 System.out.println("File not found, try again");
  57.                 incorrect = true;
  58.             } catch (Exception e) {
  59.                 incorrect = true;
  60.                 e.printStackTrace();
  61.             }
  62.             System.out.println("Successfully recorded");
  63.         } while (incorrect);
  64.     }
  65.  
  66.     static int[] getArrayFromFile() {
  67.         sc.nextLine();
  68.         boolean incorrect;
  69.         String line = "";
  70.         int[] arr = {};
  71.         do {
  72.             incorrect = false;
  73.             System.out.println("Enter file location: ");
  74.             line = sc.nextLine();
  75.             try (BufferedReader br = new BufferedReader(new FileReader(new File(line)))) {
  76.                 line = br.readLine();
  77.                 int size = Integer.parseInt(line);
  78.                 arr = new int[size];
  79.                 line = br.readLine();
  80.                 String[] arrFillings = line.split(" ");
  81.                 for (int i = 0; i < size; i++)
  82.                     arr[i] = Integer.parseInt(arrFillings[i]);
  83.             } catch (NumberFormatException e) {
  84.                 incorrect = true;
  85.                 System.out.println("Invalid data, try again");
  86.             } catch (FileNotFoundException e) {
  87.                 incorrect = true;
  88.                 System.out.println("File with this location not found");
  89.             } catch (Exception e) {
  90.                 incorrect = true;
  91.                 e.printStackTrace();
  92.             }
  93.         } while (incorrect);
  94.         return arr;
  95.     }
  96.  
  97.     static int[] getArrayFromConsole() {
  98.         boolean incorrect;
  99.         String line;
  100.         sc.nextLine();
  101.         do {
  102.             incorrect = false;
  103.             System.out.println("Enter the number of items greater than 2");
  104.             line = sc.nextLine();
  105.             try {
  106.                 if (Integer.parseInt(line) < 2) {
  107.                     incorrect = true;
  108.                     System.out.println("The number of elements must be greater than 2");
  109.                 }
  110.             } catch (Exception e) {
  111.                 incorrect = true;
  112.                 System.out.println("Enter a natural numeric value");
  113.             }
  114.         } while (incorrect);
  115.         int[] arr = new int[Integer.parseInt(line)];
  116.         for (int i = 0; i < arr.length; i++) {
  117.             do {
  118.                 incorrect = false;
  119.                 System.out.println("Enter " + (i + 1) + " element");
  120.                 line = sc.nextLine();
  121.                 try {
  122.                     arr[i] = Integer.parseInt(line);
  123.                 } catch (Exception e) {
  124.                     incorrect = true;
  125.                     System.out.println("Enter an integer value");
  126.                 }
  127.             } while (incorrect);
  128.         }
  129.         return arr;
  130.     }
  131.  
  132.     static int[] chooseInput() {
  133.         char inputType;
  134.         int[] resArr = {};
  135.         boolean incorrect;
  136.         incorrect = true;
  137.         do {
  138.             System.out.println("Enter 'F' to choose input from file\nEnter 'C to choose input from console");
  139.             inputType = sc.next().charAt(0);
  140.             inputType = Character.toUpperCase(inputType);
  141.             switch (inputType) {
  142.                 case 'F':
  143.                     resArr = getArrayFromFile();
  144.                     incorrect = false;
  145.                     break;
  146.                 case 'C':
  147.                     resArr = getArrayFromConsole();
  148.                     incorrect = false;
  149.                     break;
  150.                 default:
  151.                     System.out.println("Your choose incorrect, try again");
  152.                     break;
  153.             }
  154.  
  155.         } while (incorrect);
  156.         return resArr;
  157.     }
  158.  
  159.     public static void main(String[] args) {
  160.         System.out.println("Task: This program sorts numbers by the shell method");
  161.         char output;
  162.         boolean incorrect = true;
  163.         int[] resArr;
  164.         resArr = chooseInput();
  165.         resArr = shellSort(resArr);
  166.         outputArray(resArr);
  167.         do {
  168.             System.out.println("Do u want to save your array to file? (Y/N)");
  169.             output = sc.next().charAt(0);
  170.             output = Character.toUpperCase(output);
  171.             switch (output) {
  172.                 case 'Y':
  173.                     outputToFile(resArr);
  174.                     incorrect = false;
  175.                     break;
  176.                 case 'N':
  177.                     incorrect = false;
  178.                     System.out.println("End of program");
  179.                     break;
  180.                 default:
  181.                     System.out.println("Your choose incorrect, try again");
  182.                     break;
  183.             }
  184.         } while (incorrect);
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement