Advertisement
MadCortez

Untitled

Oct 27th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.63 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. class MyClass {
  5.  
  6.     static Scanner scanner = new Scanner(System.in);
  7.  
  8.     public static void printTask() {
  9.         System.out.println("Данная программа выполняет «прямой ход» в решении системы линейных алгебраических уравнений методом Гаусса");
  10.     }
  11.  
  12.     public static double[][] gauss(double[][] matrix) {
  13.         int n = matrix.length;
  14.         for (int k = 0; k < n; k++)
  15.             for (int j = k + 1; j < n; j++) {
  16.                 double temp = matrix[j][k] / matrix[k][k];
  17.                 for (int i = k; i < n; i++)
  18.                     matrix[j][i] = matrix[j][i] - temp * matrix[k][i];
  19.                 matrix[j][n] = matrix[j][n] - temp * matrix[k][n];
  20.             }
  21.         return matrix;
  22.     }
  23.  
  24.     public static void userInputArrayFromConsole(int n) throws FileNotFoundException {
  25.         final int MIN_VALUE = -500;
  26.         final int MAX_VALUE = 500;
  27.         double[][] matrix = new double[n][];
  28.         System.out.println("Введите коэффициенты в диапазоне " + MIN_VALUE + ".." + MAX_VALUE + ":");
  29.         for (int i = 0; i < n; i++) {
  30.             matrix[i] = new double[n + 1];
  31.             for (int j = 0; j < n; j++) {
  32.                 matrix[i][j] = inputValue(MIN_VALUE, MAX_VALUE);
  33.             }
  34.         }
  35.         System.out.println("Введите свободные члены в диапазоне " + MIN_VALUE + ".." + MAX_VALUE + ":");
  36.         for (int i = 0; i < n; i++)
  37.             matrix[i][n] = inputValue(MIN_VALUE, MAX_VALUE);
  38.         outputMethod(gauss(matrix));
  39.     }
  40.  
  41.     public static PrintWriter userOutputPath() throws FileNotFoundException {
  42.         String path;
  43.         PrintWriter file;
  44.         System.out.println("Введите абсолютный путь к файлу для вывода результата");
  45.         String zero = scanner.nextLine();
  46.         path = scanner.nextLine();
  47.         file = new PrintWriter(path);
  48.         return file;
  49.     }
  50.  
  51.     public static Scanner userInputPath() throws FileNotFoundException {
  52.         String path;
  53.         boolean isNotValid;
  54.         Scanner file = null;
  55.         String zero = scanner.nextLine();
  56.         do {
  57.             isNotValid = false;
  58.             System.out.println("Введите абсолютный путь к файлу с входными данными");
  59.             path = scanner.nextLine();
  60.             try {
  61.                 file = new Scanner(new File(path));
  62.             } catch (FileNotFoundException e) {
  63.                 isNotValid = true;
  64.                 System.out.println("Файл не найден");
  65.             }
  66.         } while (isNotValid);
  67.         return file;
  68.     }
  69.  
  70.     public static void userInputFromFile() throws FileNotFoundException {
  71.         int n;
  72.         Scanner file = userInputPath();
  73.         n = file.nextInt();
  74.         double[][] matrix = new double[n][];
  75.         for (int i = 0; i < n; i++) {
  76.             matrix[i] = new double[n + 1];
  77.             for (int j = 0; j < n; j++)
  78.                  matrix[i][j] = file.nextInt();
  79.         }
  80.         for (int i = 0; i < n; i++)
  81.             matrix[i][n] = file.nextInt();
  82.         outputMethod(gauss(matrix));
  83.     }
  84.  
  85.     public static void printWithPath(double[][] matrix) throws FileNotFoundException {
  86.         PrintWriter out = userOutputPath();
  87.         for (int i = 0; i < matrix.length; i++) {
  88.             for (int j = 0; j < matrix[i].length; j++)
  89.                 out.printf("%.0f ", matrix[i][j]);
  90.             out.println();
  91.         }
  92.         for (int i = 0; i < matrix.length; i++)
  93.             out.printf("%.0f\n", matrix[i][matrix[i].length - 1]);
  94.         out.close();
  95.         System.out.println("Результат работы помещён в файл");
  96.     }
  97.  
  98.     public static void printWithoutPath(double[][] matrix) {
  99.         System.out.println("После «прямого хода»");
  100.         for (int i = 0; i < matrix.length; i++) {
  101.             for (int j = 0; j < matrix[i].length - 1; j++)
  102.                 System.out.printf("%.0f ", matrix[i][j]);
  103.             System.out.println();
  104.         }
  105.         for (int i = 0; i < matrix.length; i++)
  106.             System.out.printf("%.0f\n", matrix[i][matrix[i].length - 1]);
  107.     }
  108.  
  109.     public static void outputMethod(double[][] matrix) throws FileNotFoundException {
  110.         int method;
  111.         System.out.println("Куда хотите вывести результат?");
  112.         System.out.println("1 - в консоль");
  113.         System.out.println("2 - в файл");
  114.         do {
  115.             method = scanner.nextInt();
  116.             switch (method) {
  117.                 case 1: { printWithoutPath(matrix); break; }
  118.                 case 2: { printWithPath(matrix); break; }
  119.                 default: System.out.println("Введите корректный способ вывода");
  120.             }
  121.         } while (method != 2 && method != 1);
  122.     }
  123.  
  124.     public static void inputMethod() throws FileNotFoundException {
  125.         int method;
  126.         System.out.println("Каким способом хотите ввести данные?");
  127.         System.out.println("1 - с помощью консоли");
  128.         System.out.println("2 - с помощью файла");
  129.         do {
  130.             method = scanner.nextInt();
  131.             switch (method) {
  132.                 case 1: { userInputFromConsole(); break; }
  133.                 case 2: { userInputFromFile(); break; }
  134.                 default: System.out.println("Введите корректный способ ввода");
  135.             }
  136.         } while (method != 2 && method != 1);
  137.     }
  138.  
  139.     public static void userInputFromConsole() throws FileNotFoundException {
  140.     final int MIN_SIZE = 1;
  141.     final int MAX_SIZE = 20;
  142.         int n;
  143.         System.out.print("Введите порядок матрицы в диапазоне " + MIN_SIZE + ".." + MAX_SIZE + ": ");
  144.         n = (int) Math.round(inputValue(MIN_SIZE, MAX_SIZE));
  145.         userInputArrayFromConsole(n);
  146.     }
  147.  
  148.     public static double inputValue(int min, int max) {
  149.         double currentValue;
  150.         boolean isNotValid = true;
  151.         do {
  152.             currentValue = scanner.nextInt();
  153.             if (currentValue >= min && currentValue <= max)
  154.                 isNotValid = false;
  155.             else
  156.                 System.out.println("Введите число в заданном диапазоне");
  157.         } while (isNotValid);
  158.         return currentValue;
  159.     }
  160.  
  161.     public static void main(String[] args) throws Exception {
  162.         printTask();
  163.         inputMethod();
  164.     }
  165. }
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement