Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #ifndef AGH_MATRIX
  2. #define AGH_MATRIX
  3. #include <cstdarg>
  4. #include <iostream>
  5. //------------------------------------------------
  6.  
  7. template<typename T>
  8. class aghMatrix{
  9. private:
  10. T** matrix;
  11. int rows,cols;
  12.  
  13. //-------------------------------------
  14.  
  15. void resize(int x, int y){
  16. if(matrix != NULL){
  17. for(int i=0;i<x;i++)
  18. delete [] matrix[i];
  19.  
  20. delete [] matrix;
  21. matrix = NULL;
  22. }
  23.  
  24. T** matrix = new T*[x];
  25. for(int i=0;i<x;i++)
  26. matrix[i] = new T[y];
  27.  
  28. rows = x;
  29. cols = y;
  30.  
  31. }
  32.  
  33. //-------------------------------
  34.  
  35.  
  36. public:
  37. void setItem(int x, int y, T n){
  38. if(matrix!=NULL) if(x<=rows && y<=cols && x>0 && y>0) matrix[x][y]=n;
  39. }
  40.  
  41. //-----------------------------------
  42. void setItems(T* items){
  43. int x=0;
  44.  
  45. for(int i=0; i<rows; i++) for (int j=0; j<cols; j++)
  46. { matrix[i][j]=items[x]; x++; }
  47. }
  48. //TODO: obsluga wyjatkow
  49.  
  50. //-----------------------------------
  51. void setItems(int r, int c, ... ){
  52. va_list arg;
  53. va_start(arg, c);
  54.  
  55. resize(r,c);
  56.  
  57. for(int i=0; i<rows; i++)
  58. for(int j=0; j<cols; j++)
  59. matrix[i][j]=arg;
  60.  
  61.  
  62. va_end(arg);
  63. }
  64.  
  65. //-----------------------------------
  66.  
  67. aghMatrix():rows(0),cols(0),matrix(NULL){}
  68.  
  69. //-----------------------------------
  70.  
  71. aghMatrix(int x, int y){
  72.  
  73. resize(x,y);
  74. for(int i=0; i<rows; i++)
  75. for(int j=0; j<cols; j++)
  76. matrix[i][j]=0;
  77. }
  78.  
  79. //-----------------------------------
  80.  
  81. };
  82.  
  83. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement