Advertisement
uberzawadaog

macierz_cpp

May 27th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #include "macierze2.h"
  2.  
  3. Matrix::Matrix(int dr, int dc){
  4.  
  5. wektor=new Vector *[dr];
  6.  
  7. if(wektor==NULL){
  8. r=0;
  9. c=0;
  10. return ;
  11. }
  12.  
  13. for(int i=0; i<dr; ++i)
  14. wektor[i]=new Vector(dc);
  15.  
  16. r=dr;
  17. c=dc;
  18. }
  19.  
  20. Matrix::Matrix(const Matrix &a){
  21.  
  22. wektor=new Vector *[a.r];
  23.  
  24.  
  25. for(int i=0; i<a.r; ++i){
  26. wektor[i]=new Vector(a.c);
  27. *(wektor[i])=*(a.wektor[i]);
  28. }
  29.  
  30. r=a.r;
  31. c=a.c;
  32. }
  33.  
  34. Matrix::~Matrix(){
  35.  
  36. delete [] wektor;
  37. }
  38.  
  39. Vector& Matrix::operator[] (size_t index){
  40.  
  41. return *(wektor[index]);
  42. }
  43.  
  44.  
  45.  
  46. Matrix operator+(const Matrix &a, const Matrix &b){
  47.  
  48. Matrix c(a.r,a.c);
  49.  
  50. for(int i=0; i<a.r; ++i){
  51. *(c.wektor[i])=*(a.wektor[i])+*(b.wektor[i]);
  52. }
  53.  
  54. return c;
  55. }
  56.  
  57. Matrix operator-(const Matrix &a, const Matrix &b){
  58.  
  59. Matrix c(a.r,a.c);
  60.  
  61. for(int i=0; i<a.r; ++i){
  62. *(c.wektor[i])=*(a.wektor[i])-*(b.wektor[i]);
  63. }
  64.  
  65. return c;
  66. }
  67.  
  68. Matrix operator*(Matrix &a,Matrix &b){
  69.  
  70. Matrix d(b.r,a.c);
  71.  
  72. for(int i=0; i < a.c; ++i)
  73. {
  74. for(int j=0; j < b.r; ++j)
  75. {
  76. for(int k=0; k < a.c; ++k)
  77. {
  78. d[i][j]+=a[i][k]*b[k][j];
  79. }
  80. }
  81. }
  82.  
  83. return d;
  84. }
  85.  
  86. Matrix operator*(const int &a, const Matrix &b){
  87.  
  88. Matrix d(b.r,b.c);
  89.  
  90. for(int i=0; i<b.r; ++i)
  91. *(d.wektor[i])=*(b.wektor[i])*a;
  92.  
  93. return d;
  94. }
  95.  
  96. Matrix& Matrix::operator+=(const Matrix &a){
  97.  
  98. for(int i=0; i<a.r; ++i)
  99. *(wektor[i])+=*(a.wektor[i]);
  100.  
  101. return *this;
  102. }
  103.  
  104. Matrix& Matrix::operator-=(const Matrix &a){
  105.  
  106. for(int i=0; i<a.r; ++i)
  107. *(wektor[i])-=*(a.wektor[i]);
  108.  
  109. return *this;
  110. }
  111.  
  112. Matrix& Matrix::operator*=(const int &a){
  113.  
  114. for(int i=0; i<r; ++i)
  115. *(wektor[i])*=a;
  116.  
  117. return *this;
  118. }
  119.  
  120. bool operator==(const Matrix &a, const Matrix &b){
  121.  
  122. if(a.r!=b.r || a.c!=b.c)
  123. return false;
  124.  
  125. for(int i=0; i<a.r; ++i){
  126. if(a.wektor[i]!=b.wektor[i])
  127. return false;
  128. else
  129. return true;
  130. }
  131. }
  132.  
  133. bool operator!=(const Matrix &a, const Matrix &b){
  134.  
  135. return(!(a==b));
  136. }
  137.  
  138. std::ostream& operator<< (std::ostream& os, Matrix &a){
  139.  
  140. for(int i = 0; i < a.r; i++)
  141. os << *(a.wektor[i]) << std::endl;
  142.  
  143. return os;
  144. }
  145.  
  146. std::istream& operator>>(std::istream& is, Matrix &a){
  147.  
  148. for(int i=0; i<a.r; i++)
  149. is >> *(a.wektor[i]);
  150.  
  151. return is;
  152. }
  153.  
  154. Matrix& Matrix::operator=(const Matrix &a){
  155.  
  156. if(*this==a)
  157. return *this;
  158.  
  159. if(r!=a.r || c!=a.c)
  160. std::cout << "nie mozna przypisywac macierzy o roznych rozmiarach" <<std::endl;
  161.  
  162.  
  163. for(int i=0; i<r; i++)
  164. *(wektor[i])=*(a.wektor[i]);
  165.  
  166. r=a.r;
  167. c=a.c;
  168. return *this;
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement