MaksNew

Untitled

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