Advertisement
Guest User

kek

a guest
May 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. template < typename type >
  6. class matrix;
  7.  
  8. template < typename type >
  9. class line {
  10.  
  11. private:
  12.  
  13.     friend class matrix < type >;
  14.  
  15.     matrix < type > &parent;
  16.     size_t X;
  17.     line(matrix < type > &_parent, size_t _X): parent(_parent), X(_X) {}
  18.  
  19.     line(line &&_line): parent(_line.parent), X(_line.X) {}
  20.  
  21. public:
  22.  
  23.     line(const line&) = delete;
  24.     line& operator = (const line&) = delete;
  25.  
  26.     type& operator[](size_t Y) {
  27.         return parent.data[X][Y];
  28.     }
  29.     const type& operator[](size_t Y) const {
  30.         return parent.data[X][Y];
  31.     }
  32.  
  33.     ~line() = default;
  34.  
  35. };
  36.  
  37. template < typename type >
  38. class matrix {
  39. private:
  40.  
  41.     typedef matrix < type > matrix_t;
  42.     typedef line < type > line_t;
  43.  
  44.     typedef function < type (const type&, const type&) > bopf;
  45.     typedef function < void (type&) > medft;
  46.     typedef function < type (const type&) > mtrft;
  47.  
  48.     type **data = nullptr;
  49.  
  50.     size_t X;
  51.     size_t Y;
  52.  
  53. public:
  54.  
  55.     bool exist() const {
  56.         return data;
  57.     }
  58.  
  59.     void destroy() {
  60.  
  61.         if (!exist()) {
  62.             return;
  63.         }
  64.  
  65.         for (size_t i = 0; i < X; ++i){
  66.             delete[] data[i];
  67.         }
  68.         delete[] data;
  69.         data = nullptr;
  70.  
  71.     }
  72.  
  73.     void resize(size_t a, size_t b) {
  74.         destroy();
  75.         X = a;
  76.         Y = b;
  77.         data = new type*[X];
  78.         for (size_t i = 0; i < X; ++i){
  79.             data[i] = new type[Y];
  80.         }
  81.     }
  82.  
  83.     matrix() = default;
  84.  
  85.     matrix(size_t _X, size_t _Y){
  86.         resize(_X, _Y);
  87.     };
  88.  
  89.     matrix(const matrix_t &m){
  90.         resize(m.X, m.Y);
  91.         for (int k = 0; k < X; k++)
  92.             for (int i = 0; i < Y; i++)
  93.                 data[k][i] = m.data[k][i];
  94.     };
  95.  
  96.     matrix(matrix_t &&m): data(m.data), X(m.X), Y(m.Y) {
  97.         m.data = nullptr;
  98.     }
  99.  
  100.     ~matrix(){
  101.         destroy();
  102.     };
  103.  
  104.     void read(istream &is = cin) {
  105.         if (!exist()){
  106.             return;
  107.         }
  108.         for (size_t i = 0; i < X; ++i){
  109.             for (size_t j = 0; j < Y; ++j){
  110.                 is >> data[i][j];
  111.             }
  112.         }
  113.     }
  114.     void print(ostream &os = cout, size_t wal = 5) const {
  115.         if (!exist()){
  116.             return;
  117.         }
  118.         for (size_t i = 0; i < X; ++i){
  119.             for (size_t j = 0; j < Y; ++j){
  120.                 os << setw(wal) << data[i][j] << " ";
  121.             }
  122.             os << "\n";
  123.         }
  124.     };
  125.  
  126.     size_t getX() const {
  127.         return X;
  128.     }
  129.     size_t getY() const {
  130.         return Y;
  131.     }
  132.  
  133.     void genMatrix()
  134.     {
  135.         for (int i = 0; i < X; i++)
  136.             for (int j = 0; j < Y; j++)
  137.                 data[i][j] = 0;
  138.     };
  139.  
  140.     static matrix_t mbop(const matrix_t &a, const matrix_t &b, bopf func) {
  141.         if (!a.exist() || !b.exist() || a.getX() != b.getX() || a.getY() != b.getY()) {
  142.             return matrix_t();
  143.         }
  144.         const size_t &X = a.X, &Y = a.Y;
  145.         matrix_t r(X, Y);
  146.         for (size_t i = 0; i < X; ++i){
  147.             for (size_t j = 0; j < Y; ++j){
  148.                 r.data[i][j] = func(a.data[i][j], b.data[i][j]);
  149.             }
  150.         }
  151.         return r;
  152.     }
  153.     static void med(matrix_t &m, medft func) {
  154.         const size_t &X = m.X, &Y = m.Y;
  155.         for (size_t i = 0; i < X; ++i) {
  156.             for (size_t j = 0; j < Y; ++j) {
  157.                 func(m.data[i][j]);
  158.             }
  159.         }
  160.     }
  161.  
  162.     matrix_t operator + (const matrix &m) const {
  163.         return mbop(*this, m, plus < type >());
  164.     }
  165.     matrix_t operator - (const matrix &m) const {
  166.         return mbop(*this, m, minus < type >());
  167.     }
  168.  
  169.     line_t operator [](size_t x){
  170.         return line_t(*this, x);
  171.     };
  172.  
  173. };
  174.  
  175. int main(){
  176.  
  177.     matrix < int > a(2, 3), b(2, 3);
  178.  
  179.     auto rf = [](int &v){ cin >> v; };
  180.  
  181.     auto medptr = matrix < int > :: med;
  182.  
  183.     medptr(a, rf);
  184.     medptr(b, rf);
  185.  
  186.     matrix < int > c = a + b;
  187.  
  188.     c.print();
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement