MaksNew

Untitled

Oct 29th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.53 KB | None | 0 0
  1. package com.company;
  2. import java.util.Scanner;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9.  
  10.  
  11. public class Main {
  12.  
  13.     public static Scanner scan = new Scanner(System.in);
  14.  
  15.     static boolean isFileCorrect(String path, int size) throws IOException {
  16.         int num;
  17.         int totalCount = 0;
  18.         num = 0;
  19.         boolean isCorrect;
  20.         FileReader reader = new FileReader(path);
  21.         Scanner scanner = new Scanner(reader);
  22.         isCorrect = true;
  23.         while (scanner.hasNextLine() && isCorrect) {
  24.             Matcher matcher = Pattern.compile("\\d+").matcher(scanner.nextLine());
  25.             int iCount = 0;
  26.             while (matcher.find()) {
  27.                 iCount++;
  28.                 totalCount++;
  29.             }
  30.             if (iCount != size && totalCount != size * size) {
  31.                 isCorrect = false;
  32.             }
  33.         }
  34.         scanner.close();
  35.         reader.close();
  36.         return isCorrect;
  37.     }
  38.  
  39.  
  40.     static String FilePath(int order) throws IOException {
  41.         String path;
  42.         boolean isIncorrect = true;
  43.         do {
  44.             System.out.println("Введите абсолютный путь к файлу: ");
  45.             path = scan.nextLine();
  46.             File matrixFile = new File(path);
  47.             if (!matrixFile.exists()) {
  48.                 System.out.println("Файл не найден. Проверьте введённый путь.");
  49.             } else {
  50.                 if (isFileCorrect(path, order)) {
  51.                     isIncorrect = false;
  52.                 } else {
  53.                     System.out.println("Проверьте данные в файле. Это должны быть числа, записанные в виде матрицы. Порядок матрицы должен соответствовать введённому.");
  54.                 }
  55.             }
  56.         } while (isIncorrect);
  57.         return path;
  58.     }
  59.  
  60.  
  61.     static int InputOrder() {
  62.         int order = 0;
  63.         boolean IsNotCorrect;
  64.         do {
  65.             System.out.println("Введите порядок матрицы: ");
  66.             IsNotCorrect = false;
  67.             try {
  68.                 order = Integer.parseInt(scan.nextLine());
  69.             } catch (Exception e) {
  70.                 IsNotCorrect = true;
  71.                 System.out.println("Порядок матрицы должен быть числом.");
  72.             }
  73.             if ((((order < 1) || (order > 10000))) && !IsNotCorrect) {
  74.                 System.out.println("Размер матрицы должен принадлежать промежутку от 1 до 10000");
  75.                 IsNotCorrect = true;
  76.             }
  77.         } while (IsNotCorrect);
  78.         return order;
  79.     }
  80.  
  81.     static int[][] MatrixCreate(int order) {
  82.         int[][] matrix = new int[order][order];
  83.         return matrix;
  84.     }
  85.  
  86.     static int[][] FileToMatrix(int[][] matrix, int order, String path) throws IOException {
  87.         FileReader matrixFile = new FileReader(path);
  88.         Scanner sc = new Scanner (matrixFile);
  89.         for (int i = 0; i < order; i++) {
  90.             for (int j = 0; j < order; j++) {
  91.                 matrix[i][j] = sc.nextInt();
  92.             }
  93.         }
  94.         System.out.println();
  95.         return matrix;
  96.     }
  97.  
  98.     static void PrintMatrix(int[][] matrix, int order) {
  99.         for (int i = 0; i < order; i++) {
  100.             for (int j = 0; j < order; j++) {
  101.                 System.out.printf("%6d", matrix[i][j]);
  102.             }
  103.             System.out.println();
  104.         }
  105.         System.out.println();
  106.     }
  107.  
  108.     static void Condition(int[][] matrix, int order, int[] zero) {
  109.         for (int i = 0; i < order; i++) {
  110.             zero[i] = 1;
  111.             System.out.printf("%6d", zero[i]);
  112.         }
  113.         System.out.println();
  114.         for (int i = 0; i < order; i++) {
  115.             for (int j = 0; j < order; j++) {
  116.                 System.out.printf("%6d", matrix[i][j]);
  117.             }
  118.             System.out.println();
  119.         }
  120.         for (int i = 0; i < order; i++) {
  121.             System.out.printf("%6d", zero[i]);
  122.         }
  123.         System.out.println();
  124.         System.out.println();
  125.     }
  126.  
  127.     static String Output() {
  128.         String patho;
  129.         boolean isIncorrect;
  130.         isIncorrect = true;
  131.         do {
  132.             System.out.println("Введите директорию, в которую хотите сохранить матрицу: ");
  133.             patho = scan.nextLine();
  134.             File outputDirectory = new File(patho);
  135.             if (outputDirectory.isDirectory() && outputDirectory.exists()) {
  136.                 isIncorrect = false;
  137.             } else {
  138.                 System.out.println("Такой директории не существует. Попробуйте ещё раз.");
  139.             }
  140.  
  141.         } while (isIncorrect);
  142.         return patho;
  143.     }
  144.  
  145.  
  146.     static void ConditionToFile(int[][] matrix, int order, int[] zero) throws IOException {
  147.         String patho;
  148.         patho = Output();
  149.         FileWriter myWriter = new FileWriter(patho + "output.txt");
  150.         for (int i = 0; i < order; i++) {
  151.             zero[i] = 1;
  152.             myWriter.write(String.format("%6d", zero[i]));
  153.         }
  154.         myWriter.write("\n");
  155.  
  156.         for (int i = 0; i < order; i++) {
  157.             for (int j = 0; j < order; j++) {
  158.                 myWriter.write(String.format("%6d", matrix[i][j]));
  159.             }
  160.             myWriter.write("\n");
  161.         }
  162.  
  163.         for (int i = 0; i < order; i++) {
  164.             zero[i] = 1;
  165.             myWriter.write(String.format("%6d", zero[i]));
  166.         }
  167.         myWriter.write("\n");
  168.         myWriter.close();
  169.         System.out.println("Матрица сохранена по указанному пути.");
  170.         System.out.println();
  171.     }
  172.  
  173.  
  174.     public static void main(String[] args) throws IOException {
  175.         int order;
  176.         int[][] matrix;
  177.         String path;
  178.         order = InputOrder();
  179.         int[] zero = new int[order];
  180.         path = FilePath(order);
  181.         matrix = MatrixCreate(order);
  182.         matrix = FileToMatrix(matrix, order, path);
  183.         System.out.println("Матрица до выполнения условия:");
  184.         PrintMatrix(matrix, order);
  185.         System.out.println("Матрица после выполнения условия:");
  186.         Condition(matrix, order, zero);
  187.         ConditionToFile(matrix, order, zero);
  188.     }
  189. }
  190.  
  191.  
Advertisement
Add Comment
Please, Sign In to add comment