ValeriaAVR

23java

Oct 24th, 2024
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.92 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3. public class Main {
  4.     final static int MIN_N = 2;
  5.     final static int MAX_N = 100;
  6.     final static int MIN_A = -10000;
  7.     final static int MAX_A = 10000;
  8.     static Scanner in = new Scanner(System.in);
  9.  
  10.     public static int inputAndCheckChoice(boolean isIncorrect) {
  11.         isIncorrect = true;
  12.         int choice = 0;
  13.         do {
  14.             System.out.println("Введите ваш выбор.");
  15.             choice = Integer.parseInt(in.nextLine());
  16.             if (choice != 1 && choice != 2) {
  17.                 System.err.println("Введите 1 или 2.");
  18.             } else {
  19.                 isIncorrect = false;
  20.             }
  21.         } while (isIncorrect);
  22.         return choice;
  23.     }
  24.  
  25.     public static int inputAndCheckN(boolean isIncorrect) {
  26.         int n = 0;
  27.         do {
  28.             isIncorrect = false;
  29.             System.out.println("Введите порядок матрицы n: ");
  30.             try {
  31.                 n = Integer.parseInt(in.nextLine());
  32.             } catch (NumberFormatException e) {
  33.                 isIncorrect = true;
  34.                 System.err.println("Некорректный ввод. Введите целое число.");
  35.             }
  36.             if (!isIncorrect && (n < MIN_N || n > MAX_N)) {
  37.                 isIncorrect = true;
  38.                 System.err.println("Диапазон значений n от " + MIN_N + " до " + MAX_N + ".");
  39.             }
  40.         } while (isIncorrect);
  41.         return n;
  42.     }
  43.  
  44.     public static boolean isEof(FileTypes fileType) {
  45.         boolean isEof = false;
  46.         File tempFile;
  47.         try (BufferedReader br = new BufferedReader(new BufferedReader(fileType))
  48.         ) {
  49.             String line;
  50.             if ((line = br.readLine()) != null) {
  51.                 isEof = true;
  52.             }
  53.         } catch (IOException e) {
  54.             System.err.println("Файл пустой.");
  55.         }
  56.         return isEof;
  57.     }
  58.  
  59.     public static String inputFileName(FileTypes fileType, boolean isIncorrect) {
  60.         String fileName = "";
  61.         isIncorrect = true;
  62.         File tempFile;
  63.         do {
  64.             System.out.println("Введите имя файла с путём.");
  65.             fileName = in.nextLine();
  66.             tempFile = new File(fileName);
  67.             if (!tempFile.exists()) {
  68.                 System.out.println("Файл не существует.");
  69.             } else if (fileName.length() < 5 || !fileName.endsWith(".txt")) {
  70.                 System.out.println("Файл не является текстовым.");
  71.             } else if (fileType == FileTypes.IN && !tempFile.canRead()) {
  72.                 System.out.println("Этот файл невозможно открыть для чтения.");
  73.             } else if (fileType == FileTypes.IN && !isEof()) {
  74.                 System.out.println("Этот файл пустой.");
  75.             } else if (fileType == FileTypes.OUT && !tempFile.canWrite()) {
  76.                 System.out.println("Этот файл невозможно открыть для записи.");
  77.             } else {
  78.                 isIncorrect = false;
  79.             }
  80.         }
  81.         return isIncorrect;
  82.     }
  83.  
  84.     public static String requestFileNameForReading(boolean isIncorrect) {
  85.         String fileName = "";
  86.         isIncorrect = true;
  87.         File tempFile;
  88.         do {
  89.             System.out.println("Введите имя файла с путём.");
  90.             fileName = in.nextLine();
  91.             tempFile = new File(fileName);
  92.             if (!tempFile.exists()) {
  93.                 System.out.println("Файл не существует.");
  94.             } else if (fileName.length() < 5 || !fileName.endsWith(".txt")) {
  95.                 System.out.println("Файл не является текстовым.");
  96.             } else if (!tempFile.canRead()) {
  97.                 System.out.println("Этот файл невозможно открыть для чтения.");
  98.             } else if (!isEof()) {
  99.                 System.out.println("Этот файл пустой.");
  100.             } else {
  101.                 isIncorrect = false;
  102.             }
  103.         } while isIncorrect;
  104.         return fileName;
  105.     }
  106.  
  107.     public static String requestFileNameForWriting(boolean isIncorrect) {
  108.         String fileName = "";
  109.         isIncorrect = true;
  110.         File tempFile;
  111.         do {
  112.             System.out.println("Введите имя файла с путём.");
  113.             fileName = in.nextLine();
  114.             tempFile = new File(fileName);
  115.             if (!tempFile.exists()) {
  116.                 System.out.println("Файл не существует.");
  117.             } else if (fileName.length() < 5 || !fileName.endsWith(".txt")) {
  118.                 System.out.println("Файл не является текстовым.");
  119.             } else if (!tempFile.canWrite()) {
  120.                 System.out.println("Этот файл невозможно открыть для записи.");
  121.             } else {
  122.                 isIncorrect = false;
  123.             }
  124.         } while isIncorrect;
  125.         return fileName;
  126.     }
  127.  
  128.     public static int readSizeFromFile(final String fileName) {
  129.         int size = 0;
  130.         int count = 0;
  131.         double temp;
  132.         String[] doubleInString = "";
  133.         String templine;
  134.         try (BufferedReader fReader = new BufferedReader(new FileReader(fileName))) {
  135.             while ((templine = fReader.readLine()) != null) {
  136.                 doubleInString = fReader.readLine().split(" ");
  137.                 count += doubleInString.length;
  138.             }
  139.         } catch (IOException e) {
  140.             System.err.println("Непредвиденная ошибка.");
  141.         }
  142.     }
  143.  
  144.     public static double[] readMatrix (final int size; final String fileName) {
  145.         double[][] matrix = new double[size][size];
  146.         String[] doubleInString = "";
  147.         try (BufferedReader fReader = new BufferedReader(new FileReader(fileName))) {
  148.             for (int i = 0; i < size; i++) {
  149.                 doubleInString = fReader.readLine().split(" ");
  150.                 for (int j = 0; j < size; j++) {
  151.                     matrix[i][j] = Double.parseDouble(doubleInString[j]);
  152.                 }
  153.             }
  154.         } catch (NumberFormatException e) {
  155.             System.err.println("Некорректный тип данных.");
  156.             matrix = null;
  157.         } catch (IOException e) {
  158.             System.err.println("Непредвиденная ошибка.");
  159.             matrix = null;
  160.         }
  161.         for (int i = 0; i < size; i++) {
  162.             for (int j = 0; j < size; j++) {
  163.                 if (matrix != null && (matrix[i][j] < MIN_A || matrix[i][j] > MAX_A)) {
  164.                     System.err.println("Ошибка при считывании матрицы из файла.");
  165.                 }
  166.             }
  167.         }
  168.         return matrix;
  169.     }
  170.  
  171.     public static void createMatrix(final int n; double[][] matrix;
  172.     boolean isIncorrect)
  173.  
  174.     {
  175.         for (int i = 0; i < n; i++) {
  176.             for (int j = 0:j < n;
  177.             j++){
  178.                 do {
  179.                     isIncorrect = false;
  180.                     System.out.println("Введите элемент матрицы А" + i + j + ": ");
  181.                     try {
  182.                         matrix[i][j] = Double.parseDouble(in.nextLine());
  183.                     } catch (NumberFormatException e) {
  184.                         isIncorrect = true;
  185.                         System.err.println("Некорректный ввод. Введите число.");
  186.                     }
  187.                     if (!isIncorrect && (matrix[i][j] < MIN_A || matrix[i][j] > MAX_A)) {
  188.                         isIncorrect = true;
  189.                         System.err.println("Диапазон значений A от " + MIN_A + " до " + MAX_A + ".");
  190.                     }
  191.                 } while (isIncorrect);
  192.             }
  193.         }
  194.     }
  195.  
  196.     public static double sumElements(double[][] matrix; int n )
  197.  
  198.     {
  199.         int sum = 0;
  200.         for (int i = 0; i < n; i++) {
  201.             for (int j = 0; j < n; j++) {
  202.                 sum += matrix[i][j];
  203.             }
  204.         }
  205.         return sum;
  206.     }
  207.  
  208.     public static boolean writeToFile(final String fileName; final double sum; boolean isIncorrect) {
  209.         isIncorrect = true;
  210.         try (BufferedWriter fWriter = new BufferedWriter(new FileWriter(fileName))) {
  211.          fWriter.write(sum);
  212.             System.out.println("Результат выведен в файле.");
  213.             isIncorrect = false;
  214.         }
  215.         catch (IOException e) {
  216.             System.err.println("Непредвиденная ошибка.");
  217.         }
  218.         return isIncorrect;
  219.     }
  220.  
  221.     public static void main(String[] args) {
  222.         int choice;
  223.         int n;
  224.         boolean isIncorrect;
  225.         System.out.println("Откуда Вы хотите вводить данные? \n 1 - консоль, 2 - файл.");
  226.         choice = inputAndCheckChoice(isIncorrect);
  227.         n = inputAndCheckN(isIncorrect);
  228.         double[][] matrix = new double[n][n];
  229.         if (choice == 1) {
  230.             createMatrix(n, matrix, isIncorrect);
  231.         } else {
  232.             do {
  233.                 fileName = requestFileNameForReading(isIncorrect);
  234.                 matrix = readMatrix(size, fileName);
  235.             } while isIncorrect;
  236.         }
  237.         sum = sumElements(matrix, n);
  238.         System.out.println("Куда Вы хотите вывести результат? \n 1 - консоль, 2 - файл.");
  239.         choice = inputAndCheckChoice();
  240.         if (choice == 1) {
  241.             System.out.println("Сумма равна " + sum);
  242.         } else {
  243.             do {
  244.                 fileName = requestFileNameForWriting(isIncorrect);
  245.                 isIncorrect = writeToFile();
  246.             } while isIncorrect;
  247.         }
  248.         in.close();
  249.     }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment