Advertisement
Marisichka

Untitled

Nov 25th, 2021
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <cassert>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. #define input "input.txt"
  8. #define output "output.txt"
  9.  
  10. class Matrix {
  11. public:
  12.     int n, m;
  13.     int** data;
  14.  
  15.     void allocate() {
  16.         data = new int* [n];
  17.  
  18.         for (int i = 0; i < n; i++)
  19.             data[i] = new int[m];
  20.     }
  21.     void destruction() {
  22.         return;
  23.         if (n && m) {
  24.             for (int i = 0; i < n; i++)
  25.                 delete data[i];
  26.             delete data;
  27.         }
  28.     }
  29.  
  30. public:
  31.     Matrix() { n = m = 0; };
  32.     Matrix(int _n, int _m) : n(_n), m(_m) { allocate(); }
  33.  
  34.     ~Matrix() { destruction(); }
  35.  
  36.     void set(string filename) {
  37.         ifstream ifs(filename);
  38.  
  39.         assert(ifs && "File is not open");
  40.  
  41.         ifs >> n >> m;
  42.  
  43.         destruction();
  44.         allocate();
  45.  
  46.         for (int i = 0; i < n; i++)
  47.             for (int j = 0; j < m; j++)
  48.                 ifs >> data[i][j];
  49.     }
  50.  
  51.     void get(string filename) {
  52.         ofstream ofs(filename);
  53.  
  54.         assert(ofs && "File was not created");
  55.  
  56.         ofs << "n = " << n << " m = " << m << endl;
  57.         ofs << "Matrix:" << endl;
  58.  
  59.         for (int i = 0; i < n; i++) {
  60.             for (int j = 0; j < m; j++)
  61.                 ofs << data[i][j] << " ";
  62.             ofs << endl;
  63.         }
  64.     }
  65.  
  66.     Matrix& operator = (const Matrix& x) {
  67.         n = x.n; m = x.m;
  68.  
  69.         destruction();
  70.         allocate();
  71.  
  72.         for (int i = 0; i < n; i++)
  73.             for (int j = 0; j < m; j++)
  74.                 data[i][j] = x.data[i][j];
  75.         return *this;
  76.     }
  77.  
  78.     Matrix operator * (const Matrix& x) { return sum(*this, x); }
  79.     friend Matrix sum(const Matrix& a);
  80.     friend Matrix amount(const Matrix& a);
  81. };
  82.  
  83. Matrix sum(const Matrix& a) {
  84.     assert(a.m  && "No way of getting sum");
  85.  
  86.     Matrix b(a.n);
  87.  
  88.     for (int j = 0; j < n; j++)
  89.     {
  90.         Temp[j] = b[0][j];
  91.         for (int i = 0; i < n; i++)
  92.             if (b[i][j] < Temp[j])
  93.                 Temp[j] = B[i][j];
  94.     }
  95.  
  96.     cout << "Matrix A:\n";
  97.     for (int i = 0; i < n; i++)
  98.     {
  99.         for (int j = 0; j < n; j++)
  100.         {
  101.             b[i][j] = Temp[i * m + j];
  102.             if (b[i][j] > 0.) {
  103.                 sum_pos += A[i][j];
  104.             pos++;
  105.             }
  106.             cout << A[i][j] << " ";
  107.         }
  108.         cout << "\n";
  109.     }
  110.  
  111. int main() {
  112.  
  113.     Matrix a, b;
  114.  
  115.     a.set(input);
  116.  
  117.    // считаем кол-во элементов и сумму
  118.  
  119.     b.get(output);
  120.  
  121.     return 0;
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement