Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <clocale>
- using namespace std;
- class Tmatrix
- {
- private:
- int n, m;
- int **matrix;
- public:
- Tmatrix(int newn, int newm)
- {
- matrix = new int*[newm];
- for(int i=0;i<newm;i++)
- matrix[i] = new int[newn];
- for(int i=0;i<newn;i++)
- for(int j=0;j<newm;j++)
- this->matrix[i][j] = 0;
- n = newn;
- m = newm;
- }
- Tmatrix(){}
- int printMatrix()
- {
- for(int i=0;i<this->n;i++)
- {
- for(int j=0;j<this->m;j++)
- cout<<this->matrix[i][j]<<" ";
- cout<<endl;
- }
- cout<<endl;
- return 0;
- }
- int getElem(int i, int j)
- {
- if(i<this->n && j<this->m)
- return this->matrix[i][j];
- else
- return -1;
- }
- int setElem(int i, int j, int elem)
- {
- if(i<this->n && j<this->m)
- {
- this->matrix[i][j] = elem;
- return 0;
- }
- else
- return -1;
- }
- int fillMatrix()
- {
- for(int i=0;i<this->n;i++)
- for(int j=0;j<this->m;j++)
- this->matrix[i][j] = rand() % 10;
- return 0;
- }
- int getN()
- {
- return n;
- }
- int getM()
- {
- return m;
- }
- Tmatrix* sum(Tmatrix* sumB)
- {
- if(this->n==sumB->getN() && this->m==sumB->getM())
- {
- Tmatrix* resSum = new Tmatrix(this->n,this->m);
- for(int i=0;i<this->n;i++)
- for(int j=0;j<this->m;j++)
- resSum->setElem(i,j,this->matrix[i][j]+sumB->getElem(i,j));
- return resSum;
- }else
- return NULL;
- }
- Tmatrix* neg(Tmatrix* negB)
- {
- if(this->n==negB->getN() && this->m==negB->getM())
- {
- Tmatrix* resNeg = new Tmatrix(this->n,this->m);
- for(int i=0;i<this->n;i++)
- for(int j=0;j<this->m;j++)
- resNeg->setElem(i,j,this->matrix[i][j]-negB->getElem(i,j));
- return resNeg;
- }else
- return NULL;
- }
- Tmatrix* mult(Tmatrix* multB)
- {
- if(this->n==multB->getM() && this->m==multB->getN())
- {
- Tmatrix* resMult = new Tmatrix(this->n, multB->getM());
- int premult;
- for(int i=0; i<this->n;i++)
- for(int j=0; j<multB->getM();j++)
- for(int k=0; k<this->m;k++)
- {
- premult = resMult->getElem(i,j);
- resMult->setElem(i, j, premult+this->matrix[i][k] * multB->getElem(k,j));
- }
- return resMult;
- }else
- return NULL;
- }
- friend ostream &operator<<(ostream &stream, Tmatrix* obj);
- friend istream &operator>>(istream &stream, Tmatrix** obj);
- ~Tmatrix()
- {
- for(int i=0;i<n;i++)
- delete[] matrix[i];
- delete[] matrix;
- }
- };
- ostream &operator<<(ostream &stream, Tmatrix* obj)
- {
- for(int i=0;i<obj->n;i++)
- {
- for(int j=0;j<obj->m;j++)
- stream<<obj->matrix[i][j]<<" ";
- stream<<endl;
- }
- stream<<endl;
- return stream;
- }
- istream &operator>>(istream &stream, Tmatrix** obj)
- {
- int N,M;
- cout<<"Введите размерность матрицы"<<endl;
- cout<<"N=";
- cin>>N;
- cout<<"M=";
- cin>>M;
- *obj = new Tmatrix(N,M);
- cout<<"Введите матрицу"<<endl;
- for(int i=0;i<(*obj)->n;i++)
- for(int j=0;j<(*obj)->m;j++)
- cin>>(*obj)->matrix[i][j];
- return stream;
- }
- int main()
- {
- setlocale(LC_ALL,"RU");
- Tmatrix* A;
- Tmatrix* B;
- cin>>&A;
- cout<<A;
- cin>>&B;
- cout<<B;
- Tmatrix* sumC;
- Tmatrix* negC;
- Tmatrix* multC;
- sumC = A->sum(B);
- negC = A->neg(B);
- multC = A->mult(B);
- if(sumC!=NULL)
- {
- cout<<"Сумма матриц А и В:"<<endl;
- sumC->printMatrix();
- }else
- cout<<"Суммирование невозможно"<<endl;
- if(negC!=NULL)
- {
- cout<<"Разность матриц А и В:"<<endl;
- negC->printMatrix();
- }else
- cout<<"Вычитание невозможно"<<endl;
- if(multC!=NULL)
- {
- cout<<"Произведение матриц A и B"<<endl;
- multC->printMatrix();
- }else
- cout<<"Умножение невозможно"<<endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement