Advertisement
pawelgalka

mat

Jun 25th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. class Mat : public std::vector<double *>{
  2. public:
  3.     std::vector<int> dlugosci;
  4.     Mat(){};
  5.     ~Mat(){
  6.     }
  7.     double* addRow(int size){
  8.         push_back(new double[size]);
  9.         dlugosci.push_back(size);
  10.         return back();
  11.     }
  12.  
  13.     void deleteRow(int r){
  14.         if (r<0 || r>=size() ) throw -1;
  15.  
  16.         else{
  17.             int licznik=0;
  18.             vector<double *>::iterator it;
  19.             for (it=begin(); it!=end(); ++it){
  20.                 if (licznik==r){
  21.                     delete[] at(r);
  22.                     erase(it);
  23.                     return;
  24.                 }
  25.                 licznik++;
  26.             }
  27.         }
  28.     }
  29.  
  30.     double &operator()(int row, int col){
  31.         if (row<0 || row>=size() || col<0 || col>=dlugosci[row]) throw -1;
  32.         else return at(row)[col];
  33.     }
  34.  
  35.     Mat operator+(Mat &other){
  36.         Mat result;
  37.         int rozmiar = max(dlugosci.size(),other.dlugosci.size());
  38.         for (int i=0; i<rozmiar; ++i){
  39.             int maxi = max(dlugosci[i],other.dlugosci[i]);
  40.             result.addRow(maxi);
  41.             for (int j=0; j<maxi; j++){
  42.                 if (j<dlugosci[i] && j<other.dlugosci[i]) result(i,j) = at(i)[j]+other.at(i)[j];
  43.                 else if (j>=dlugosci[i] && j<other.dlugosci[i]) result(i,j) = other.at(i)[j];
  44.                 else if (j<dlugosci[i] && j>=other.dlugosci[i]) result(i,j) = at(i)[j];
  45.  
  46.             }
  47.         }
  48.         return result;
  49.     }
  50. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement