Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. #include <iostream>
  2. #include "cmake-build-debug/declaration.h"
  3. using namespace std ;
  4.  
  5.  
  6. int main() {
  7. Matrix a(3, 4);
  8. Matrix b(2,2);
  9. b.addElem(1,0,0);
  10. b.addElem(2,1,0);
  11. a.addElem(1,0,0);
  12. a.addElem(2,0,1);
  13. a.addElem(9,1,0);
  14. a.addElem(10,1,1);
  15. for (int i=0;i<3;i++)
  16. {
  17. for (int j=0;j<4;j++)
  18. {
  19. if (a.isEx(i,j))
  20. {
  21. cout << a.getElem(i,j)*(-1)<< " ";
  22. } else cout << "Null ";
  23. }
  24. cout <<endl;
  25. }
  26. cout<<endl;
  27. a.Check(a,100);
  28. cout<<endl;
  29.  
  30. for (int i=0;i<a.getN();i++)
  31. {
  32. for (int j=0;j<a.getM();j++)
  33. {
  34. if (a.isEx(i,j))
  35. {
  36. cout << a.getElem(i,j)*(-1)<< " ";
  37. } else cout << "Null ";
  38. }
  39. cout <<endl;
  40. }
  41. cout<<endl;
  42. a=b;
  43.  
  44. for (int i=0;i<a.getN();i++)
  45. {
  46. for (int j=0;j<a.getM();j++)
  47. {
  48. if (a.isEx(i,j))
  49. {
  50. cout << a.getElem(i,j)*(-1)<< " ";
  51. } else cout << "Null ";
  52. }
  53. cout <<endl;
  54. }
  55. cout<<endl;
  56.  
  57. a.sum(10,0,1);
  58.  
  59. for (int i=0;i<a.getN();i++)
  60. {
  61. for (int j=0;j<a.getM();j++)
  62. {
  63. if (a.isEx(i,j))
  64. {
  65. cout << a.getElem(i,j)*(-1)<< " ";
  66. } else cout << "Null ";
  67. }
  68. cout <<endl;
  69. }
  70. return 0;
  71. }
  72.  
  73.  
  74. // main
  75.  
  76. //
  77. // Created by brfh on 21.04.18.
  78. //
  79.  
  80.  
  81. #include <iostream>
  82. using namespace std ;
  83. #include <algorithm>
  84. #include "declaration.h"
  85.  
  86. Matrix:: Matrix(int n,int m)
  87. {
  88. this->n=n;
  89. this->m=m;
  90. arr = new Point*[n];
  91. for (int j=0;j<n;j++)
  92. {
  93. arr[j]=new Point[m];
  94. }
  95. }
  96.  
  97. void Matrix::addElem(int a,int i,int j)
  98. {
  99. this->arr[i][j].v=a;
  100. this->arr[i][j].ex= true;
  101. }
  102.  
  103. int& Matrix::getElem(int i,int j)
  104. {
  105. return (this->arr[i][j].v);
  106. }
  107.  
  108. bool Matrix::isEx(int i,int j)
  109. {
  110. return this->arr[i][j].ex;
  111. }
  112.  
  113. Matrix::Matrix(const Matrix &obj)
  114. {
  115. arr = new Point*[obj.n];
  116. for (int i =0;i<obj.n;i++)
  117. {
  118. arr[i]=new Point[obj.m];
  119. }
  120. this->n=obj.n;
  121. this->m=obj.m;
  122. for (int i =0;i<obj.n;i++)
  123. {
  124. std::copy(obj.arr[i],obj.arr[i]+obj.n,arr[i]);
  125. }
  126. }
  127.  
  128.  
  129. int Matrix::getN()
  130. {
  131. return this->n;
  132. }
  133.  
  134. int Matrix::getM()
  135. {
  136. return this->m;
  137. }
  138.  
  139. Matrix::~Matrix()
  140. {
  141. delete arr;
  142. }
  143.  
  144. int& Matrix::operator[](int index) {}
  145.  
  146. void Matrix::sum(int x, int i, int j)
  147. {
  148. for (int k=0;k<this->m;k++)
  149. {
  150. this->arr[j][k].v+=this->arr[i][k].v*x;
  151. }
  152. }
  153.  
  154. void Matrix::Check(Matrix a,int x)
  155. {
  156. for (int i=0;i<n;i++)
  157. {
  158. for (int j=0;j<m;j++)
  159. {
  160. a.addElem(a.arr[i][j].v*x,i,j);
  161. cout<<a.getElem(i,j) << " " ;
  162. }
  163. cout<<endl;
  164. }
  165.  
  166. cout<<endl;
  167. }
  168.  
  169. Matrix& Matrix::operator=(const Matrix &obj)
  170. {
  171. if (this != &obj)
  172. {
  173. Point **a=new Point*[obj.n];
  174. for (int i=0;i<obj.n;i++)
  175. {
  176. a[i]=new Point[obj.m];
  177. }
  178. for (int i=0; i<obj.n;i++)
  179. {
  180. std::copy(obj.arr[i],obj.arr[i]+obj.n,a[i]);
  181. }
  182. for (int i=0; i<n; delete arr[i++]);
  183. delete arr;
  184. n=obj.n;
  185. m=obj.m;
  186. arr=a;
  187.  
  188. }
  189. return *this;
  190. }
  191.  
  192. Point::Point()
  193. {
  194. this->ex= false;
  195. this->v=0;
  196. }
  197.  
  198. //
  199. //
  200. // Created by brfh on 21.04.18.
  201. //
  202.  
  203. #ifndef LAB1_DECLARATION_H
  204. #define LAB1_DECLARATION_H
  205.  
  206. #endif //LAB1_DECLARATION_H
  207.  
  208.  
  209. class Point
  210. {
  211. public:
  212. int v;
  213. bool ex;
  214. Point();
  215. };
  216.  
  217. class Matrix
  218. {
  219. private:
  220. int n, m;
  221. Point** arr;
  222. public:
  223. Matrix&operator=(const Matrix &obj);
  224. int getM();
  225. int getN();
  226. void cop(Matrix a);
  227. void Check(Matrix a,int x);
  228. int& operator [] (int index);
  229. bool isEx(int i,int j);
  230. int& getElem(int i, int j);
  231. void addElem(int a,int i,int j);
  232. void sum(int x,int i, int j);
  233. Matrix(int n,int m);
  234. Matrix(const Matrix &obj);
  235. virtual ~Matrix();
  236.  
  237. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement