SHARE
TWEET

Untitled

a guest Feb 24th, 2020 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. using namespace std;
  5.  
  6. class Matrix{
  7.     public:
  8.     int N, M;
  9.     int **Arr;
  10.  
  11.     Matrix(int N, int M){
  12.         this -> N = N;
  13.         this -> M = M;
  14.         this -> Arr = new int* [N + 1];
  15.         for(int i = 1 ; i <= N ; i++){
  16.             this -> Arr[i] = new int[M + 1];
  17.             memset(this -> Arr[i], 0, sizeof(this -> Arr[i]));
  18.         }
  19.     }
  20.  
  21.     friend ostream & operator << (ostream &out, const Matrix *c);
  22.     friend istream & operator >> (istream &in,  Matrix *c);
  23.     Matrix operator = (Matrix other){
  24.         for(int i = 1 ; i <= this -> N ; i++){
  25.             for(int j = 1 ; j <= this -> M ; j++){
  26.                 this -> Arr[i][j] = other.Arr[i][j];
  27.             }
  28.         }
  29.     }
  30.     Matrix operator + (Matrix RHS){
  31.         Matrix *temp = new Matrix(this -> N, RHS.M);
  32.         for(int i = 1 ; i <= this -> N ; i++){
  33.             for(int j = 1 ; j <= this -> M ; j++){
  34.                 temp -> Arr[i][j] = RHS.Arr[i][j] + Arr[i][j];
  35.             }
  36.         }
  37.         return *temp;
  38.     }
  39.     Matrix operator - (Matrix RHS){
  40.         Matrix *temp = new Matrix(this -> N, this -> M);
  41.         for(int i = 1 ; i <= this -> N ; i++){
  42.             for(int j = 1 ; j <= this -> M ; j++){
  43.                 temp -> Arr[i][j] =  Arr[i][j] - RHS.Arr[i][j];
  44.             }
  45.         }
  46.         return *temp;
  47.     }
  48.     Matrix operator * (Matrix RHS){
  49.         Matrix *temp = new Matrix(this -> N, RHS.M);
  50.         for(int i = 1 ; i <= this -> N ; i++){
  51.             for(int j = 1 ; j <= RHS.M ; j++){
  52.                 for(int k = 1 ; k <= this -> M ; k++){
  53.                     temp -> Arr[i][j] += this -> Arr[i][k] * RHS.Arr[k][j];
  54.                 }
  55.             }
  56.         }
  57.         return *temp;
  58.     }
  59.  
  60.     Matrix operator / (Matrix RHS){
  61.         Matrix *temp = new Matrix(this -> M, this -> N);
  62.         for(int i = 1 ; i <= this -> N ; i++){
  63.             for(int j = 1 ; j <= this -> M ; j++){
  64.                 temp -> Arr[j][i] = RHS.Arr[i][j];
  65.             }
  66.         }
  67.         return *temp;
  68.     }
  69.  
  70. };
  71.  
  72. ostream & operator << (ostream &out, Matrix &c)
  73. {
  74.     for(int i = 1 ; i <= c.N ; i++){
  75.         out << c.Arr[i][1];
  76.         for(int j = 2 ; j <= c.M ; j++){
  77.             out << ' ' << c.Arr[i][j];
  78.         }
  79.         out << endl;
  80.     }
  81.     return out;
  82. }
  83.  
  84. istream & operator >> (istream &in,  Matrix &c)
  85. {
  86.     for(int i = 1 ; i <= c.N ; i++){
  87.         for(int j = 1 ; j <= c.M ; j++){
  88.             in >> c.Arr[i][j];
  89.         }
  90.     }
  91.     return in;
  92. }
  93.  
  94. class square: public Matrix{
  95.     public:
  96.         square(int n) : Matrix(n, n){}
  97.         Matrix operator = (Matrix rhs)
  98.         {
  99.             return Matrix::operator=(rhs);
  100.         }
  101. };
  102.  
  103.  
  104. int main()
  105. {
  106.     int n;
  107.     cin >> n;
  108.     square A(n);
  109.     cin >> A;
  110.     cin >> n;
  111.     square B(n);
  112.     cin >> B;
  113.     cin >> n;
  114.     square C(n);
  115.     cin >> C;
  116.     square D(n);
  117.     D = A + B;
  118.     cout << D;
  119.     square E(n);
  120.     E = B - A;
  121.     cout << E;
  122.     square F(n);
  123.     F = C * A;
  124.     cout << F;
  125.     square G(n);
  126.     G = A / A;
  127.     cout << G;
  128.     return 0;
  129. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top