Advertisement
Sauka1337

Untitled

Oct 11th, 2023
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. // Function prototypes
  9. vector<vector<int>> inputMatrix(const string& name);
  10. vector<vector<int>> readMatrixFromFile(const string& fileName);
  11. vector<vector<int>> multiplyMatrices(const vector<vector<int>>& matrixA, const vector<vector<int>>& matrixB);
  12. void displayMatrix(const vector<vector<int>>& matrix);
  13. void saveMatrixToFile(const vector<vector<int>>& matrix, const string& filename);
  14.  
  15. int main() {
  16.     cout << "Choose an option:" << endl;
  17.     cout << "1. Input matrices manually" << endl;
  18.     cout << "2. Read matrices from a text file" << endl;
  19.  
  20.     int option;
  21.     cin >> option;
  22.  
  23.     vector<vector<int>> matrixA, matrixB;
  24.  
  25.     if (option == 1) {
  26.         matrixA = inputMatrix("first");
  27.         matrixB = inputMatrix("second");
  28.     } else if (option == 2) {
  29.         cout << "Enter the file name for matrix A: ";
  30.         string fileNameA;
  31.         cin >> fileNameA;
  32.         matrixA = readMatrixFromFile(fileNameA);
  33.  
  34.         cout << "Enter the file name for matrix B: ";
  35.         string fileNameB;
  36.         cin >> fileNameB;
  37.         matrixB = readMatrixFromFile(fileNameB);
  38.     } else {
  39.         cout << "Invalid option. Exiting program." << endl;
  40.         return 1;
  41.     }
  42.  
  43.     if (matrixA.empty() || matrixB.empty()) {
  44.         cout << "Error in reading matrices. Exiting program." << endl;
  45.         return 1;
  46.     }
  47.  
  48.     // Perform matrix multiplication
  49.     vector<vector<int>> resultMatrix = multiplyMatrices(matrixA, matrixB);
  50.  
  51.     // Display the result
  52.     cout << "Result of matrix multiplication:" << endl;
  53.     displayMatrix(resultMatrix);
  54.  
  55.     // Save the result to a text file
  56.     saveMatrixToFile(resultMatrix, "resultMatrix.txt");
  57.     cout << "Result matrix saved to resultMatrix.txt" << endl;
  58.  
  59.     return 0;
  60. }
  61.  
  62. // Function to input matrix from the user
  63. vector<vector<int>> inputMatrix(const string& name) {
  64.     cout << "Enter the number of rows for the " << name << " matrix: ";
  65.     int rows, cols;
  66.     cin >> rows;
  67.  
  68.     cout << "Enter the number of columns for the " << name << " matrix: ";
  69.     cin >> cols;
  70.  
  71.     if (rows <= 0 || cols <= 0) {
  72.         cout << "Error: Matrix dimensions must be positive." << endl;
  73.         return {};
  74.     }
  75.  
  76.     vector<vector<int>> matrix(rows, vector<int>(cols, 0));
  77.  
  78.     cout << "Enter the elements for the " << name << " matrix:" << endl;
  79.     for (int i = 0; i < rows; i++) {
  80.         for (int j = 0; j < cols; j++) {
  81.             cout << "Element at position (" << (i + 1) << ", " << (j + 1) << "): ";
  82.             cin >> matrix[i][j];
  83.         }
  84.     }
  85.  
  86.     return matrix;
  87. }
  88.  
  89. // Function to read matrix from a text file
  90. vector<vector<int>> readMatrixFromFile(const string& fileName) {
  91.     ifstream file(fileName);
  92.  
  93.     if (!file.is_open()) {
  94.         cout << "Error: File not found or cannot be read." << endl;
  95.         return {};
  96.     }
  97.  
  98.     // Initialize a stringstream to parse each line
  99.     stringstream ss;
  100.     string line;
  101.     int rows = 0;
  102.     int cols = 0;
  103.  
  104.     // Count the number of rows and columns
  105.     while (getline(file, line)) {
  106.         ss.clear();
  107.         ss.str(line);
  108.  
  109.         int value;
  110.         while (ss >> value) {
  111.             cols++;
  112.         }
  113.  
  114.         rows++;
  115.     }
  116.  
  117.     // Reset file to the beginning
  118.     file.clear();
  119.     file.seekg(0);
  120.  
  121.     // Initialize the matrix
  122.     vector<vector<int>> matrix(rows, vector<int>(cols / rows, 0));
  123.  
  124.     // Read matrix elements
  125.     for (int i = 0; i < rows; i++) {
  126.         for (int j = 0; j < cols / rows; j++) {
  127.             if (!(file >> matrix[i][j])) {
  128.                 cout << "Error: Insufficient data in the file." << endl;
  129.                 return {};
  130.             }
  131.         }
  132.     }
  133.  
  134.     file.close();
  135.     return matrix;
  136. }
  137.  
  138. // Function to multiply two matrices
  139. vector<vector<int>> multiplyMatrices(const vector<vector<int>>& matrixA, const vector<vector<int>>& matrixB) {
  140.     int rowsA = matrixA.size();
  141.     int colsA = matrixA[0].size();
  142.     int colsB = matrixB[0].size();
  143.  
  144.     vector<vector<int>> resultMatrix(rowsA, vector<int>(colsB, 0));
  145.  
  146.     for (int i = 0; i < rowsA; i++) {
  147.         for (int j = 0; j < colsB; j++) {
  148.             for (int k = 0; k < colsA; k++) {
  149.                 resultMatrix[i][j] += matrixA[i][k] * matrixB[k][j];
  150.             }
  151.         }
  152.     }
  153.  
  154.     return resultMatrix;
  155. }
  156.  
  157. // Function to display a matrix
  158. void displayMatrix(const vector<vector<int>>& matrix) {
  159.     for (const auto& row : matrix) {
  160.         for (int value : row) {
  161.             cout << value << " ";
  162.         }
  163.         cout << endl;
  164.     }
  165. }
  166.  
  167. // Function to save a matrix to a text file
  168. void saveMatrixToFile(const vector<vector<int>>& matrix, const string& filename) {
  169.     ofstream writer(filename);
  170.  
  171.     if (!writer.is_open()) {
  172.         cout << "Error: Unable to save result matrix to file." << endl;
  173.         return;
  174.     }
  175.  
  176. ////    writer << matrix.size() << " " << matrix[0].size() << endl;
  177.  
  178.     for (const auto& row : matrix) {
  179.         for (int value : row) {
  180.             writer << value << " ";
  181.         }
  182.         writer << endl;
  183.     }
  184.  
  185.     writer.close();
  186. }
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement