Advertisement
Alyks

Untitled

Nov 17th, 2019
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. vector<string> split(string str, string delimiter) {
  9.     vector<string> result;
  10.     int pos = 0;
  11.     while(pos != -1) {
  12.         pos = str.find(delimiter);
  13.         result.push_back(str.substr(0, pos));
  14.         str.erase(0, pos + delimiter.length());
  15.     }
  16.     return result;
  17. }
  18.  
  19. void showVector(vector<double> vector) {
  20.     for(double el : vector)
  21.         cout << el << " ";
  22.     cout << endl;
  23. }
  24.  
  25. string showMatrix(vector<vector<double>> matrix) {
  26.     string matrixString = "";
  27.     for(vector<double> row : matrix) {
  28.         for(double el : row) {
  29.             cout << el << " ";
  30.             matrixString += to_string(el) + " ";
  31.         }
  32.         cout << endl;
  33.     }
  34.     return matrixString;
  35. }
  36.  
  37. string takeVectorType(ifstream &inputFile) {
  38.     inputFile.seekg(0);
  39.     string vectorType = "";
  40.     bool notCorrect = true;
  41.     while(notCorrect && getline(inputFile, vectorType)) {
  42.         if(vectorType == "вектор-строка" || vectorType == "вектор-столбец")
  43.             notCorrect = false;
  44.     }
  45.     return vectorType;
  46. }
  47.  
  48. vector<double> takeRow(string str, int size) {
  49.     vector<double> row(size);
  50.     vector<string> outputList = split(str, " ");
  51.     if(outputList.size() == size) {
  52.         for(int i = 0; i < size; i++)
  53.             if(isdigit(outputList[i][0]))
  54.                 row[i] = stod(outputList[i]);
  55.     } else
  56.         cout << "Количество столбцов, введенных в файле, должно совпадать с количеством столбцов в матрице" << endl;
  57.     return row;
  58. }
  59.  
  60. vector<double> takeVector(ifstream &inputFile) {
  61.     string line = "";
  62.     vector<double> vec;
  63.  
  64.     getline(inputFile, line);
  65.     int vectorLength = stoi(line);
  66.     getline(inputFile, line);
  67.     vec = takeRow(line, vectorLength);
  68.  
  69.     return vec;
  70. }
  71.  
  72. vector<vector<double>> takeMatrixFromFile(ifstream &inputFile) {
  73.     int matrixRows = 0, matrixCols = 0;
  74.     string line = "";
  75.     getline(inputFile, line);
  76.     matrixRows = stoi(line);
  77.     getline(inputFile, line);
  78.     matrixCols = stoi(line);
  79.  
  80.     vector<vector<double>> matrix(matrixRows, vector<double>(matrixCols));
  81.     for(int i = 0; i < matrixRows; i++) {
  82.         getline(inputFile, line);
  83.         matrix[i] = takeRow(line, matrixCols);
  84.     }
  85.  
  86.     return matrix;
  87. }
  88.  
  89. vector<vector<double>> takeProduct(vector<vector<double>> matrix, vector<double> vec, int vectorLength) {
  90.     vector<vector<double>> product(vectorLength, vector<double>(matrix[0].size(), 0));
  91.  
  92.     for(int i = 0; i < product.size(); i++) {
  93.         for(int j = 0; j < matrix.size(); j++) {
  94.             for(int k = 0; k < matrix[j].size(); k++) {
  95.                 product[i][k] += vec[vectorLength == 1 ? j : i] * matrix[j][k];
  96.             }
  97.         }
  98.     }
  99.  
  100.     return product;
  101. }
  102.  
  103. int main() {
  104.     cout << "Данная программа находит произведение вектора на матрицу\n" << endl;
  105.     bool notCorrect = true;
  106.     string filePath;
  107.     cout << "Введите путь до файла" << endl;
  108.     cin >> filePath;
  109.     int matrixRows = 0, matrixCols = 0, vectorLength = 0;
  110.     string vectorType = "";
  111.     vector<vector<double>> matrix;
  112.     vector<double> vec;
  113.     ifstream inputFile;
  114.     inputFile.open(filePath);
  115.     if(inputFile) {
  116.         matrix = takeMatrixFromFile(inputFile);
  117.         vectorType = takeVectorType(inputFile);
  118.         vec = takeVector(inputFile);
  119.         inputFile.close();
  120.         notCorrect = false;
  121.     } else {
  122.         cout << "Произошла ошибка при чтении файла. Убедитесь, что такой файл существует, либо проверьте имя файла." << endl;
  123.     }
  124.  
  125.     if(!notCorrect) {
  126.         cout << "Матрица:" << endl;
  127.         showMatrix(matrix);
  128.         cout << "Тип вектора: " << vectorType << endl;
  129.         cout << "Вектор:" << endl;
  130.         showVector(vec);
  131.  
  132.         matrixRows = matrix.size();
  133.         vectorLength = vec.size();
  134.  
  135.         if((vectorType == "вектор-столбец" && matrixRows != 1) || (vectorType == "вектор-строка" && matrixRows != vectorLength)) {
  136.             cout << "Число строк в матрице должно быть равно числу столбцов в векторе" << endl;
  137.         } else {
  138.             vectorLength = vectorType == "вектор-столбец" ? vectorLength : 1;
  139.             vector<vector<double>> product = takeProduct(matrix, vec, vectorLength);
  140.             cout << "Результат:" << endl;
  141.             string result = showMatrix(product);
  142.             ofstream outputFile;
  143.             outputFile.open("output.txt");
  144.             outputFile << result;
  145.             cout << "Результат сохранен в файл output.txt" << endl;
  146.         }
  147.     }
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement