Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QtCore/QCoreApplication>
- #include <cstdlib>
- #include <ctime>
- #include <iostream>
- using namespace std;
- class matrix
- {
- private:
- int ** mx;
- int h, w;
- public:
- matrix (int h = 2, int w = 2);
- matrix (const matrix &);
- ~matrix ();
- matrix operator + (const matrix&);
- matrix operator * (const matrix&);
- matrix operator = (const matrix&);
- friend istream & operator >> ( istream &,const matrix &);
- friend ostream & operator << ( ostream &,const matrix &);
- void fillRandom(int scale = 10);
- };
- matrix :: matrix (int h , int w)
- {
- // first
- this->h = h; this->w = w;
- mx = new int * [h];
- for (int i = 0; i < h; i ++)
- mx [i] = new int [w];
- }
- matrix :: matrix (const matrix & m)
- {
- h = m.h;
- w = m.w;
- mx = new int * [h];
- for (int i = 0; i < h; i ++)
- mx [i] = new int [w];
- for (int i = 0; i < h; i ++)
- for (int j = 0; j < w; j ++)
- mx [i] [j] = m.mx [i] [j];
- }
- matrix :: ~matrix ()
- {
- for(int i = 0; i < h; i ++)
- delete mx [i];
- delete [] mx;
- }
- matrix matrix :: operator + (const matrix& m2)
- {
- matrix m3;
- if (h==m2.h && w==m2.w)
- {
- m3.h = h;
- m3.w = w;
- for (int i = 0; i < h; i ++)
- for (int j = 0; j < w; j ++)
- m3.mx [i] [j] = mx [i] [j] + m2.mx [i] [j];
- }
- return m3;
- }
- matrix matrix :: operator = (const matrix& m2)//????????????
- {
- h = m2.h;
- w = m2.w;
- for (int i = 0; i < h; i ++)
- for (int j = 0; j < w; j ++)
- mx [i] [j] = m2.mx [i] [j];
- return *this;
- }
- matrix matrix :: operator * (const matrix &m2)
- {
- matrix m3;
- int work;
- if (w == m2.h)
- {
- m3.h = h;
- m3.w = m2.w;
- for (int i = 0; i < m3.h; i ++)
- for (int j = 0; j < m3.w; j ++)
- {
- work = 0;
- for (int k = 0; k < w; k ++)
- work += (mx [i] [k]) * (m2.mx [k] [j]);
- m3.mx [i] [j] = work;
- }
- }
- return m3;
- }
- istream & operator >> ( istream & cin_,const matrix & m)
- {
- cin_ >> m.h >> m.w;
- for (int i = 0; i < m.h; i ++)
- for (int j = 0; j < m.w; j ++)
- cin_ >> m.mx [i] [j];
- return cin_;
- }
- ostream & operator << ( ostream & cout_,const matrix & m)
- {
- for (int i = 0; i < m.h; i ++)
- {
- for (int j = 0; j < m.w; j ++)
- cout_ << m.mx [i] [j] << ' ';
- cout_ << endl;
- }
- return cout_;
- }
- void matrix::fillRandom(int scale){
- for (int i = 0; i < h; i ++){
- srand(time(NULL) + rand()%195);
- for (int j = 0; j < w; j ++)
- mx [i][j] = rand()%(scale);
- }
- }
- int main(int argc, char *argv[])
- {
- QCoreApplication app(argc, argv);
- matrix a, b, c;
- a.fillRandom();
- b.fillRandom();
- cout << a << endl << b << endl;
- c=a+b;
- cout << c << endl ;
- c=a*b;
- cout << c << endl;
- return app.exec();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement