Vladislav_Bezruk

Untitled

Nov 4th, 2021 (edited)
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. #define IN_FILE1 "input1.txt"
  5. #define IN_FILE2 "input2.txt"
  6. #define OUT_FILE "output.txt"
  7.  
  8. using namespace std;
  9.  
  10. class Matrica {
  11.     public:
  12.         int n;
  13.         int m;
  14.         int** data;
  15.  
  16.         void allocate() {
  17.             data = new int* [n];
  18.  
  19.             for (int i = 0; i < n; i++)
  20.                 data[i] = new int [m];
  21.         }
  22.  
  23.         void destroy() {
  24.             return;
  25.             if (n && m) {
  26.                 for (int i = 0; i < n; i++)
  27.                     delete data[i];
  28.                 delete data;
  29.             }
  30.         }
  31.  
  32.     public:
  33.         Matrica() { n = m = 0; };
  34.  
  35.         Matrica(int _n, int _m) : n(_n), m(_m) { allocate(); }
  36.  
  37.         ~ Matrica() { destroy(); }
  38.  
  39.         void set(string filename) {
  40.             ifstream ifs(filename);
  41.  
  42.             if (!ifs) {
  43.                 cout << "File does not open!" << endl;
  44.                 exit(1);
  45.             }
  46.  
  47.             ifs >> n >> m;
  48.  
  49.             destroy();
  50.             allocate();
  51.  
  52.             for (int i = 0; i < n; i++)
  53.                 for (int j = 0; j < m; j++)
  54.                     ifs >> data[i][j];
  55.         }
  56.  
  57.         void get(string filename) {
  58.             ofstream ofs(filename);
  59.  
  60.             if (!ofs) {
  61.                 cout << "File does not created!" << endl;
  62.                 exit(1);
  63.             }
  64.  
  65.             ofs << "n = " << n << " m = " << m << endl;
  66.             ofs << "Matrix:" << endl;
  67.  
  68.             for (int i = 0; i < n; i++) {
  69.                 for (int j = 0; j < m; j++)
  70.                     ofs << data[i][j] << " ";
  71.                 ofs << endl;
  72.             }
  73.         }
  74.  
  75.         Matrica operator * (const Matrica &x) { return mult(*this, x); }
  76.  
  77.         friend Matrica mult(const Matrica &a, const Matrica &b);
  78. };
  79.  
  80. Matrica mult(const Matrica &a, const Matrica &b) {
  81.     if (a.m != b.n) {
  82.         cout << "Matrices cannot be multiplied!" << endl;
  83.         exit(1);
  84.     }
  85.  
  86.     Matrica c(a.n, b.m);
  87.  
  88.     for (int i = 0; i < c.n; i++)
  89.         for (int j = 0; j < c.m; j++) {
  90.             c.data[i][j] = 0;
  91.             for (int k = 0; k < a.m; k++)
  92.                 c.data[i][j] += a.data[i][k] * b.data[k][j];
  93.         }
  94.     return c;
  95. }
  96.  
  97. int main() {
  98.  
  99.     Matrica a, b, c;
  100.  
  101.     a.set(IN_FILE1);
  102.  
  103.     b.set(IN_FILE2);
  104.  
  105.     c = a * b;
  106.  
  107.     c.get(OUT_FILE);
  108.  
  109.     return 0;
  110. }
  111.  
Add Comment
Please, Sign In to add comment