Advertisement
MadCortez

Untitled

Oct 27th, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.27 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. class MyClass {
  5.  
  6.     static Scanner scanner = new Scanner(System.in);
  7.  
  8.     public static void printTask() {
  9.         System.out.println("Данная программа в матрице порядка 2n меняет местами подматрицы порядка n");
  10.         System.out.println("1 2");
  11.         System.out.println("3 4");
  12.         System.out.println("---");
  13.         System.out.println("4 3");
  14.         System.out.println("1 2");
  15.     }
  16.  
  17.     public static double[][] swap(double[][] matrix) {
  18.         int n = matrix.length - 1;
  19.         int n1 = n / 2;
  20.         for (int i = 0; i <= n1; i++)
  21.             for (int j = 0; j <= n; j++) {
  22.                 double temp = matrix[i][j];
  23.                 matrix[i][j] = matrix[n1 + i + 1][j];
  24.                 matrix[n1 + i + 1][j] = temp;
  25.             }
  26.         for (int i = 0; i <= n1; i++)
  27.             for (int j = 0; j <= n1; j++) {
  28.                 double  temp = matrix[i][j];
  29.                 matrix[i][j] = matrix[i][n1 + j + 1];
  30.                 matrix[i][n1 + j + 1] = temp;
  31.             }
  32.         return matrix;
  33.     }
  34.  
  35.     public static void userInputArrayFromConsole(int n) throws FileNotFoundException {
  36.         final int MIN_VALUE = -500;
  37.         final int MAX_VALUE = 500;
  38.         double[][] matrix = new double[n][];
  39.         System.out.println("Введите элементы массива в диапазоне " + MIN_VALUE + ".." + MAX_VALUE + ":");
  40.         for (int i = 0; i < n; i++) {
  41.             matrix[i] = new double[n];
  42.             for (int j = 0; j < n; j++) {
  43.                 matrix[i][j] = inputValue(MIN_VALUE, MAX_VALUE);
  44.             }
  45.         }
  46.         outputMethod(swap(matrix));
  47.     }
  48.  
  49.     public static PrintWriter userOutputPath() throws FileNotFoundException {
  50.         String path;
  51.         PrintWriter file;
  52.         System.out.println("Введите абсолютный путь к файлу для вывода результата");
  53.         String zero = scanner.nextLine();
  54.         path = scanner.nextLine();
  55.         file = new PrintWriter(path);
  56.         return file;
  57.     }
  58.  
  59.     public static Scanner userInputPath() throws FileNotFoundException {
  60.         String path;
  61.         boolean isNotValid;
  62.         Scanner file = null;
  63.         String zero = scanner.nextLine();
  64.         do {
  65.             isNotValid = false;
  66.             System.out.println("Введите абсолютный путь к файлу с входными данными");
  67.             path = scanner.nextLine();
  68.             try {
  69.                 file = new Scanner(new File(path));
  70.             } catch (FileNotFoundException e) {
  71.                 isNotValid = true;
  72.                 System.out.println("Файл не найден");
  73.             }
  74.         } while (isNotValid);
  75.         return file;
  76.     }
  77.  
  78.     public static void userInputFromFile() throws FileNotFoundException {
  79.         int n;
  80.         Scanner file = userInputPath();
  81.         n = file.nextInt();
  82.         double[][] matrix = new double[n][];
  83.         for (int i = 0; i < n; i++) {
  84.             matrix[i] = new double[n];
  85.             for (int j = 0; j < n; j++)
  86.                  matrix[i][j] = file.nextInt();
  87.         }
  88.         outputMethod(swap(matrix));
  89.     }
  90.  
  91.     public static void printWithPath(double[][] matrix) throws FileNotFoundException {
  92.         PrintWriter out = userOutputPath();
  93.         for (int i = 0; i < matrix.length; i++) {
  94.             for (int j = 0; j < matrix[i].length; j++)
  95.                 out.printf("%.0f ", matrix[i][j]);
  96.             out.println();
  97.         }
  98.         out.close();
  99.         System.out.println("Результат работы помещён в файл");
  100.     }
  101.  
  102.     public static void printWithoutPath(double[][] matrix) {
  103.         for (int i = 0; i < matrix.length; i++) {
  104.             for (int j = 0; j < matrix[i].length; j++)
  105.                 System.out.printf("%.0f ", matrix[i][j]);
  106.             System.out.println();
  107.         }
  108.     }
  109.  
  110.     public static void outputMethod(double[][] matrix) throws FileNotFoundException {
  111.         int method;
  112.         System.out.println("Куда хотите вывести результат?");
  113.         System.out.println("1 - в консоль");
  114.         System.out.println("2 - в файл");
  115.         do {
  116.             method = scanner.nextInt();
  117.             switch (method) {
  118.                 case 1: { printWithoutPath(matrix); break; }
  119.                 case 2: { printWithPath(matrix); break; }
  120.                 default: System.out.println("Введите корректный способ вывода");
  121.             }
  122.         } while (method != 2 && method != 1);
  123.     }
  124.  
  125.     public static void inputMethod() throws FileNotFoundException {
  126.         int method;
  127.         System.out.println("Каким способом хотите ввести данные?");
  128.         System.out.println("1 - с помощью консоли");
  129.         System.out.println("2 - с помощью файла");
  130.         do {
  131.             method = scanner.nextInt();
  132.             switch (method) {
  133.                 case 1: { userInputFromConsole(); break; }
  134.                 case 2: { userInputFromFile(); break; }
  135.                 default: System.out.println("Введите корректный способ ввода");
  136.             }
  137.         } while (method != 2 && method != 1);
  138.     }
  139.  
  140.     public static void userInputFromConsole() throws FileNotFoundException {
  141.     final int MIN_SIZE = 1;
  142.     final int MAX_SIZE = 20;
  143.         int n;
  144.         System.out.print("Введите порядок матрицы 2n в диапазоне " + MIN_SIZE + ".." + MAX_SIZE + ": ");
  145.         n = inputValue(MIN_SIZE, MAX_SIZE);
  146.         userInputArrayFromConsole(n);
  147.     }
  148.  
  149.     public static int inputValue(int min, int max) {
  150.         int currentValue;
  151.         boolean isNotValid = true;
  152.         do {
  153.             currentValue = scanner.nextInt();
  154.             if (currentValue >= min && currentValue <= max)
  155.                 isNotValid = false;
  156.             else
  157.                 System.out.println("Введите число в заданном диапазоне");
  158.         } while (isNotValid);
  159.         return currentValue;
  160.     }
  161.  
  162.     public static void main(String[] args) throws Exception {
  163.         printTask();
  164.         inputMethod();
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement