Guest User

Untitled

a guest
Jan 24th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <boost/thread/thread.hpp>
  3. #define parity(x) (1-(x%2)*2)
  4.  
  5.  
  6.  
  7.  
  8.  
  9. using namespace std;
  10.  
  11. class Macierz {
  12.  
  13. private:
  14.     int rows, cols;
  15.     int **tab;
  16.  
  17.  
  18. public:
  19.  
  20.  
  21.  
  22.     Macierz()
  23.     {
  24.     rows=0;
  25.     cols=0;
  26.     tab[0][0]=1;};
  27.  
  28.     Macierz & operator* (  Macierz &q)
  29.     {
  30.         static Macierz temp(getRows(),q.getCols());
  31.  
  32.         if(getCols() == q.getRows())
  33.         {
  34.             for(int i=0;i<getRows();++i)
  35.                  for(int j=0;j<q.getCols();++j)
  36.                     for(int k=0;k<getCols();++k)
  37.                        temp.setValue(temp.at(i,j)+at(i,k)*q.at(k,j),i,j);
  38.         return temp;
  39.         }
  40.         else return q;
  41.     };
  42.  
  43.  
  44.     int determine;
  45.     Macierz(int rows,int cols)
  46.         {
  47.         this->rows=rows;
  48.         this->cols=cols;
  49.  
  50.        int (**tab) = new int *[rows];
  51.  
  52.         for(int i=0;i<rows;++i)
  53.             tab[i] = new int [cols];
  54.  
  55.         for(int i=0;i<rows;++i)
  56.             for(int j=0;j<cols;++j)
  57.                 if(i==j) tab[i][j]=1;
  58.         else tab[i][j]==0;
  59.  
  60.         this->tab=tab;
  61.         }
  62.  
  63. ~Macierz()
  64. {
  65. for(int i=0;i<cols;++i) delete [] tab[i];
  66.    
  67. delete [] tab;
  68. }
  69.  
  70.  
  71. void setSize(int rows,int cols)
  72. {
  73.     this->rows=rows;
  74.     this->cols=cols;
  75.     Macierz(rows,cols);
  76. }
  77.  
  78. void generateRandom(int n)
  79. {
  80.     for(int i=0;i<this->rows;++i)
  81.     for(int j=0;j<cols;++j)
  82.             tab[i][j]= (rand() % n);
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89. Macierz Minor(int row, int col)
  90. {
  91. Macierz temp(rows-1,cols-1);
  92.  
  93.  
  94. for(int i=0;i<rows-1;++i)
  95.     for(int j=0;j<cols-1;++j)
  96.         temp.setValue(at(i < row ? i : i+1,j < col ? j : j+1),i,j);
  97.  
  98. return temp;
  99. }
  100.  
  101.  
  102.  
  103. int det()
  104. {
  105.     if(rows == cols && rows == 1) return at(0,0);
  106.     else {
  107.  
  108.     int X=0;
  109.  
  110.     for(int i=0;i<rows;i++)
  111.             {
  112.             X=X+(Minor(i,0).det())*at(i,0)*parity(i+0);
  113.             }
  114.  
  115.             determine=X;
  116.             return X;
  117.             }
  118. }
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. Macierz& operator=(Macierz const &q) {
  126. if (this != &q) {
  127.     delete tab; // free resource;
  128.     tab = 0;
  129.     tab = q.tab;
  130.     rows=q.rows;
  131.     cols=q.cols;
  132.     }
  133. return *this;
  134. };
  135.  
  136.  
  137.  
  138.     Macierz& operator+ (  Macierz &q) {
  139.  
  140.         static Macierz temp(q.getRows(),q.getCols());
  141.  
  142.         if(q.getRows()==getRows() && q.getCols()==getCols() )
  143.             {
  144.             for(int i=0;i<q.getRows();++i)
  145.                 for(int j=0;j<q.getCols();++j)
  146.                    temp.setValue(q.at(i,j)+at(i,j),i,j);
  147.           return temp;
  148.             }
  149.         else
  150.             return q;
  151.         };
  152.  
  153.  
  154.     Macierz& operator- (  Macierz &q) {
  155.  
  156.         static Macierz temp(q.getRows(),q.getCols());
  157.          if(q.getRows()==getRows() && q.getCols()==getCols() ) {
  158.         for(int i=0;i<q.getRows();++i)
  159.             for(int j=0;j<q.getCols();++j)
  160.                 temp.setValue(q.at(i,j)-at(i,j),i,j);
  161.         return temp;
  162.          }
  163.          else
  164.              return q;
  165.     };
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.     int at(int row, int col)
  174.     {
  175.         if(row>rows || col > cols)
  176.             {
  177.             cout << "index excedes the size of matrix!" << endl ;
  178.             return 666;
  179.             }
  180.         else return tab[row][col];
  181.     }
  182.  
  183.     int getCols()
  184.     {
  185.         return cols;
  186.     }
  187.  
  188.     int getRows()
  189.     {
  190.         return rows;
  191.     }
  192.  
  193.     void setValue(int value,int row,int col)
  194.     {
  195.         tab[row][col]=value;
  196.     }
  197.  
  198. };
  199.  
  200.  
  201. ostream & operator<< (ostream &wyjscie, Macierz &s)
  202. {
  203.  
  204.     for(int i=0;i< s.getRows();++i)
  205.     {
  206.         for(int j=0;j<s.getCols();++j)
  207.         {
  208.             wyjscie << s.at(i,j) << ", ";
  209.         }
  210.         wyjscie << ";" << endl ;
  211.     }
  212.     wyjscie << endl ;
  213.  
  214.    return wyjscie;
  215. }
Add Comment
Please, Sign In to add comment