Advertisement
VadimThink

ы

Oct 13th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. package com.Laba2_4;
  2.  
  3. import java.io.*;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.     public static Scanner in = new Scanner(System.in);
  8.  
  9.  
  10.     public static int[][] inputArrFromFile() {
  11.         boolean isInvalidInput = false;
  12.         int[][] myArr = new int[0][0];
  13.  
  14.         do {
  15.             isInvalidInput = false;
  16.             System.out.println("Например, \"C:\\Text.txt\".");
  17.             String fileName = in.nextLine();
  18.             try {
  19.                 BufferedReader input = new BufferedReader(new FileReader(fileName));
  20.                 String text = input.readLine();
  21.                 int number = Integer.parseInt(text);
  22.                 myArr = new int[number][number];
  23.                 int i = 0, j = 0;
  24.                 for (i = 0; i < number; i++) {
  25.                     text = input.readLine();
  26.                     String[] arrFilling = text.split(" ");
  27.                     for (j = 0; j < number; j++) {
  28.                         myArr[i][j] = Integer.parseInt(arrFilling[j]);
  29.                     }
  30.                 }
  31.                 for (i = 0; i < number; i++) {
  32.                     for (j = 0; j < number; j++) {
  33.                         System.out.printf(" " + myArr[i][j]);
  34.                     }
  35.                     System.out.println();
  36.                 }
  37.                 input.close();
  38.             } catch (FileNotFoundException e) {
  39.                 System.out.println("Файл с таким именем не найден, повторите попытку");
  40.                 isInvalidInput = true;
  41.             } catch (IOException e) {
  42.                 System.out.println("Такой файл невозможно открыть, повторите попытку");
  43.             } catch (NumberFormatException e) {
  44.                 System.out.println("Ошибка! Файл содержит неверные данные. Пожалуйста, проверьте файл и повторите попытку.");
  45.                 isInvalidInput = true;
  46.             }
  47.         } while (isInvalidInput);
  48.         return myArr;
  49.     }
  50.  
  51.     public static void Swap(int[][] MyFirstArr, int[][] MySecondArr, int LengthOfArr) {
  52.         int i, j, HalfOfLength, NextLine;
  53.         HalfOfLength = LengthOfArr / 2;
  54.         NextLine = (LengthOfArr / 2) + 1;
  55.         for (i = 0; i <= HalfOfLength; i++) {
  56.             for (j = 0; j <= LengthOfArr; j++) {
  57.                 MySecondArr[((NextLine) + i)][j] = MyFirstArr[i][j];
  58.             }
  59.         }
  60.         for (i = NextLine; i <= LengthOfArr; i++) {
  61.             for (j = 0; j <= LengthOfArr; j++) {
  62.                 MySecondArr[i - 2][LengthOfArr - j] = MyFirstArr[i][j];
  63.             }
  64.         }
  65.         System.out.println("Result of matrix swap: ");
  66.         for (i = 0; i <= LengthOfArr; i++) {
  67.             for (j = 0; j <= LengthOfArr; j++) {
  68.                 System.out.print(" " + MySecondArr[i][j]);
  69.             }
  70.             System.out.println();
  71.         }
  72.     }
  73.  
  74.     public static void SaveToFile(int[][] MyArr, int LengthOfArr) throws FileNotFoundException {
  75.         try (FileWriter writer = new FileWriter("C:\\Output.txt", false)) {
  76.             writer.write("Result of matrix swap: ");
  77.             int i,j;
  78.             for (i = 0; i <= LengthOfArr; i++) {
  79.                 writer.append("\n");
  80.                 for (j = 0; j <= LengthOfArr; j++) {
  81.                     String str = Integer.toString(MyArr[i][j]);
  82.                     writer.write(" " + str);
  83.                 }
  84.             }
  85.         } catch (IOException e) {
  86.             System.out.println("Такой файл невозможно открыть");
  87.         }
  88.     }
  89.  
  90.  
  91.     public static void main(String[] args) throws FileNotFoundException {
  92.         System.out.println("Theme: There are a real square matrix of order 2n.");
  93.         System.out.println("It`s necessary to obtain a new matrix, where the sub-matrices are interchanged");
  94.         int[][] ArrA = inputArrFromFile();
  95.         int[][] ArrB = new int[ArrA.length][ArrA.length];
  96.         Swap(ArrA, ArrB, ArrA.length - 1);
  97.         SaveToFile(ArrB, ArrA.length - 1);
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement