Advertisement
MaksNew

Untitled

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