Advertisement
Guest User

dsa

a guest
Oct 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6. class Matrix
  7. {
  8.     int ln,col,**mat;
  9.     public:
  10.         Matrix(int n = 1,int  m = 1)
  11.         {
  12.             if(n != m)
  13.             {
  14.                 cout << "Matricea nu este patratica !" << endl;
  15.                 return;
  16.             }
  17.             col = n;
  18.             ln = m;
  19.  
  20.             mat = new int*[m];
  21.             for(int i = 0; i < m; i++) mat[i] = new int[n];
  22.  
  23.             for(int i = 0; i < m; i++)
  24.                 for(int j = 0; j < n; j++)
  25.                     mat[i][j] = 0;
  26.         }
  27.         Matrix operator+(const Matrix&) const;
  28.         Matrix operator*(const Matrix&) const;
  29.         Matrix operator^(const int&) const;
  30.         bool checkSize(const Matrix&) const;
  31.         void insertMat();
  32.         void printMat();
  33.         char* toString();
  34. };
  35. void Matrix::insertMat()
  36. {
  37.     if(this->ln != this->col) return;
  38.     cout << "Introduceti elementele matricii" << endl;
  39.     for(int i = 0; i < this->ln; i++)
  40.     {
  41.         for(int j = 0; j < this->col; j++)
  42.         {
  43.             cout << "[" << i << "][" << j << "] = ";
  44.             cin >> this->mat[i][j];
  45.         }
  46.     }
  47. }
  48. void Matrix::printMat()
  49. {
  50.     cout << "Afisarea matricii\nDimensiune " << this->ln << "*" << this->col << endl;
  51.     for(int i = 0; i < this->ln; i++)
  52.     {
  53.         for(int j = 0; j < this->col; j++)
  54.         {
  55.             cout << this->mat[i][j] << " ";
  56.         }
  57.         cout << endl;
  58.     }
  59. }
  60. bool Matrix::checkSize(const Matrix &M) const
  61. {        Matrix operator+(const Matrix&) const;
  62.         Matrix operator*(const Matrix&) const;
  63.         Matrix operator^(const int&) const;
  64.     if(this->ln == M.ln && this->col == M.col) return true;
  65.     return false;
  66. }
  67. Matrix Matrix::operator+(const Matrix &M) const
  68. {
  69.     if(!(*this).checkSize(M))
  70.     {
  71.         cout << "Matricile sunt de dimensiuni diferite !";
  72.         return -1;
  73.     }
  74.     Matrix result(this->ln,this->col);
  75.     for(int i = 0; i < this->col; i++)
  76.     {
  77.         for(int j = 0; j< this->ln;j++)
  78.         {
  79.             result.mat[i][j] = this->mat[i][j] + M.mat[i][j];
  80.         }
  81.     }
  82.     return result;
  83. }
  84. Matrix Matrix::operator*(const Matrix &M) const
  85. {
  86.     if(!(*this).checkSize(M)))
  87.     Matrix result(this->ln,this->col);
  88.     for(int i = 0; i < this->ln; i++)
  89.     {
  90.         for(int j = 0; j < this->col; j++)
  91.         {
  92.             for(int k = 0; k < this->ln; k++)
  93.             {
  94.                 result.mat[i][j] += this->mat[i][k]*M.mat[k][j];
  95.             }
  96.         }
  97.     }
  98.     return result;
  99. }
  100. Matrix Matrix::operator^(const int &p) const
  101. {
  102.     Matrix result(this->ln,this->col);
  103.  
  104.     for(int i = 1; i <= p; i++)
  105.     {
  106.  
  107.     }
  108.     result.mat = this->mat;
  109.     return result;
  110. }
  111. int main()
  112. {
  113.     Matrix A(2,2),B(2,2),C(2,2);
  114.     A.insertMat();
  115.     A.printMat();
  116.     C = A^2;
  117.     C.printMat();
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement