Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- private static int currentIndex = -1;
- private static Integer next(String numbers[]) {
- ++currentIndex;
- while (currentIndex < numbers.length && numbers[currentIndex].equals(""))
- ++currentIndex;
- return currentIndex < numbers.length ? Integer.parseInt(numbers[currentIndex]) : null;
- }
- static void condition(){
- System.out.println("Дана действительная квадратная матрица порядка 2n. Цифрами обозначены подматрицы порядка n");
- System.out.println(" ------- ");
- System.out.println("| 1 | 2 |");
- System.out.println(" ------- ");
- System.out.println("| 3 | 4 |");
- System.out.println(" ------- ");
- System.out.println("Получить новую матрицу: ");
- System.out.println(" -------");
- System.out.println("| 4 | 3 |");
- System.out.println(" ------- ");
- System.out.println("| 1 | 2 |");
- System.out.println(" -------");
- }
- static int[][] outputMatrix(int[][] Matrix, int size){
- for (int i = 0; i < size; ++i, System.out.println())
- for (int j = 0; j < size; ++j)
- System.out.print(Matrix[i][j] + " ");
- return Matrix;
- }
- static void swap(int[][] matrix, int size) {
- int temp;
- int center;
- center = size / 2;
- for (int i = 0; i < (size - center); i++) {
- for (int j = 0; j < (size - center); j++) {
- temp = matrix[j][i];
- matrix[j][i] = matrix[j + center][i + center];
- matrix[j + center][i + center] = temp;
- }
- }
- for (int i = size - 1; i >= center ; i--) {
- for (int j = 0; j < (center); j++) {
- temp = matrix[j][i];
- matrix[j][i] = matrix[j + center][i - center];
- matrix[j + center][i - center] = temp;
- }
- }
- System.out.println("Полученная матрица: ");
- outputMatrix(matrix,size);
- }
- static void outputInFile(int[][] matrix, int size) throws IOException{
- Scanner in = new Scanner(System.in);
- int i,j;
- String outputFileName;
- System.out.println("Введите имя файла в который хотите вывести данные: ");
- outputFileName = in.nextLine();
- outputFileName = outputFileName + ".txt";
- File newFile = new File(outputFileName);
- try
- {
- boolean created = newFile.createNewFile();
- if(created)
- System.out.println("Файл успешно создан.");
- }
- catch(IOException ex){
- System.out.println(ex.getMessage());
- }
- FileWriter out = new FileWriter(outputFileName);
- out.write("\n Полученная матрица: ");
- for (i = 0; i < size; ++i) {
- for (j = 0; j < size; ++j) {
- out.write( matrix[i][j] + "\n ");
- }
- out.write("\n ");
- }
- out.close();
- }
- public static void main(String[] args) throws IOException {
- Scanner in = new Scanner(System.in);
- String NameOfFile;
- Boolean CorrectFile;
- condition();
- CorrectFile = true;
- System.out.println("Введите имя файла, с которого хотите считать информацию: ");
- NameOfFile = in.nextLine();
- NameOfFile = NameOfFile + ".txt";
- FileInputStream inFile = new FileInputStream(NameOfFile);
- byte[] str = new byte[inFile.available()];
- inFile.read(str);
- String text = new String(str);
- String[] numbers = text.split("\\D");
- int i, j;
- int size = next(numbers);
- if (size % 2 == 0) {
- int[][] matrix = new int[size][size];
- for (i = 0; i < size; ++i)
- for (j = 0; j < size; ++j)
- matrix[i][j] = next(numbers);
- System.out.println("Исходная матрица порядка 2n: ");
- outputMatrix(matrix, size);
- swap(matrix, size);
- outputInFile(matrix, size);
- }else{
- System.out.println("Ошибка! Измените файл, он должен содержать только цифры,\n" +
- " и размер матрицы должен равняться 2n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement