VadimThink

Ы

Oct 26th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. package com.Laba2_3;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.util.Scanner;
  9.  
  10. public class Main {
  11.     public static Scanner in = new Scanner(System.in);
  12.  
  13.     static double[][] inputArrFromFile() {
  14.         boolean isInvalidInput;
  15.         double[][] matrice = new double[0][0];
  16.  
  17.         do {
  18.             isInvalidInput = false;
  19.             System.out.println("Enter the path to the file, for example 'C:\\Users\\Think\\Desktop\\Input.txt' ");
  20.             String fileName = in.nextLine();
  21.             try {
  22.                 BufferedReader input = new BufferedReader(new FileReader(fileName));
  23.                 String text = input.readLine();
  24.                 int number = Integer.parseInt(text);
  25.                 matrice = new double[number + 1][number + 1];
  26.                 for (int i = 0; i < number; i++) {
  27.                     text = input.readLine();
  28.                     String[] arrFilling = text.split(" ");
  29.                     for (int j = 0; j < number + 1; j++) {
  30.                         matrice[i][j] = Double.parseDouble(arrFilling[j]);
  31.                     }
  32.                 }
  33.                 for (int i = 0; i < number; i++) {
  34.                     for (int j = 0; j < number + 1; j++) {
  35.                         System.out.printf(" " + matrice[i][j]);
  36.                     }
  37.                     System.out.println();
  38.                 }
  39.                 input.close();
  40.             } catch (FileNotFoundException e) {
  41.                 System.out.println("A file with the same name was not found, try again");
  42.                 isInvalidInput = true;
  43.             } catch (IOException e) {
  44.                 System.out.println("This file cannot be opened, try again");
  45.             } catch (NumberFormatException e) {
  46.                 System.out.println("Error! The file contains invalid data. Please check the file and ad text.");
  47.                 isInvalidInput = true;
  48.             }
  49.         } while (isInvalidInput);
  50.         return matrice;
  51.     }
  52.  
  53.     static double[][] gauss(double matrice[][], int length) {
  54.         for (int i = 0; i < length + 1; i++) {
  55.             matrice[1][i] = (matrice[0][i] * 1.5) + matrice[1][i];
  56.             matrice[2][i] = (matrice[0][i] + matrice[2][i]);
  57.             matrice[2][i] = (matrice[2][i] - matrice[1][i] * 4);
  58.             matrice[1][i] = matrice[1][i] * 2;
  59.         }
  60.         return matrice;
  61.     }
  62.  
  63.     static void output(double[][] matrice, int length) {
  64.         System.out.println("The result is");
  65.         for (int i = 0; i < length - 1; i++) {
  66.             for (int j = 0; j < length; j++) {
  67.                 System.out.print(" " + matrice[i][j]);
  68.             }
  69.             System.out.println();
  70.  
  71.         }
  72.     }
  73.  
  74.     public static void saveToFile(double[][] matrice, int length) {
  75.         System.out.println("Enter the path to the output file, for example 'C:\\Users\\Think\\Desktop\\Output.txt' ");
  76.         String fileName = in.nextLine();
  77.         try (FileWriter writer = new FileWriter(fileName, false)) {
  78.             writer.write("The result is ");
  79.             int i, j;
  80.             for (i = 0; i < length - 1; i++) {
  81.                 writer.append("\n");
  82.                 for (j = 0; j < length; j++) {
  83.                     String str = Double.toString(matrice[i][j]);
  84.                     writer.write(" " + str);
  85.                 }
  86.             }
  87.         } catch (IOException e) {
  88.             System.out.println("File couldn`t be opened");
  89.         }
  90.     }
  91.  
  92.  
  93.     public static void main(String[] args) {
  94.         System.out.println("This program performs a direct move by the Gauss method for a system of linear equations");
  95.         double[][] arrA = inputArrFromFile();
  96.         double[][] arrB = gauss(arrA, arrA.length - 1);
  97.         saveToFile(arrB, arrB.length);
  98.         output(arrB, arrB.length);
  99.     }
  100. }
Add Comment
Please, Sign In to add comment