Advertisement
rnort

lab2_3_Ilya

Oct 2nd, 2011
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <QtCore/QCoreApplication>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. class matrix
  9. {
  10. private:
  11.     int ** mx;
  12.     int h, w;
  13. public:
  14.     matrix (int h = 2, int w = 2);
  15.     matrix (const matrix &);
  16.     ~matrix ();
  17.     matrix operator + (const matrix&);
  18.     matrix operator * (const matrix&);
  19.     matrix operator = (const matrix&);
  20.     friend istream & operator >> ( istream &,const  matrix &);
  21.     friend ostream & operator << ( ostream &,const  matrix &);
  22.     void fillRandom(int scale = 10);
  23. };
  24.  
  25. matrix :: matrix (int h , int w)
  26. {
  27.     //  first
  28.     this->h = h; this->w = w;
  29.     mx = new int * [h];
  30.     for (int i = 0; i < h; i ++)
  31.         mx [i] = new int [w];
  32. }
  33.  
  34. matrix :: matrix (const matrix & m)
  35. {
  36.     h = m.h;
  37.     w = m.w;
  38.     mx = new int * [h];
  39.     for (int i = 0; i < h; i ++)
  40.         mx [i] = new int [w];
  41.     for (int i = 0; i < h; i ++)
  42.         for (int j = 0; j < w; j ++)
  43.             mx [i] [j] = m.mx [i] [j];
  44. }
  45.  
  46. matrix :: ~matrix ()
  47. {
  48.     for(int i = 0; i < h; i ++)
  49.         delete mx [i];
  50.     delete [] mx;
  51. }
  52.  
  53. matrix matrix :: operator + (const matrix& m2)
  54. {
  55.     matrix m3;
  56.     if (h==m2.h && w==m2.w)
  57.     {
  58.         m3.h = h;
  59.         m3.w = w;
  60.         for (int i = 0; i < h; i ++)
  61.             for (int j = 0; j < w; j ++)
  62.                 m3.mx [i] [j] = mx [i] [j] + m2.mx [i] [j];
  63.     }
  64.     return m3;
  65. }
  66.  
  67. matrix matrix :: operator = (const matrix& m2)//????????????
  68. {
  69.     h = m2.h;
  70.     w = m2.w;
  71.     for (int i = 0; i < h; i ++)
  72.         for (int j = 0; j < w; j ++)
  73.             mx [i] [j] = m2.mx [i] [j];
  74.     return *this;
  75. }
  76.  
  77. matrix matrix :: operator * (const matrix &m2)
  78. {
  79.     matrix m3;
  80.     int work;
  81.     if (w == m2.h)
  82.     {
  83.         m3.h = h;
  84.         m3.w = m2.w;
  85.         for (int i = 0; i < m3.h; i ++)
  86.             for (int j = 0; j < m3.w; j ++)
  87.             {
  88.                 work = 0;
  89.                 for (int k = 0; k < w; k ++)
  90.                     work += (mx [i] [k]) * (m2.mx [k] [j]);
  91.                 m3.mx [i] [j] = work;
  92.             }
  93.     }
  94.     return m3;
  95. }
  96.  
  97. istream & operator >> ( istream & cin_,const  matrix & m)
  98. {
  99.     cin_ >> m.h >> m.w;
  100.     for (int i = 0; i < m.h; i ++)
  101.         for (int j = 0; j < m.w; j ++)
  102.             cin_ >> m.mx [i] [j];
  103.     return cin_;
  104. }
  105.  
  106. ostream & operator << ( ostream & cout_,const  matrix & m)
  107. {
  108.     for (int i = 0; i < m.h; i ++)
  109.     {
  110.         for (int j = 0; j < m.w; j ++)
  111.             cout_ << m.mx [i] [j] << ' ';
  112.         cout_ << endl;
  113.     }
  114.     return cout_;
  115. }
  116. void matrix::fillRandom(int scale){
  117.  
  118.  
  119.  
  120.     for (int i = 0; i < h; i ++){
  121.         srand(time(NULL) + rand()%195);
  122.         for (int j = 0; j < w; j ++)
  123.             mx [i][j] = rand()%(scale);
  124.     }
  125. }
  126.  
  127. int main(int argc, char *argv[])
  128. {
  129.     QCoreApplication app(argc, argv);
  130.  
  131.     matrix a, b, c;
  132.     a.fillRandom();
  133.     b.fillRandom();
  134.     cout << a <<  endl << b << endl;
  135.  
  136.     c=a+b;
  137.     cout << c << endl ;
  138.  
  139.     c=a*b;
  140.     cout << c << endl;
  141.  
  142.     return app.exec();
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement