Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.Laba2_4;
- import java.io.*;
- import java.util.Scanner;
- public class Main {
- public static Scanner in = new Scanner(System.in);
- public static int[][] inputArrFromFile() {
- boolean isInvalidInput = false;
- int[][] myArr = new int[0][0];
- do {
- isInvalidInput = false;
- System.out.println("Например, \"C:\\Text.txt\".");
- String fileName = in.nextLine();
- try {
- BufferedReader input = new BufferedReader(new FileReader(fileName));
- String text = input.readLine();
- int number = Integer.parseInt(text);
- myArr = new int[number][number];
- int i = 0, j = 0;
- for (i = 0; i < number; i++) {
- text = input.readLine();
- String[] arrFilling = text.split(" ");
- for (j = 0; j < number; j++) {
- myArr[i][j] = Integer.parseInt(arrFilling[j]);
- }
- }
- for (i = 0; i < number; i++) {
- for (j = 0; j < number; j++) {
- System.out.printf(" " + myArr[i][j]);
- }
- System.out.println();
- }
- input.close();
- } catch (FileNotFoundException e) {
- System.out.println("Файл с таким именем не найден, повторите попытку");
- isInvalidInput = true;
- } catch (IOException e) {
- System.out.println("Такой файл невозможно открыть, повторите попытку");
- } catch (NumberFormatException e) {
- System.out.println("Ошибка! Файл содержит неверные данные. Пожалуйста, проверьте файл и повторите попытку.");
- isInvalidInput = true;
- }
- } while (isInvalidInput);
- return myArr;
- }
- public static void Swap(int[][] MyFirstArr, int[][] MySecondArr, int LengthOfArr) {
- int i, j, HalfOfLength, NextLine;
- HalfOfLength = LengthOfArr / 2;
- NextLine = (LengthOfArr / 2) + 1;
- for (i = 0; i <= HalfOfLength; i++) {
- for (j = 0; j <= LengthOfArr; j++) {
- MySecondArr[((NextLine) + i)][j] = MyFirstArr[i][j];
- }
- }
- for (i = NextLine; i <= LengthOfArr; i++) {
- for (j = 0; j <= LengthOfArr; j++) {
- MySecondArr[i - 2][LengthOfArr - j] = MyFirstArr[i][j];
- }
- }
- System.out.println("Result of matrix swap: ");
- for (i = 0; i <= LengthOfArr; i++) {
- for (j = 0; j <= LengthOfArr; j++) {
- System.out.print(" " + MySecondArr[i][j]);
- }
- System.out.println();
- }
- }
- public static void SaveToFile(int[][] MyArr, int LengthOfArr) throws FileNotFoundException {
- try (FileWriter writer = new FileWriter("C:\\Output.txt", false)) {
- writer.write("Result of matrix swap: ");
- int i,j;
- for (i = 0; i <= LengthOfArr; i++) {
- writer.append("\n");
- for (j = 0; j <= LengthOfArr; j++) {
- String str = Integer.toString(MyArr[i][j]);
- writer.write(" " + str);
- }
- }
- } catch (IOException e) {
- System.out.println("Такой файл невозможно открыть");
- }
- }
- public static void main(String[] args) throws FileNotFoundException {
- System.out.println("Theme: There are a real square matrix of order 2n.");
- System.out.println("It`s necessary to obtain a new matrix, where the sub-matrices are interchanged");
- int[][] ArrA = inputArrFromFile();
- int[][] ArrB = new int[ArrA.length][ArrA.length];
- Swap(ArrA, ArrB, ArrA.length - 1);
- SaveToFile(ArrB, ArrA.length - 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement