Advertisement
ryabov

Untitled

Oct 27th, 2021
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.97 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.io.*;
  3. public class Laba2_3 {
  4.     static Scanner scan = new Scanner(System.in);
  5.     final static char MIN_SIZE = 2;
  6.     final static char MAX_SIZE = 5;
  7.     final static int MIN_VALUE = -100;
  8.     final static int MAX_VALUE = 100;
  9.     static char chooseIO(String text){
  10.         boolean isIncorrect;
  11.         String choice;
  12.         System.out.println("Выберите способ " + text + ": \nf - file\nc - console");
  13.         do{
  14.             isIncorrect = false;
  15.             choice = scan.nextLine();
  16.             if (choice.length() != 1){
  17.                 System.err.println("Введите корректные данные!");
  18.                 isIncorrect = true;
  19.             }
  20.             if (!isIncorrect && !(choice.charAt(0) == 'f' || choice.charAt(0) == 'c')){
  21.                 System.err.println("Введите корректные данные!");
  22.                 isIncorrect = true;
  23.             }
  24.         } while (isIncorrect);
  25.         return choice.charAt(0);
  26.     }
  27.     static String takePath(){
  28.         boolean isIncorrect;
  29.         String path;
  30.         System.out.println("Введите путь к файлу");
  31.         do {
  32.             isIncorrect = false;
  33.             path = scan.nextLine();
  34.             if (!path.endsWith(".txt")) {
  35.                 System.err.println("Введите путь с расширением .txt");
  36.                 isIncorrect = true;
  37.             }
  38.         } while (isIncorrect);
  39.         return path;
  40.     }
  41.     static String inputPath(){
  42.         String path;
  43.         boolean isIncorrect;
  44.         do{
  45.             isIncorrect = false;
  46.             path = takePath();
  47.             File file = new File(path);
  48.             if (!file.exists()) {
  49.                 isIncorrect = true;
  50.                 System.err.println("Введите существующий файл!");
  51.             }
  52.         } while (isIncorrect);
  53.         return path;
  54.     }
  55.     static int inputSizeConsole(){
  56.         System.out.println("Введите порядок матрицы");
  57.         boolean isIncorrect;
  58.         int size = 0;
  59.         do{
  60.             isIncorrect = false;
  61.             try {
  62.                 size = Integer.parseInt(scan.nextLine());
  63.             }
  64.             catch (NumberFormatException e) {
  65.                 System.err.println("Введите натуральное число!");
  66.                 isIncorrect = true;
  67.             }
  68.             if (!isIncorrect && (size < MIN_SIZE || size > MAX_SIZE)){
  69.                 System.err.println("Введите число от 2 до 5!");
  70.                 isIncorrect = true;
  71.             }
  72.         } while (isIncorrect);
  73.         return size;
  74.     }
  75.     static double inputElements(){
  76.         boolean isIncorrect;
  77.         double element = 0;
  78.         do{
  79.             isIncorrect = false;
  80.             try {
  81.                 element = Integer.parseInt(scan.nextLine());
  82.             }
  83.             catch (NumberFormatException e) {
  84.                 System.err.println("Введите натуральное число!");
  85.                 isIncorrect = true;
  86.             }
  87.             if (!isIncorrect && (element < MIN_VALUE || element > MAX_VALUE)){
  88.                 isIncorrect = true;
  89.                 System.err.println("Введите число в диапазоне от -100 до 100");
  90.             }
  91.         } while (isIncorrect);
  92.         return element;
  93.     }
  94.     static double[][] inputMatrixConsole(int matrixSize){
  95.         double[][] matrix = new double[matrixSize][matrixSize];
  96.         System.out.println("Введите элементы матрицы");
  97.         for(int i = 0; i < matrixSize; i++){
  98.             for(int j = i; j < matrixSize; j++){
  99.                 matrix[i][j] = inputElements();
  100.             }
  101.         }
  102.         return matrix;
  103.     }
  104.     static double[] inputVectorConsole(int size) {
  105.         double[] vectorLine = new double[size];
  106.         System.out.println("Введите вектор свободных членов");
  107.         for (int i = 0; i < size; i++) {
  108.             vectorLine[i] = inputElements();
  109.         }
  110.         return vectorLine;
  111.     }
  112.     static int inputSizeFile(String path) {
  113.         boolean isIncorrect;
  114.         int matrixSize = 0;
  115.         do {
  116.             isIncorrect = false;
  117.             try {
  118.                 BufferedReader reader = new BufferedReader(new FileReader(path));
  119.                     matrixSize = Integer.parseInt(reader.readLine());
  120.  
  121.                 if (matrixSize < MIN_SIZE || matrixSize > MAX_SIZE) {
  122.                     System.err.println("Порядок матрицы должен находиться в диапазоне от 2 до 6!");
  123.                     isIncorrect = true;
  124.                     path = inputPath();
  125.                 }
  126.                 reader.close();
  127.             } catch (Exception e) {
  128.                 System.err.println("Произошла ошибка");
  129.                 isIncorrect = true;
  130.                 path = inputPath();
  131.             }
  132.         } while (isIncorrect);
  133.         return matrixSize;
  134.     }
  135.     static double[][] inputMatrixFile(int matrixSize, String path){
  136.         double[][] matrix = new double[matrixSize][matrixSize];
  137.         boolean isIncorrect;
  138.         do{
  139.             isIncorrect = false;
  140.             try {
  141.                 Scanner filereader = new Scanner(new File(path));
  142.                 filereader.nextLine();
  143.                 try {
  144.                     for (int i = 0; i < matrixSize; i++) {
  145.                         for (int j = i; j < matrixSize; j++) {
  146.                             matrix[i][j] = filereader.nextInt();
  147.                         }
  148.                         filereader.nextLine();
  149.                     }
  150.                 } catch (Exception e){
  151.                     isIncorrect = true;
  152.                     System.err.println("Ошибка");
  153.                     path = inputPath();
  154.                 }
  155.             } catch (IOException e) {
  156.                 System.err.println("Ошибка");
  157.                 isIncorrect = true;
  158.             }
  159.         }while(isIncorrect);
  160.         return matrix;
  161.     }
  162.     static double[] inputVectorFile(int size, String path){
  163.         double[] vectorLine = new double[size];
  164.         boolean isIncorrect;
  165.         do{
  166.             isIncorrect = false;
  167.             try{
  168.                 Scanner filereader = new Scanner(new File(path));
  169.                 for (int k = 0; k < size + 1; k++){
  170.                     filereader.nextLine();
  171.                 }
  172.                 for (int i = 0; i < size; i++){
  173.                     try{
  174.                         vectorLine[i] = filereader.nextInt();
  175.                     } catch(Exception e){
  176.                         isIncorrect = true;
  177.                     }
  178.                 }
  179.                 if(isIncorrect) {
  180.                     System.err.println("Ошибка");
  181.                     path = inputPath();
  182.                 }
  183.             } catch(IOException e){
  184.                 System.err.println("Ошибка");
  185.                 isIncorrect = true;
  186.             }
  187.  
  188.         }while(isIncorrect);
  189.         return  vectorLine;
  190.     }
  191.     static boolean checkZero(int matrixSize, double[][] matrix){
  192.         for(int i = 0; i < matrixSize; i++){
  193.             for(int j = i; j < matrixSize; j++){
  194.                 if (matrix[i][j] == 0) {
  195.                     return true;
  196.                 }
  197.             }
  198.         }
  199.         return false;
  200.     }
  201.     static double[] findRoots(int size, double[] vectorLine, double[][] matrix){
  202.         double[] roots = new double[size];
  203.         for (int i = size - 1; i > -1; i--) {
  204.             double sum = 0;
  205.             for (int j = i + 1; j < size; j++) {
  206.                 sum = (matrix[i][j] * roots[j]) + sum;
  207.             }
  208.                 roots[i] = (vectorLine[i] - sum) / matrix[i][i];
  209.         }
  210.         return roots;
  211.     }
  212.     static void outputRootsConsole(int size, double[] roots){
  213.         for(int i = 0; i < size; i++){
  214.             double root = roots[i];
  215.             System.out.println("x" + (i + 1) + " = " + root);
  216.         }
  217.     }
  218.     static void outputRootsFile(int size, double[] roots, String path){
  219.         boolean isIncorrect;
  220.         do{
  221.             isIncorrect = false;
  222.             try {
  223.                 FileWriter writer = new FileWriter(path);
  224.                 for(int i = 0; i < size; i++){
  225.                     writer.write("x" + (i+1) + "=" + roots[i] + "\n");
  226.                 }
  227.                 writer.close();
  228.             } catch (IOException e) {
  229.                 isIncorrect = true;
  230.                 System.err.println("Ошибка");
  231.                 path = takePath();
  232.             }
  233.         } while(isIncorrect);
  234.     }
  235.     public static void main(String[] args) {
  236.         String path;
  237.         int size;
  238.         double[][] matrix;
  239.         double[] vectorLine;
  240.         System.out.println("Программа выполняет обратный ход Гаусса. Вводить только элементы матрицы системы, стоящие выше главной диагонали и вектор свободных членов.");
  241.         char choice = chooseIO("ввода матрицы и вектора свободных членов");
  242.         if (choice == 'c') {
  243.             size = inputSizeConsole();
  244.             matrix = inputMatrixConsole(size);
  245.             vectorLine = inputVectorConsole(size);
  246.         }
  247.         else {
  248.             path = inputPath();
  249.             size = inputSizeFile(path);
  250.             matrix = inputMatrixFile(size, path);
  251.             vectorLine = inputVectorFile(size, path);
  252.         }
  253.         if(checkZero(size, matrix))
  254.             System.out.println("Нет решений");
  255.         else {
  256.             double[] roots = findRoots(size, vectorLine, matrix);
  257.             choice = chooseIO("вывода корней");
  258.             if (choice == 'c') {
  259.                 outputRootsConsole(size, roots);
  260.             } else {
  261.                 path = takePath();
  262.                 outputRootsFile(size, roots, path);
  263.             }
  264.         }
  265.         scan.close();
  266.     }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement