Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- public class Main {
- public static String inputPath(){
- Scanner scan = new Scanner(System.in);
- boolean isIncorrect;
- String path;
- do {
- isIncorrect = false;
- path = scan.nextLine();
- File file = new File(path);
- if (!file.exists()){
- System.out.println("Файл не найден! Введите абсолютную ссылку на файл");
- isIncorrect = true;
- }
- }while (isIncorrect);
- return path;
- }
- public static int inputOrderFromFile(String path) throws FileNotFoundException {
- Scanner scan = new Scanner(new File(path));
- int order;
- boolean flag = true;
- System.out.println("Порядок матрицы считывается из файла...");
- try {
- order = scan.nextInt();
- } catch (Exception e) {
- flag = false;
- order = inputOrderFromConsole();
- }
- if (flag && (order < 2 || order > 6)) {
- order = inputOrderFromConsole();
- }
- return order;
- }
- public static int inputOrderFromConsole(){
- Scanner scan = new Scanner(System.in);
- System.out.println("В вашем файле вписан некорректный порядок матрицы. Введите порядок из косоли!");
- int order = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- order = Integer.parseInt(scan.nextLine());
- } catch (Exception e) {
- System.out.println("Введите целое число!");
- isIncorrect = true;
- }
- if (isIncorrect == false && (order < 2 || order > 6)) {
- System.out.println("Некорректные данные. Введите порядок из диапазона [2;6]");
- isIncorrect = true;
- }
- }while (isIncorrect);
- return order;
- }
- public static void inputMatrixFromFile(double[][] matrix, String path) throws FileNotFoundException {
- Scanner scan = new Scanner(new File(path));
- scan.nextLine();
- System.out.println("Матрица считывается из файла...");
- for (int i = 0; i < matrix.length; i++){
- for (int j = 0; j < matrix.length; j++){
- try {
- matrix[i][j] = scan.nextDouble();
- }catch (Exception e){
- System.out.println("В вашем файле некорректный элемент матрицы a" + (i + 1) + (j + 1) + ". Введите его из консоли!");
- matrix[i][j] = inputMatrixFromConsole();
- scan.next();
- }
- }
- }
- }
- public static double inputMatrixFromConsole(){
- Scanner scan = new Scanner(System.in);
- double element = 0;
- boolean isIncorrect;
- do {
- isIncorrect = false;
- try {
- element = Double.parseDouble(scan.nextLine());
- } catch (Exception e) {
- System.out.println("Введите число!");
- isIncorrect = true;
- }
- }while (isIncorrect);
- return element;
- }
- public static void printMatrix(double[][] matrix){
- for (int i = 0; i < matrix.length; i++){
- System.out.println();
- for (int j = 0; j < matrix.length; j++){
- System.out.print(matrix[i][j] + " ");
- }
- }
- System.out.println();
- }
- public static void doInsertionSort(double[][] matrix){
- for (int i = 0; i < matrix.length; i++){
- for (int j = 0; j < matrix.length; j++){
- double current = matrix[i][j];
- int k = j;
- while(k > 0 && current < matrix[i][k - 1]) {
- matrix[i][k] = matrix[i][k - 1];
- k--;
- }
- matrix[i][k] = current;
- }
- }
- }
- public static void doBubbleSortForStrings(double[][] matrix){
- for (int k = 0; k < matrix.length; k++) {
- for (int i = 0; i < matrix.length - 1 ; i++ ) {
- if ( matrix[i][matrix.length - 1] > matrix[i + 1][matrix.length - 1]) {
- for (int j = 0 ; j < matrix.length; j++) {
- double temp = matrix[i][j];
- matrix[i][j] = matrix[i + 1][j];
- matrix[i + 1][j] = temp;
- }
- }
- }
- }
- }
- public static void outputToFile(double[][] matrix, String path) throws IOException {
- FileWriter writer = new FileWriter(path);
- for (int i = 0; i < matrix.length; i++){
- for (int j = 0; j < matrix.length; j++){
- writer.write(matrix[i][j] + " ");
- }
- writer.write("\n");
- }
- writer.close();
- System.out.println("Данные успешно записаны в файл!");
- }
- public static void main(String[] args) throws IOException {
- System.out.println("Дана матрица A порядка n. \nПрограмма отсортировывает строки матрицы \nв порядке возрастания наибольших элементов в каждой строке.\n(Порядок матрицы должен быть в диапазоне целых чисел от 2 до 6)\n");
- System.out.println("Введите абсолютную ссылку на файл ввода");
- String pathInput = inputPath();
- System.out.println("Введите абсолютную ссылку на файл вывода");
- String pathOutput = inputPath();
- int order = inputOrderFromFile(pathInput);
- double[][] matrix = new double[order][order];
- inputMatrixFromFile(matrix, pathInput);
- System.out.print("Исходная матрица:");
- printMatrix(matrix);
- doInsertionSort(matrix);
- System.out.print("Отсортированная матрица:");
- printMatrix(matrix);
- outputToFile(matrix, pathOutput);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement