Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- using namespace std;
- class Matrix
- {
- int ln,col,**mat;
- public:
- Matrix(int n = 1,int m = 1)
- {
- if(n != m)
- {
- cout << "Matricea nu este patratica !" << endl;
- return;
- }
- col = n;
- ln = m;
- mat = new int*[m];
- for(int i = 0; i < m; i++) mat[i] = new int[n];
- for(int i = 0; i < m; i++)
- for(int j = 0; j < n; j++)
- mat[i][j] = 0;
- }
- Matrix operator+(const Matrix&) const;
- Matrix operator*(const Matrix&) const;
- Matrix operator^(const int&) const;
- bool checkSize(const Matrix&) const;
- void insertMat();
- void printMat();
- char* toString();
- };
- void Matrix::insertMat()
- {
- if(this->ln != this->col) return;
- cout << "Introduceti elementele matricii" << endl;
- for(int i = 0; i < this->ln; i++)
- {
- for(int j = 0; j < this->col; j++)
- {
- cout << "[" << i << "][" << j << "] = ";
- cin >> this->mat[i][j];
- }
- }
- }
- void Matrix::printMat()
- {
- cout << "Afisarea matricii\nDimensiune " << this->ln << "*" << this->col << endl;
- for(int i = 0; i < this->ln; i++)
- {
- for(int j = 0; j < this->col; j++)
- {
- cout << this->mat[i][j] << " ";
- }
- cout << endl;
- }
- }
- bool Matrix::checkSize(const Matrix &M) const
- { Matrix operator+(const Matrix&) const;
- Matrix operator*(const Matrix&) const;
- Matrix operator^(const int&) const;
- if(this->ln == M.ln && this->col == M.col) return true;
- return false;
- }
- Matrix Matrix::operator+(const Matrix &M) const
- {
- if(!(*this).checkSize(M))
- {
- cout << "Matricile sunt de dimensiuni diferite !";
- return -1;
- }
- Matrix result(this->ln,this->col);
- for(int i = 0; i < this->col; i++)
- {
- for(int j = 0; j< this->ln;j++)
- {
- result.mat[i][j] = this->mat[i][j] + M.mat[i][j];
- }
- }
- return result;
- }
- Matrix Matrix::operator*(const Matrix &M) const
- {
- if(!(*this).checkSize(M)))
- Matrix result(this->ln,this->col);
- for(int i = 0; i < this->ln; i++)
- {
- for(int j = 0; j < this->col; j++)
- {
- for(int k = 0; k < this->ln; k++)
- {
- result.mat[i][j] += this->mat[i][k]*M.mat[k][j];
- }
- }
- }
- return result;
- }
- Matrix Matrix::operator^(const int &p) const
- {
- Matrix result(this->ln,this->col);
- for(int i = 1; i <= p; i++)
- {
- }
- result.mat = this->mat;
- return result;
- }
- int main()
- {
- Matrix A(2,2),B(2,2),C(2,2);
- A.insertMat();
- A.printMat();
- C = A^2;
- C.printMat();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement