Advertisement
Vanya_Shestakov

laba2.4 (Java)

Oct 11th, 2020 (edited)
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.52 KB | None | 0 0
  1. package com.company;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.Scanner;
  7.  
  8. public class Main {
  9.  
  10.     public static String inputPath(){
  11.         Scanner scan = new Scanner(System.in);
  12.         boolean isIncorrect;
  13.         String path;
  14.         do {
  15.             isIncorrect = false;
  16.             path = scan.nextLine();
  17.             File file = new File(path);
  18.  
  19.             if (!file.exists()){
  20.                 System.out.println("Файл не найден! Введите абсолютную ссылку на файл");
  21.                 isIncorrect = true;
  22.             }
  23.         }while (isIncorrect);
  24.         return path;
  25.     }
  26.  
  27.     public static int inputOrderFromFile(String path) throws FileNotFoundException {
  28.         Scanner scan = new Scanner(new File(path));
  29.         int order;
  30.         boolean flag = true;
  31.         System.out.println("Порядок матрицы считывается из файла...");
  32.  
  33.         try {
  34.             order = scan.nextInt();
  35.         } catch (Exception e) {
  36.             flag = false;
  37.             order = inputOrderFromConsole();
  38.         }
  39.         if (flag && (order < 2 || order > 6)) {
  40.             order = inputOrderFromConsole();
  41.         }
  42.         return order;
  43.     }
  44.  
  45.     public static int inputOrderFromConsole(){
  46.         Scanner scan = new Scanner(System.in);
  47.         System.out.println("В вашем файле вписан некорректный порядок матрицы. Введите порядок из косоли!");
  48.         int order = 0;
  49.         boolean isIncorrect;
  50.  
  51.         do {
  52.             isIncorrect = false;
  53.             try {
  54.                 order = Integer.parseInt(scan.nextLine());
  55.             } catch (Exception e) {
  56.                 System.out.println("Введите целое число!");
  57.                 isIncorrect = true;
  58.             }
  59.             if (isIncorrect == false && (order < 2 || order > 6)) {
  60.                 System.out.println("Некорректные данные. Введите порядок из диапазона [2;6]");
  61.                 isIncorrect = true;
  62.             }
  63.         }while (isIncorrect);
  64.         return order;
  65.     }
  66.  
  67.     public static void inputMatrixFromFile(double[][] matrix, String path) throws FileNotFoundException {
  68.         Scanner scan = new Scanner(new File(path));
  69.         scan.nextLine();
  70.         System.out.println("Матрица считывается из файла...");
  71.  
  72.         for (int i = 0; i < matrix.length; i++){
  73.             for (int j = 0; j < matrix.length; j++){
  74.                 try {
  75.                     matrix[i][j] = scan.nextDouble();
  76.                 }catch (Exception e){
  77.                     System.out.println("В вашем файле некорректный элемент матрицы a" + (i + 1) + (j + 1) + ". Введите его из консоли!");
  78.                     matrix[i][j] = inputMatrixFromConsole();
  79.                     scan.next();
  80.                 }
  81.             }
  82.         }
  83.     }
  84.  
  85.     public static double inputMatrixFromConsole(){
  86.         Scanner scan = new Scanner(System.in);
  87.         double element = 0;
  88.         boolean isIncorrect;
  89.  
  90.         do {
  91.             isIncorrect = false;
  92.             try {
  93.                 element = Double.parseDouble(scan.nextLine());
  94.             } catch (Exception e) {
  95.                 System.out.println("Введите число!");
  96.                 isIncorrect = true;
  97.             }
  98.         }while (isIncorrect);
  99.         return element;
  100.     }
  101.  
  102.     public static void printMatrix(double[][] matrix){
  103.         for (int i = 0; i < matrix.length; i++){
  104.             System.out.println();
  105.             for (int j = 0; j < matrix.length; j++){
  106.                 System.out.print(matrix[i][j] + "  ");
  107.             }
  108.         }
  109.         System.out.println();
  110.     }
  111.  
  112.     public static void doInsertionSort(double[][] matrix){
  113.         for (int i = 0; i < matrix.length; i++){
  114.             for (int j = 0; j < matrix.length; j++){
  115.                 double current = matrix[i][j];
  116.                 int k = j;
  117.                 while(k > 0 && current < matrix[i][k - 1]) {
  118.                     matrix[i][k] = matrix[i][k - 1];
  119.                     k--;
  120.                 }
  121.                 matrix[i][k] = current;
  122.             }
  123.         }
  124.     }
  125.  
  126.     public static void doBubbleSortForStrings(double[][] matrix){
  127.         for (int k = 0; k < matrix.length; k++) {
  128.             for (int i = 0; i < matrix.length - 1 ; i++ ) {
  129.                 if ( matrix[i][matrix.length - 1] > matrix[i + 1][matrix.length - 1]) {
  130.                     for (int j = 0 ; j < matrix.length; j++) {
  131.                         double temp = matrix[i][j];
  132.                         matrix[i][j] = matrix[i + 1][j];
  133.                         matrix[i + 1][j] = temp;
  134.                     }
  135.                 }
  136.             }
  137.         }
  138.     }
  139.  
  140.     public static void outputToFile(double[][] matrix, String path) throws IOException {
  141.         FileWriter writer = new FileWriter(path);
  142.  
  143.         for (int i = 0; i < matrix.length; i++){
  144.             for (int j = 0; j < matrix.length; j++){
  145.                 writer.write(matrix[i][j] + "  ");
  146.             }
  147.             writer.write("\n");
  148.         }
  149.         writer.close();
  150.         System.out.println("Данные успешно записаны в файл!");
  151.     }
  152.  
  153.     public static void main(String[] args) throws IOException {
  154.         System.out.println("Дана матрица A порядка n. \nПрограмма отсортировывает строки матрицы \nв порядке возрастания наибольших элементов в каждой строке.\n(Порядок матрицы должен быть в диапазоне целых чисел от 2 до 6)\n");
  155.  
  156.         System.out.println("Введите абсолютную ссылку на файл ввода");
  157.         String pathInput = inputPath();
  158.         System.out.println("Введите абсолютную ссылку на файл вывода");
  159.         String pathOutput = inputPath();
  160.  
  161.         int order = inputOrderFromFile(pathInput);
  162.         double[][] matrix = new double[order][order];
  163.         inputMatrixFromFile(matrix, pathInput);
  164.         System.out.print("Исходная матрица:");
  165.         printMatrix(matrix);
  166.  
  167.         doInsertionSort(matrix);
  168.         System.out.print("Отсортированная матрица:");
  169.         printMatrix(matrix);
  170.  
  171.         outputToFile(matrix, pathOutput);
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement