Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.util.Scanner;
- public class Main {
- static void showVector(double[] vector) {
- for (double el : vector)
- System.out.print(el + " ");
- System.out.println("");
- }
- static String showMatrix(double[][] matrix) {
- String matrixString = "";
- for (double[] row : matrix) {
- for (double el : row) {
- System.out.print(el + " ");
- matrixString += el + " ";
- }
- System.out.println("");
- }
- return matrixString;
- }
- static String takeVectorType(RandomAccessFile raf) throws IOException {
- raf.seek(0);
- String vectorType = "";
- boolean notCorrect = true;
- while (vectorType != null && notCorrect) {
- vectorType = raf.readLine();
- if (vectorType != null) {
- vectorType = new String(vectorType.getBytes("ISO-8859-1"), "windows-1251").toLowerCase();
- if (vectorType.equals("вектор-строка") || vectorType.equals("вектор-столбец"))
- notCorrect = false;
- }
- }
- return vectorType;
- }
- static double[] takeVector(RandomAccessFile raf) throws IOException {
- String line = "";
- double[] vector = {};
- int vectorLength = Integer.parseInt(raf.readLine());
- vector = takeRow(raf.readLine(), vectorLength);
- return vector;
- }
- static double[] takeRow(String str, int size) {
- double[] row = new double[size];
- String[] outputList = str.split("\\s");
- if (outputList.length == size) {
- for (int i = 0; i < size; i++)
- if (outputList[i].matches("\\d+"))
- row[i] = Double.parseDouble(outputList[i]);
- } else
- System.out.println("Количество столбцов, введенных в файле, должно совпадать с количеством столбцов в матрице");
- return row;
- }
- static double[][] takeMatrixFromFile(RandomAccessFile raf) throws IOException {
- int matrixRows = Integer.parseInt(raf.readLine());
- int matrixCols = Integer.parseInt(raf.readLine());
- double[][] matrix = new double[matrixRows][matrixCols];
- for (int i = 0; i < matrixRows; i++) {
- String currString = raf.readLine();
- matrix[i] = takeRow(currString, matrixCols);
- }
- return matrix;
- }
- static double[][] takeProduct(double[][] matrix, double[] vector, int vectorLength) {
- double[][] product = new double[vectorLength][matrix[0].length];
- for (int i = 0; i < product.length; i++) {
- for (int j = 0; j < matrix.length; j++) {
- for (int k = 0; k < matrix[j].length; k++) {
- product[i][k] += vector[vectorLength == 1 ? j : i] * matrix[j][k];
- }
- }
- }
- return product;
- }
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- boolean notCorrect = true;
- System.out.println("Данная программа находит произведение вектора на матрицу\n");
- System.out.println("Введите путь до файла");
- String filePath = scan.nextLine();
- int matrixRows = 0, matrixCols = 0, vectorLength = 0;
- String vectorType = "";
- double[][] matrix = {};
- double[] vector = {};
- try {
- RandomAccessFile raf = new RandomAccessFile(filePath, "r");
- matrix = takeMatrixFromFile(raf);
- vectorType = takeVectorType(raf);
- vector = takeVector(raf);
- raf.close();
- notCorrect = false;
- } catch (IOException err) {
- System.out.println("Произошла ошибка при чтении файла. Убедитесь, что такой файл существует, либо проверьте имя файла.");
- }
- if (!notCorrect) {
- System.out.println("Матрица:");
- showMatrix(matrix);
- System.out.println("Тип вектора: " + vectorType);
- System.out.println("Вектор:");
- showVector(vector);
- matrixRows = matrix.length;
- vectorLength = vector.length;
- if ((vectorType.equals("вектор-столбец") && matrixRows != 1) || (vectorType.equals("вектор-строка") && matrixRows != vectorLength)) {
- System.out.println("Число строк в матрице должно быть равно числу столбцов в векторе");
- } else {
- vectorLength = vectorType.equals("вектор-столбец") ? vectorLength : 1;
- double[][] product = takeProduct(matrix, vector, vectorLength);
- System.out.println("Результат: ");
- String result = showMatrix(product);
- try (FileWriter fw = new FileWriter("output.txt", false)) {
- fw.write("Результат: " + result);
- } catch (IOException err) {
- System.out.println("Произошла ошибка при попытке записи результата в файл");
- }
- System.out.println("Результат сохранен в файл output.txt");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement