Advertisement
StefiIOE

Matrix

Apr 14th, 2020
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include<iostream>
  2. #include <cstring>
  3. using namespace std;
  4. class Matrix
  5. {
  6.     protected:
  7.     float matrix[10][10];
  8.     int rows;
  9.     int columns;
  10.  /** void copy(const Matrix &m)
  11.     {
  12.         this->rows=m.rows;
  13.         this->columns=m.columns;
  14.         for(int i = 0 ; i < rows ; i ++)
  15.         {
  16.             for(int j = 0 ; j < columns ; j ++)
  17.             {
  18.             this->matrix[i][j]=m.matrix[i][j];
  19.             }
  20.         }
  21.     }
  22.     **/
  23.     public:
  24.     Matrix(){}
  25.     Matrix(int rows , int columns)
  26.     {
  27.         this->rows=rows;
  28.         this->columns=columns;
  29.         for(int i = 0 ; i < rows ; i ++)
  30.         {
  31.         for(int j = 0 ; j < columns ; j ++)
  32.          {
  33.             this->matrix[i][j]=0;
  34.                
  35.             }
  36.         }
  37.     }
  38.    // Matrix (const Matrix &s){copy(s);}
  39.    
  40.    
  41.     Matrix &operator +(int s)
  42.     {
  43.        for(int i = 0 ; i < rows ; i ++)
  44.         {
  45.         for(int j = 0 ; j < columns ; j ++)
  46.          {
  47.             matrix[i][j]+=s;
  48.             }
  49.         }
  50.         return *this;
  51.     }
  52.     Matrix &operator-(Matrix &s)
  53.     {
  54.        for(int i = 0 ; i < rows ; i ++)
  55.         {
  56.         for(int j = 0 ; j < columns ; j ++)
  57.          {
  58.             this->matrix[i][j]-=s.matrix[i][j];
  59.             }
  60.         }
  61.         return *this;
  62.     }
  63.     Matrix operator*(Matrix &s)
  64.     {
  65.        Matrix  newMatrix(this->rows,this->columns);
  66.        
  67.        for(int i = 0 ; i < rows ; i ++)
  68.         {
  69.         for(int j = 0 ; j < columns ; j ++)
  70.              
  71.          {
  72.             for(int t = 0 ; t < columns ; t ++){
  73.            
  74.            newMatrix.matrix[i][j]+=this->matrix[i][t]*s.matrix[t][j];    
  75.             }
  76.            
  77.             }
  78.         }
  79.         return newMatrix;
  80.     }
  81.     friend ostream &operator <<(ostream &out ,const Matrix &s)
  82.     {
  83.          for(int i = 0 ; i < s.rows ; i ++)
  84.         {
  85.         for(int j = 0 ; j < s.columns ; j ++)
  86.              
  87.          {
  88.             out<<s.matrix[i][j]<<" ";
  89.            
  90.    
  91.     }
  92.              out<<endl;
  93. }
  94.         return out;
  95.     }
  96.     friend istream &operator >>(istream &in , Matrix &s)
  97.     {
  98.     in>>s.rows>>s.columns;
  99.          for(int i = 0 ; i < s.rows ; i ++)
  100.         {
  101.         for(int j = 0 ; j < s.columns ; j ++)
  102.              
  103.          {
  104.             in>>s.matrix[i][j];
  105.       }
  106.    }
  107.     return in ;
  108.     }
  109.    
  110. };
  111. int main() {
  112.     Matrix A,B,C;
  113.     cin >> A >> B >> C;
  114.     Matrix D = B * C;
  115.     Matrix R = A - D + 2;
  116.     cout << R;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement