Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <math.h>
  4. #include <string>
  5. using namespace std;
  6.  
  7.  
  8. class Matrix
  9. {
  10. private:
  11.  
  12. int n;
  13. double **arr;
  14.  
  15. public:
  16. Matrix(int numb)
  17. {
  18. n = numb;
  19. arr = new double *[n];
  20. for (int i = 0; i < n; i++)
  21. {
  22. arr[i] = new double[n];
  23. for (int j = 0; j < n; j++)
  24. {
  25. cout << "Введите " << "элемент " << "столбца " << i + 1 << " " << "строки " << j + 1 << ": ";
  26. cin >> arr[i][j];
  27. }
  28. }
  29.  
  30. cout << "Ваша матрица: " << endl;
  31. }
  32.  
  33. Matrix(int numb, int one)
  34. {
  35. n = numb;
  36. arr = new double *[n];
  37. for (int i = 0; i < n; i++)
  38. {
  39. arr[i] = new double[n];
  40. for (int j = 0; j < n; j++)
  41. {
  42. arr[i][j] = one;
  43. }
  44. }
  45. }
  46.  
  47. double **get_arr()
  48. {
  49. return arr;
  50. }
  51.  
  52. void set_arr(double **arr_buf)
  53. {
  54. arr = arr_buf;
  55. }
  56.  
  57. Matrix pl(Matrix ob)
  58. {
  59. Matrix rez(n, 0);
  60.  
  61. for (int i = 0; i < n; i++)
  62. {
  63. for (int j = 0; j < n; j++)
  64. {
  65. rez.arr[i][j] = arr[i][j] + ob.arr[i][j];
  66. }
  67. }
  68.  
  69. return rez;
  70. }
  71.  
  72. Matrix mns(Matrix ob)
  73. {
  74. Matrix rez(n, 0);
  75.  
  76. for (int i = 0; i < n; i++)
  77. {
  78. for (int j = 0; j < n; j++)
  79. {
  80. rez.arr[i][j] = arr[i][j] - ob.arr[i][j];
  81. }
  82. }
  83.  
  84. return rez;
  85. }
  86.  
  87. Matrix umn(Matrix ob)
  88. {
  89. Matrix rez(n, 0);
  90.  
  91. for (int i = 0; i < n; i++)
  92. {
  93. for (int j = 0; j < n; j++)
  94. {
  95. int skal = 0;
  96. for (int k = 0; k < n; k++)
  97. rez.arr[i][j] += arr[i][j] * ob.arr[i][j];
  98. }
  99. }
  100.  
  101. return rez;
  102. }
  103.  
  104. void matr_na_chisl(Matrix ob, int k)
  105. {
  106. for (int i = 0; i < n; i++)
  107. {
  108. for (int j = 0; j < n; j++)
  109. {
  110. arr[i][j] = ob.arr[i][j] * k;
  111. }
  112. }
  113. };
  114.  
  115. void matr_div_chisl(Matrix ob, int k)
  116. {
  117. for (int i = 0; i < n; i++)
  118. {
  119. for (int j = 0; j < n; j++)
  120. {
  121. arr[i][j] = ob.arr[i][j] / k;
  122. }
  123. }
  124. };
  125.  
  126. void print(int n)
  127. {
  128. for (int i = 0; i < n; i++)
  129. {
  130. for (int j = 0; j < n; j++)
  131. {
  132. cout << "|" << arr[i][j] << "|" << " ";
  133. }
  134. cout << endl;
  135. }
  136. }
  137. };
  138.  
  139. class formula : public Matrix
  140. {
  141. public:
  142. formula(int n, int k = 0) :Matrix(n, k)
  143. {
  144.  
  145. }
  146.  
  147. Matrix sin(Matrix object, int n)
  148. {
  149. double **arr_buf = new double*[n];
  150. for (int i = 0; i < n; i++)
  151. {
  152. arr_buf[i] = new double[n];
  153. }
  154.  
  155. Matrix result(n, 0);
  156. double fact;
  157. int k;
  158. double sinx = 0;
  159. double st;
  160. double zn;
  161.  
  162. for (int i = 0; i < n; i++)
  163. {
  164. for (int j = 0; j < n; j++)
  165. {
  166. k = 0;
  167. sinx = 0;
  168. st = object.get_arr()[i][j];
  169. fact = 1;
  170. zn = 1;
  171. while (k < 10)
  172. {
  173. sinx = sinx + st * zn / fact;
  174. k = k + 2;
  175. st = st * object.get_arr()[i][j] * object.get_arr()[i][j];
  176. fact = fact * (k - 1) * k;
  177. zn = zn * -1;
  178. }
  179. arr_buf[i][j] = sinx;
  180. }
  181. }
  182. result.set_arr(arr_buf);
  183. return result;
  184. }
  185. };
  186.  
  187.  
  188. void menu_for2()
  189. {
  190. cout << "1)Сложение матриц" << endl;
  191. cout << "2)Разность матриц" << endl;
  192. cout << "3)Произведение матриц" << endl;
  193. cout << "4)Умножение матрицы на число" << endl;
  194. cout << "5)Деление матрицы на число" << endl;
  195. cout << "6)Формула " << endl;
  196. }
  197.  
  198. int main()
  199. {
  200. setlocale(LC_ALL, "rus");
  201.  
  202. cout << "Введите размерность матрицы: ";
  203. int n;
  204. cin >> n;
  205. Matrix ob1(n);
  206. Matrix ob3(n, 0);
  207.  
  208. cout << endl;
  209.  
  210. menu_for2();
  211.  
  212. int choise_2;
  213. cin >> choise_2;
  214.  
  215. switch (choise_2)
  216. {
  217. case 1: {
  218. cout << "Введите вторую матрицу" << endl;
  219. Matrix ob2(n);
  220.  
  221. ob3 = ob1.pl(ob2);
  222. ob3.print(n);
  223.  
  224. break;
  225. }
  226. case 2: {
  227. cout << "Введите вторую матрицу" << endl;
  228. Matrix ob2(n);
  229.  
  230. ob3 = ob1.mns(ob2);
  231. ob3.print(n);
  232. break;
  233. }
  234. case 3: {
  235. cout << "Введите вторую матрицу" << endl;
  236. Matrix ob2(n);
  237.  
  238. ob3 = ob1.umn(ob2);
  239. ob3.print(n);
  240. break;
  241. }
  242. case 4: {
  243. float k;
  244.  
  245. cout << "Введите число, на которое собираетесь умножить матрицу: ";
  246. cin >> k;
  247.  
  248. ob1.matr_na_chisl(ob1, k);
  249. ob1.print(n);
  250. break;
  251. }
  252. case 5: {
  253. float k;
  254.  
  255. cout << "Введите число, на которое собираетесь делить матрицу: ";
  256. cin >> k;
  257.  
  258. if (k == 0)
  259. {
  260. do
  261. {
  262. cout << "Введите корректное значение!";
  263. cin >> k;
  264. } while (k == 0);
  265. }
  266.  
  267. ob1.matr_div_chisl(ob1,k);
  268. ob1.print(n);
  269.  
  270. break;
  271. }
  272.  
  273. case 6: {
  274. formula res(n);
  275.  
  276. ob3 = res.sin(ob1, n);
  277. ob3.print(n);
  278.  
  279. break;
  280.  
  281. }
  282. default:
  283. break;
  284. };
  285.  
  286. system ("pause");
  287. return 0;
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement