Advertisement
Vladislav_Bezruk

Untitled

Nov 2nd, 2021
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cassert>
  4.  
  5. /*input file of first matrix*/
  6. #define IN_FA "inputA.txt"
  7.  
  8. /*input file of second matrix*/
  9. #define IN_FB "inputB.txt"
  10.  
  11. /*output file*/
  12. #define OUT_F "output.txt"
  13.  
  14. using namespace std;
  15.  
  16. /*class Matrix*/
  17. class Matrix {
  18.     public:
  19.         /*count of rows*/
  20.         int n;
  21.  
  22.         /*count of columns*/
  23.         int m;
  24.  
  25.         /*two-dimensional array*/
  26.         int** data;
  27.  
  28.         /*function for allocating two-dimensional array*/
  29.         void allocate() {
  30.             data = new int* [n];
  31.  
  32.             for (int i = 0; i < n; i++)
  33.                 data[i] = new int [m];
  34.         }
  35.  
  36.         /*function for destroying two-dimensional array*/
  37.         void destroy() {
  38.             return;
  39.             if (n && m) {
  40.                 for (int i = 0; i < n; i++)
  41.                     delete data[i];
  42.                 delete data;
  43.             }
  44.         }
  45.  
  46.     public:
  47.         /*constructor by default*/
  48.         Matrix() { n = m = 0; };
  49.  
  50.         /*initializer list constructors*/
  51.         Matrix(int _n, int _m) : n(_n), m(_m) { allocate(); }
  52.  
  53.         /*destructor*/
  54.         ~ Matrix() { destroy(); }
  55.  
  56.         /*function set. Reading matrix from file*/
  57.         void set(string filename) {
  58.             ifstream ifs(filename);
  59.  
  60.             assert(ifs && "File does not open!");
  61.  
  62.             ifs >> n >> m;
  63.  
  64.             destroy();
  65.             allocate();
  66.  
  67.             for (int i = 0; i < n; i++)
  68.                 for (int j = 0; j < m; j++)
  69.                     ifs >> data[i][j];
  70.         }
  71.  
  72.         /*function get. Writing matrix to file*/
  73.         void get(string filename) {
  74.             ofstream ofs(filename);
  75.  
  76.             assert(ofs && "File does not created!");
  77.  
  78.             ofs << "n = " << n << " m = " << m << endl;
  79.             ofs << "Matrix:" << endl;
  80.  
  81.             for (int i = 0; i < n; i++) {
  82.                 for (int j = 0; j < m; j++)
  83.                     ofs << data[i][j] << " ";
  84.                 ofs << endl;
  85.             }
  86.         }
  87.  
  88.         /*assignation operator*/
  89.         Matrix& operator = (const Matrix &x) {
  90.             n = x.n; m = x.m;
  91.  
  92.             destroy();
  93.             allocate();
  94.  
  95.             for (int i = 0; i < n; i++)
  96.                 for (int j = 0; j < m; j++)
  97.                     data[i][j] = x.data[i][j];
  98.             return *this;
  99.         }
  100.  
  101.         /*multiplication operator*/
  102.         Matrix operator * (const Matrix &x) { return mult(*this, x); }
  103.  
  104.         /*friend function for multiplication*/
  105.         friend Matrix mult(const Matrix &a, const Matrix &b);
  106. };
  107.  
  108. /*multiplication function*/
  109. Matrix mult(const Matrix &a, const Matrix &b) {
  110.     assert(a.m == b.n && "Matrices cannot be multiplied!");
  111.  
  112.     Matrix c(a.n, b.m);
  113.  
  114.     for (int i = 0; i < c.n; i++)
  115.         for (int j = 0; j < c.m; j++) {
  116.             c.data[i][j] = 0;
  117.             for (int k = 0; k < a.m; k++)
  118.                 c.data[i][j] += a.data[i][k] * b.data[k][j];
  119.         }
  120.     return c;
  121. }
  122.  
  123. int main() {
  124.  
  125.     Matrix a, b, c;
  126.  
  127.     /*reading from file*/
  128.     a.set(IN_FA);
  129.  
  130.     b.set(IN_FB);
  131.  
  132.     /*calculating*/
  133.     c = a * b;
  134.  
  135.     /*writing to file*/
  136.     c.get(OUT_F);
  137.  
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement