Advertisement
Proff_Ust

Lab2_new

Dec 25th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <clocale>
  3.  
  4. using namespace std;
  5.  
  6. class Tmatrix
  7. {
  8. private:
  9.     int n, m;
  10.     int **matrix;
  11.  
  12. public:
  13.     Tmatrix(int newn, int newm)
  14.     {
  15.         matrix = new int*[newm];
  16.         for(int i=0;i<newm;i++)
  17.             matrix[i] = new int[newn];
  18.  
  19.         for(int i=0;i<newn;i++)
  20.             for(int j=0;j<newm;j++)
  21.                 this->matrix[i][j] = 0;
  22.  
  23.         n = newn;
  24.         m = newm;
  25.     }
  26.  
  27.     Tmatrix(){}
  28.     int printMatrix()
  29.     {
  30.         for(int i=0;i<this->n;i++)
  31.         {
  32.             for(int j=0;j<this->m;j++)
  33.                 cout<<this->matrix[i][j]<<" ";
  34.             cout<<endl;
  35.         }
  36.         cout<<endl;
  37.         return 0;
  38.     }
  39.  
  40.     int getElem(int i, int j)
  41.     {
  42.         if(i<this->n && j<this->m)
  43.             return this->matrix[i][j];
  44.         else
  45.             return -1;
  46.     }
  47.  
  48.     int setElem(int i, int j, int elem)
  49.     {
  50.         if(i<this->n && j<this->m)
  51.         {
  52.             this->matrix[i][j] = elem;
  53.             return 0;
  54.         }
  55.         else
  56.             return -1;
  57.     }
  58.  
  59.  
  60.     int fillMatrix()
  61.     {
  62.         for(int i=0;i<this->n;i++)
  63.             for(int j=0;j<this->m;j++)
  64.                 this->matrix[i][j] = rand() % 10;
  65.         return 0;
  66.     }
  67.  
  68.     int getN()
  69.     {
  70.         return n;
  71.     }
  72.  
  73.     int getM()
  74.     {
  75.         return m;
  76.     }
  77.  
  78.     Tmatrix* sum(Tmatrix* sumB)
  79.     {
  80.         if(this->n==sumB->getN() && this->m==sumB->getM())
  81.         {
  82.             Tmatrix* resSum = new Tmatrix(this->n,this->m);
  83.             for(int i=0;i<this->n;i++)
  84.                 for(int j=0;j<this->m;j++)
  85.                     resSum->setElem(i,j,this->matrix[i][j]+sumB->getElem(i,j));
  86.             return resSum;
  87.         }else
  88.             return NULL;
  89.     }
  90.  
  91.     Tmatrix* neg(Tmatrix* negB)
  92.     {
  93.         if(this->n==negB->getN() && this->m==negB->getM())
  94.         {
  95.             Tmatrix* resNeg = new Tmatrix(this->n,this->m);
  96.             for(int i=0;i<this->n;i++)
  97.                 for(int j=0;j<this->m;j++)
  98.                     resNeg->setElem(i,j,this->matrix[i][j]-negB->getElem(i,j));
  99.             return resNeg;
  100.         }else
  101.             return NULL;
  102.     }
  103.  
  104.     Tmatrix* mult(Tmatrix* multB)
  105.     {
  106.         if(this->n==multB->getM() && this->m==multB->getN())
  107.         {
  108.             Tmatrix* resMult = new Tmatrix(this->n, multB->getM());
  109.             int premult;
  110.             for(int i=0; i<this->n;i++)
  111.                 for(int j=0; j<multB->getM();j++)
  112.                     for(int k=0; k<this->m;k++)
  113.                     {
  114.                         premult = resMult->getElem(i,j);
  115.                         resMult->setElem(i, j, premult+this->matrix[i][k] * multB->getElem(k,j));
  116.                     }
  117.             return resMult;
  118.         }else
  119.             return NULL;
  120.     }
  121.  
  122.     friend ostream &operator<<(ostream &stream, Tmatrix* obj);
  123.     friend istream &operator>>(istream &stream, Tmatrix** obj);
  124.  
  125.     ~Tmatrix()
  126.     {
  127.         for(int i=0;i<n;i++)
  128.             delete[] matrix[i];
  129.         delete[] matrix;
  130.     }
  131. };
  132.  
  133.  
  134. ostream &operator<<(ostream &stream, Tmatrix* obj)
  135. {
  136.     for(int i=0;i<obj->n;i++)
  137.     {
  138.         for(int j=0;j<obj->m;j++)
  139.             stream<<obj->matrix[i][j]<<" ";
  140.         stream<<endl;
  141.     }
  142.     stream<<endl;
  143.  
  144.     return stream;
  145. }
  146.  
  147. istream &operator>>(istream &stream, Tmatrix** obj)
  148. {
  149.     int N,M;
  150.     cout<<"Введите размерность матрицы"<<endl;
  151.     cout<<"N=";
  152.     cin>>N;
  153.     cout<<"M=";
  154.     cin>>M;
  155.     *obj = new Tmatrix(N,M);
  156.     cout<<"Введите матрицу"<<endl;
  157.     for(int i=0;i<(*obj)->n;i++)
  158.         for(int j=0;j<(*obj)->m;j++)
  159.             cin>>(*obj)->matrix[i][j];
  160.     return stream;
  161. }
  162.  
  163. int main()
  164. {
  165.     setlocale(LC_ALL,"RU");
  166.  
  167.     Tmatrix* A;
  168.     Tmatrix* B;
  169.     cin>>&A;
  170.     cout<<A;
  171.  
  172.     cin>>&B;
  173.     cout<<B;
  174.  
  175.     Tmatrix* sumC;
  176.     Tmatrix* negC;
  177.     Tmatrix* multC;
  178.     sumC = A->sum(B);
  179.     negC = A->neg(B);
  180.     multC = A->mult(B);
  181.  
  182.     if(sumC!=NULL)
  183.     {
  184.         cout<<"Сумма матриц А и В:"<<endl;
  185.         sumC->printMatrix();
  186.     }else
  187.         cout<<"Суммирование невозможно"<<endl;
  188.  
  189.     if(negC!=NULL)
  190.     {
  191.         cout<<"Разность матриц А и В:"<<endl;
  192.         negC->printMatrix();
  193.     }else
  194.         cout<<"Вычитание невозможно"<<endl;
  195.  
  196.     if(multC!=NULL)
  197.     {
  198.         cout<<"Произведение матриц A и B"<<endl;
  199.         multC->printMatrix();
  200.     }else
  201.         cout<<"Умножение невозможно"<<endl;
  202.     system("pause");
  203.     return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement