Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.78 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <math.h>
  4. #include <iomanip>
  5. #include <stdlib.h>
  6. #include <conio.h>
  7. #include <time.h>
  8. #include <string>
  9.  
  10.  
  11. using namespace std;
  12.  
  13. struct cmplx
  14. {
  15. double r;
  16. double i;
  17. };
  18.  
  19. class Matrix
  20. {
  21. private:
  22. cmplx** a;
  23. int x;
  24. int y;
  25. string name;
  26. friend void cmPrint(cmplx a);
  27. public:
  28. //static int n;
  29. int n;
  30. Matrix()
  31. {
  32. x=3;
  33. y=3;
  34. cmplx** a=new cmplx*[y];
  35. for (int i = 0; i < y; i++)
  36. a[i] = new cmplx[x];
  37. for (int i = 0; i < y; i++)
  38. for (int j = 0; j < x; j++)
  39. {
  40. am(i, j).r = 0;
  41. am(i, j).i = 0;
  42. }
  43. name = "Матрица";
  44. name += n;
  45. n++;
  46. cout << endl << name;
  47. cout<<endl<<"DefaultConstructor";
  48. }
  49. Matrix(int i, int j)
  50. {
  51. x=j;
  52. y=i;
  53. cmplx** a = new cmplx*[y];
  54. for (int i = 0; i < y; i++)
  55. a[i] = new cmplx[x];
  56. for (int i = 0; i < y; i++)
  57. for (int j = 0; j < x; j++)
  58. {
  59. am(i, j).r = 0;
  60. am(i, j).i = 0;
  61. }
  62. name = "Матрица";
  63. name += n;
  64. n++;
  65. cout << endl << name;
  66. cout<<endl<<"CustomConstructor";
  67. }
  68. Matrix(Matrix &b)
  69. {
  70. x=b.x;
  71. y=b.y;
  72. cmplx** a=new cmplx*[y];
  73. for (int i = 0; i < y; i++)
  74. a[i]=new cmplx[x];
  75. for (int i = 0; i < y; i++)
  76. for (int j = 0; j < x; j++)
  77. am(i, j) = b.am(i, j);
  78. name = "Матрица";
  79. name += n;
  80. n++;
  81. cout << endl << name;
  82. cout<<endl<<"CopyConstructor";
  83. }
  84. ~Matrix()
  85. {
  86. for (int i = 0; i < y; i++)
  87. delete [] a[i];
  88. delete [] a;
  89.  
  90. cout<<endl<<name<<" destructor";
  91. }
  92.  
  93. void matrixIn()
  94. {
  95. cout << endl << "Ready to enter matrix. It is COMPLEX, don't forget Press any button to continue" << endl;
  96. _getch();
  97. for (int i = 0; i < y; i++)
  98. for (int j = 0; j < x; j++)
  99. {
  100. cin >> am(i, j).r;
  101. cin >> am(i, j).i;
  102. }
  103. }
  104.  
  105. void print()
  106. {
  107. cout << endl << "PrintingOnScreen:" <<name<< endl;
  108. for (int i = 0; i < y; i++)
  109. {
  110. cout << endl;
  111. for (int j = 0; j < x; j++)
  112. cmPrint(am(i, j));
  113. }
  114. }
  115.  
  116. void print(int y1, int x1)
  117. {
  118. cout << endl << "PrintingOnScreen:" <<name<< endl;
  119. for (int i = 0; i < y1; i++)
  120. {
  121. cout << endl;
  122. for (int j = 0; j < x1; j++)
  123. cmPrint(am(i, j));
  124. }
  125. }
  126.  
  127. bool isEqual(Matrix b)
  128. {
  129. if(x!=b.x) return false;
  130. if(y!=b.y) return false;
  131. for (int i = 0; i < y; i++)
  132. for (int j = 0; j < x; j++)
  133. {
  134. if (am(i, j).r != b.am(i, j).r)return false;
  135. if (am(i, j).i != b.am(i, j).i)return false;
  136. }
  137. return true;
  138. }
  139.  
  140. Matrix matrixMp(Matrix t)
  141. {
  142. int y1 = y;
  143. int x1 = t.x;
  144. Matrix b(y1, x1);
  145. for (int i = 0; i < y1; i++)
  146. for (int j = 0; j < x1; j++)
  147. for (int k = 0; k < x; k++)
  148. {
  149. b.am(i,j).r += t.am(i,k).r * am(k,j).r - t.am(i,k).i * am(k,j).i;
  150. b.am(i,j).i += t.am(i, k).r * am(k,j).i + t.am(i,k).i * am(k,j).r;
  151. }
  152. return b;
  153. }
  154.  
  155. Matrix matrixSum(Matrix t)
  156. {
  157. //size
  158. int y1 = y;
  159. int x1 = x;
  160. Matrix b(y1, x1);
  161. for (int i = 0; i < y1; i++)
  162. for (int j = 0; j < x1; j++)
  163. {
  164. b.am(i, j).r += am(i, j).r + t.am(i, j).r;
  165. b.am(i, j).i += am(i, j).i + t.am(i, j).i;
  166. }
  167. return b;
  168. }
  169.  
  170. Matrix matrixMin(Matrix t)
  171. {
  172. //size
  173. int y1 = y;
  174. int x1 = x;
  175. Matrix b(y1, x1);
  176. for (int i = 0; i < y1; i++)
  177. for (int j = 0; j < x1; j++)
  178. {
  179. b.am(i, j).r += am(i, j).r - t.am(i, j).r;
  180. b.am(i, j).i += am(i, j).i - t.am(i, j).i;
  181. }
  182. return b;
  183. }
  184.  
  185. void intSum(cmplx t)
  186. {
  187. int min;
  188. if (x < y) min = x;
  189. else min = y;
  190. for (int i = 0; i < min; i++)
  191. {
  192. am(i, i).r = am(i, i).r + t.i;
  193. am(i, i).i = am(i, i).i + t.r;
  194. }
  195. }
  196.  
  197. void intMin(cmplx t)
  198. {
  199. t.r = t.r*(-1);
  200. t.i = t.i*(-1);
  201. intSum(t);
  202. }
  203.  
  204. void intMp(cmplx t)
  205. {
  206. for (int i = 0; i < y; i++)
  207. for (int j = 0; j < x; j++)
  208. {
  209. am(i, j).r = am(i, j).r * t.r;
  210. am(i, j).i = am(i, j).i * t.i;
  211. }
  212. }
  213.  
  214. void intDiv(cmplx t)
  215. {
  216. t.r = 1 / t.r;
  217. t.i = 1 / t.i;
  218. intMp(t);
  219. }
  220.  
  221. Matrix transp()
  222. {
  223. Matrix t;
  224. t.x = y;
  225. t.y = x;
  226. for (int i = 0; i < y; i++)
  227. for (int j = 0; j < x; j++)
  228. t.am(j, i) = am(i, j);
  229. return t;
  230. }
  231.  
  232. void selfTr()
  233. {
  234. Matrix t = transp();
  235. *this = t;
  236. }
  237.  
  238. void matrixPow(int p)
  239. {
  240. Matrix d = (*this);
  241. int i = 0;
  242. while (i < p)
  243. {
  244. Matrix t = matrixMp(d);
  245. i++;
  246. (*this) = t;
  247. }
  248. }
  249.  
  250. bool isSqua()
  251. {
  252. if (x == y) return true;
  253. else return false;
  254. }
  255.  
  256. bool isDig()
  257. {
  258. for (int i = 0; i < y; i++)
  259. for (int j = 0; j < x; j++)
  260. if ((am(i, j).r != 0) && (am(i, j).i != 0) && (i != j))
  261. return false;
  262. return true;
  263. }
  264.  
  265. bool isNull()
  266. {
  267. for (int i = 0; i < y; i++)
  268. for (int j = 0; j < x; j++)
  269. if ((am(i, j).r != 0) && (am(i, j).i != 0))
  270. return false;
  271. return true;
  272. }
  273.  
  274. bool isUn()
  275. {
  276. for (int i = 0; i < y; i++)
  277. for (int j = 0; j < x; j++)
  278. if (((am(i, j).r != 0) && (am(i, j).i != 0) && (i != j)) || ((i == j) && ((am(i, j).r != 1) && (am(i, j).i != 0))))
  279. return false;
  280. return true;
  281. }
  282.  
  283. bool isSim()
  284. {
  285. for (int i = 0; i < y; i++)
  286. for (int j = 0; j < x; j++)
  287. if ((am(i, j).r != am(j, i).r) && (am(i, j).i != am(j, i).i) && (i != j))
  288. return false;
  289. return true;
  290. }
  291.  
  292. bool isUpTr()
  293. {
  294. for (int i = 0; i < y; i++)
  295. for (int j = 0; j < x; j++)
  296. if ((j<i) && (am(i, j).r != 0) && (am(i, j).i != 0))
  297. return false;
  298. return true;
  299. }
  300.  
  301. bool isDwTr()
  302. {
  303. for (int i = 0; i < y; i++)
  304. for (int j = 0; j < x; j++)
  305. if ((j>i) && (am(i, j).r != 0) && (am(i, j).i != 0))
  306. return false;
  307. return true;
  308. }
  309.  
  310. cmplx& am(int i, int j)
  311. {
  312. if (i >= y)
  313. {
  314. if (j >= x)
  315. {
  316. cout << endl << "Out of range...";
  317. return a[y][x];
  318. }
  319. if (j < 0)
  320. {
  321. cout << endl << "Out of range...";
  322. return a[y][0];
  323. }
  324. }
  325. if (i < 0)
  326. {
  327. if (j >= x)
  328. {
  329. cout << endl << "Out of range...";
  330. return a[0][x];
  331. }
  332. if (j < 0)
  333. {
  334. cout << endl << "Out of range...";
  335. return a[0][0];
  336. }
  337. }
  338. return a[i][j];
  339. }
  340. void operator=(Matrix &b)
  341. {
  342. x=b.x;
  343. y=b.y;
  344. for (int i = 0; i < x; i++)
  345. for (int j = 0; j < y; j++)
  346. am(i,j)=b.am(i,j);
  347. }
  348. void operator+=(Matrix &b)
  349. {
  350. *this=matrixSum(b);
  351. }
  352. void operator-=(Matrix &b)
  353. {
  354. *this=matrixMin(b);
  355. }
  356. void operator*=(Matrix &b)
  357. {
  358. *this=matrixMp(b);
  359. }
  360. void operator+=(cmplx b)
  361. {
  362. intSum(b);
  363. }
  364. void operator-=(cmplx b)
  365. {
  366. intMin(b);
  367. }
  368. void operator*=(cmplx b)
  369. {
  370. intMp(b);
  371. }
  372. void operator/=(cmplx b)
  373. {
  374. intDiv(b);
  375. }
  376. };
  377.  
  378. void cmPrint(cmplx a)
  379. {
  380. cout << a.r;
  381. cout << " ";
  382. cout << a.i;
  383. cout << "i ";
  384. }
  385.  
  386. int main()
  387. {
  388. //Matrix::n = 0;
  389. cout << "Push the temple" << endl;
  390. _getch();
  391. cout << "Hello! Please, enter dimensions ";
  392. int y, x;
  393. cin >> y;
  394. cin >> x;
  395. Matrix a(y, x);
  396. a.matrixIn();
  397. cout << endl;
  398. a.print();
  399. return 0;
  400. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement