Advertisement
TwiNNeR

Matrica cpp

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