Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <algorithm>
- using namespace std;
- vector<string> split(string str, string delimiter) {
- vector<string> result;
- int pos = 0;
- while(pos != -1) {
- pos = str.find(delimiter);
- result.push_back(str.substr(0, pos));
- str.erase(0, pos + delimiter.length());
- }
- return result;
- }
- void showVector(vector<double> vector) {
- for(double el : vector)
- cout << el << " ";
- cout << endl;
- }
- string showMatrix(vector<vector<double>> matrix) {
- string matrixString = "";
- for(vector<double> row : matrix) {
- for(double el : row) {
- cout << el << " ";
- matrixString += to_string(el) + " ";
- }
- cout << endl;
- }
- return matrixString;
- }
- string takeVectorType(ifstream &inputFile) {
- inputFile.seekg(0);
- string vectorType = "";
- bool notCorrect = true;
- while(notCorrect && getline(inputFile, vectorType)) {
- if(vectorType == "вектор-строка" || vectorType == "вектор-столбец")
- notCorrect = false;
- }
- return vectorType;
- }
- vector<double> takeRow(string str, int size) {
- vector<double> row(size);
- vector<string> outputList = split(str, " ");
- if(outputList.size() == size) {
- for(int i = 0; i < size; i++)
- if(isdigit(outputList[i][0]))
- row[i] = stod(outputList[i]);
- } else
- cout << "Количество столбцов, введенных в файле, должно совпадать с количеством столбцов в матрице" << endl;
- return row;
- }
- vector<double> takeVector(ifstream &inputFile) {
- string line = "";
- vector<double> vec;
- getline(inputFile, line);
- int vectorLength = stoi(line);
- getline(inputFile, line);
- vec = takeRow(line, vectorLength);
- return vec;
- }
- vector<vector<double>> takeMatrixFromFile(ifstream &inputFile) {
- int matrixRows = 0, matrixCols = 0;
- string line = "";
- getline(inputFile, line);
- matrixRows = stoi(line);
- getline(inputFile, line);
- matrixCols = stoi(line);
- vector<vector<double>> matrix(matrixRows, vector<double>(matrixCols));
- for(int i = 0; i < matrixRows; i++) {
- getline(inputFile, line);
- matrix[i] = takeRow(line, matrixCols);
- }
- return matrix;
- }
- vector<vector<double>> takeProduct(vector<vector<double>> matrix, vector<double> vec, int vectorLength) {
- vector<vector<double>> product(vectorLength, vector<double>(matrix[0].size(), 0));
- for(int i = 0; i < product.size(); i++) {
- for(int j = 0; j < matrix.size(); j++) {
- for(int k = 0; k < matrix[j].size(); k++) {
- product[i][k] += vec[vectorLength == 1 ? j : i] * matrix[j][k];
- }
- }
- }
- return product;
- }
- int main() {
- cout << "Данная программа находит произведение вектора на матрицу\n" << endl;
- bool notCorrect = true;
- string filePath;
- cout << "Введите путь до файла" << endl;
- cin >> filePath;
- int matrixRows = 0, matrixCols = 0, vectorLength = 0;
- string vectorType = "";
- vector<vector<double>> matrix;
- vector<double> vec;
- ifstream inputFile;
- inputFile.open(filePath);
- if(inputFile) {
- matrix = takeMatrixFromFile(inputFile);
- vectorType = takeVectorType(inputFile);
- vec = takeVector(inputFile);
- inputFile.close();
- notCorrect = false;
- } else {
- cout << "Произошла ошибка при чтении файла. Убедитесь, что такой файл существует, либо проверьте имя файла." << endl;
- }
- if(!notCorrect) {
- cout << "Матрица:" << endl;
- showMatrix(matrix);
- cout << "Тип вектора: " << vectorType << endl;
- cout << "Вектор:" << endl;
- showVector(vec);
- matrixRows = matrix.size();
- vectorLength = vec.size();
- if((vectorType == "вектор-столбец" && matrixRows != 1) || (vectorType == "вектор-строка" && matrixRows != vectorLength)) {
- cout << "Число строк в матрице должно быть равно числу столбцов в векторе" << endl;
- } else {
- vectorLength = vectorType == "вектор-столбец" ? vectorLength : 1;
- vector<vector<double>> product = takeProduct(matrix, vec, vectorLength);
- cout << "Результат:" << endl;
- string result = showMatrix(product);
- ofstream outputFile;
- outputFile.open("output.txt");
- outputFile << result;
- cout << "Результат сохранен в файл output.txt" << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement