Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.51 KB | None | 0 0
  1. {
  2.     private static int currentIndex = -1;
  3.     private static Integer next(String numbers[]) {
  4.         ++currentIndex;
  5.         while (currentIndex < numbers.length && numbers[currentIndex].equals(""))
  6.             ++currentIndex;
  7.         return currentIndex < numbers.length ? Integer.parseInt(numbers[currentIndex]) : null;
  8.     }
  9.     static void condition(){
  10.         System.out.println("Дана действительная квадратная матрица порядка 2n. Цифрами обозначены подматрицы порядка n");
  11.         System.out.println(" ------- ");
  12.         System.out.println("| 1 | 2 |");
  13.         System.out.println(" ------- ");
  14.         System.out.println("| 3 | 4 |");
  15.         System.out.println(" ------- ");
  16.         System.out.println("Получить новую матрицу: ");
  17.         System.out.println(" -------");
  18.         System.out.println("| 4 | 3 |");
  19.         System.out.println(" ------- ");
  20.         System.out.println("| 1 | 2 |");
  21.         System.out.println(" -------");
  22.  
  23.     }
  24.     static int[][] outputMatrix(int[][] Matrix, int size){
  25.         for (int i = 0; i < size; ++i, System.out.println())
  26.             for (int j = 0; j < size; ++j)
  27.                 System.out.print(Matrix[i][j] + " ");
  28.         return Matrix;
  29.     }
  30.     static void swap(int[][] matrix, int size) {
  31.         int temp;
  32.         int center;
  33.         center = size / 2;
  34.         for (int i = 0; i < (size - center); i++) {
  35.             for (int j = 0; j < (size - center); j++) {
  36.                 temp = matrix[j][i];
  37.                 matrix[j][i] = matrix[j + center][i + center];
  38.                 matrix[j + center][i + center] = temp;
  39.             }
  40.         }
  41.         for (int i = size - 1; i >= center ; i--) {
  42.             for (int j = 0; j < (center); j++) {
  43.                 temp = matrix[j][i];
  44.                 matrix[j][i] = matrix[j + center][i - center];
  45.                 matrix[j + center][i - center] = temp;
  46.             }
  47.         }
  48.         System.out.println("Полученная матрица: ");
  49.         outputMatrix(matrix,size);
  50.     }
  51.     static void outputInFile(int[][] matrix, int size) throws IOException{
  52.         Scanner in = new Scanner(System.in);
  53.         int i,j;
  54.         String outputFileName;
  55.         System.out.println("Введите имя файла в который хотите вывести данные: ");
  56.         outputFileName = in.nextLine();
  57.         outputFileName = outputFileName + ".txt";
  58.         File newFile = new File(outputFileName);
  59.         try
  60.         {
  61.             boolean created = newFile.createNewFile();
  62.             if(created)
  63.                 System.out.println("Файл успешно создан.");
  64.         }
  65.         catch(IOException ex){
  66.             System.out.println(ex.getMessage());
  67.         }
  68.         FileWriter out = new FileWriter(outputFileName);
  69.         out.write("\n Полученная матрица: ");
  70.         for (i = 0; i < size; ++i) {
  71.             for (j = 0; j < size; ++j) {
  72.                 out.write( matrix[i][j] + "\n ");
  73.             }
  74.             out.write("\n ");
  75.         }
  76.         out.close();
  77.     }
  78.    
  79.     public static void main(String[] args) throws IOException {
  80.         Scanner in = new Scanner(System.in);
  81.         String NameOfFile;
  82.         Boolean CorrectFile;
  83.         condition();
  84.         CorrectFile = true;
  85.         System.out.println("Введите имя файла, с которого хотите считать информацию: ");
  86.         NameOfFile = in.nextLine();
  87.         NameOfFile = NameOfFile + ".txt";
  88.         FileInputStream inFile = new FileInputStream(NameOfFile);
  89.         byte[] str = new byte[inFile.available()];
  90.         inFile.read(str);
  91.         String text = new String(str);
  92.         String[] numbers = text.split("\\D");
  93.         int i, j;
  94.         int size = next(numbers);
  95.         if (size % 2 == 0) {
  96.             int[][] matrix = new int[size][size];
  97.             for (i = 0; i < size; ++i)
  98.                 for (j = 0; j < size; ++j)
  99.                     matrix[i][j] = next(numbers);
  100.             System.out.println("Исходная матрица порядка 2n: ");
  101.             outputMatrix(matrix, size);
  102.             swap(matrix, size);
  103.             outputInFile(matrix, size);
  104.         }else{
  105.             System.out.println("Ошибка! Измените файл, он должен содержать только цифры,\n" +
  106.                     " и размер матрицы должен равняться 2n");
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement