MainWindow.xaml.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace Matrices
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для MainWindow.xaml
  19. /// </summary>
  20. public partial class MainWindow : Window
  21. {
  22. gr692_zrrEntities db = new gr692_zrrEntities();
  23. public MainWindow()
  24. {
  25. InitializeComponent();
  26. this.createGrid10x10();
  27. }
  28. int[,] a = new int[3, 4];
  29. int[,] b = new int[3, 4];
  30. public void Matrix(ref int[,] a, ref int[,] b)
  31. {
  32. try
  33. {
  34. int nn = Convert.ToInt32(n.Text);
  35. int mm = Convert.ToInt32(m.Text);
  36. if (nn == 2 && mm == 3)
  37. {
  38. a[0, 0] = Convert.ToInt32(i00.Text);
  39. a[0, 1] = Convert.ToInt32(i01.Text);
  40. a[0, 2] = Convert.ToInt32(i02.Text);
  41. a[1, 0] = Convert.ToInt32(i10.Text);
  42. a[1, 1] = Convert.ToInt32(i11.Text);
  43. a[1, 2] = Convert.ToInt32(i12.Text);
  44. b[0, 0] = Convert.ToInt32(b00.Text);
  45. b[0, 1] = Convert.ToInt32(b01.Text);
  46. b[0, 2] = Convert.ToInt32(b02.Text);
  47. b[1, 0] = Convert.ToInt32(b10.Text);
  48. b[1, 1] = Convert.ToInt32(b11.Text);
  49. b[1, 2] = Convert.ToInt32(b12.Text);
  50. }
  51. else if (nn == 1 && mm == 4)
  52. {
  53. a[0, 0] = Convert.ToInt32(i00.Text);
  54. a[0, 1] = Convert.ToInt32(i01.Text);
  55. a[0, 2] = Convert.ToInt32(i02.Text);
  56. a[0, 3] = Convert.ToInt32(i03.Text);
  57. b[0, 0] = Convert.ToInt32(b00.Text);
  58. b[0, 1] = Convert.ToInt32(b01.Text);
  59. b[0, 2] = Convert.ToInt32(b02.Text);
  60. b[0, 3] = Convert.ToInt32(b03.Text);
  61. }
  62. else
  63. for (int i = 0; i < 3; i++)
  64. {
  65. for (int j = 0; j < 4; j++)
  66. {
  67. a[0, 0] = Convert.ToInt32(i00.Text);
  68. a[0, 1] = Convert.ToInt32(i01.Text);
  69. a[0, 2] = Convert.ToInt32(i02.Text);
  70. a[0, 3] = Convert.ToInt32(i03.Text);
  71. a[1, 0] = Convert.ToInt32(i10.Text);
  72. a[1, 1] = Convert.ToInt32(i11.Text);
  73. a[1, 2] = Convert.ToInt32(i12.Text);
  74. a[1, 3] = Convert.ToInt32(i13.Text);
  75. a[2, 0] = Convert.ToInt32(i20.Text);
  76. a[2, 1] = Convert.ToInt32(i21.Text);
  77. a[2, 2] = Convert.ToInt32(i22.Text);
  78. a[2, 3] = Convert.ToInt32(i23.Text);
  79. b[0, 0] = Convert.ToInt32(b00.Text);
  80. b[0, 1] = Convert.ToInt32(b01.Text);
  81. b[0, 2] = Convert.ToInt32(b02.Text);
  82. b[0, 3] = Convert.ToInt32(b03.Text);
  83. b[1, 0] = Convert.ToInt32(b10.Text);
  84. b[1, 1] = Convert.ToInt32(b11.Text);
  85. b[1, 2] = Convert.ToInt32(b12.Text);
  86. b[1, 3] = Convert.ToInt32(b13.Text);
  87. b[2, 0] = Convert.ToInt32(b20.Text);
  88. b[2, 1] = Convert.ToInt32(b21.Text);
  89. b[2, 2] = Convert.ToInt32(b22.Text);
  90. b[2, 3] = Convert.ToInt32(b23.Text);
  91. }
  92. }
  93. }
  94. catch
  95. {
  96. MessageBox.Show("Введите матрицы 2х3 или 3х4");
  97. }
  98. }
  99. private void Zapis()
  100. {
  101. int nn = Convert.ToInt32(n.Text);
  102. int mm = Convert.ToInt32(m.Text);
  103. int per;
  104. Matrix(ref a, ref b);
  105. for (int i = 0; i < nn; i++)
  106. {
  107. for (int j = 0; j < mm; j++)
  108. {
  109. per = a[i, j];
  110. IshodMassiv mat = new IshodMassiv()
  111. {
  112. Ishod = per
  113. };
  114. db.IshodMassiv.Add(mat);
  115. db.SaveChanges();
  116. }
  117. }
  118. }
  119. public void Itog(int[,] rezult)
  120. {
  121. int nn = Convert.ToInt32(n.Text);
  122. int mm = Convert.ToInt32(m.Text);
  123. if (nn == 2 && mm == 3)
  124. {
  125. r00.Text = rezult[0, 0].ToString();
  126. r01.Text = rezult[0, 1].ToString();
  127. r02.Text = rezult[0, 2].ToString();
  128. r10.Text = rezult[1, 0].ToString();
  129. r11.Text = rezult[1, 1].ToString();
  130. r12.Text = rezult[1, 2].ToString();
  131. }
  132. else if (nn == 1 && mm == 4)
  133. {
  134. r00.Text = rezult[0, 0].ToString();
  135. r01.Text = rezult[0, 1].ToString();
  136. r02.Text = rezult[0, 2].ToString();
  137. r10.Text = rezult[1, 0].ToString();
  138. r11.Text = rezult[1, 1].ToString();
  139. r12.Text = rezult[1, 2].ToString();
  140. }
  141. else
  142. {
  143. r00.Text = rezult[0, 0].ToString();
  144. r01.Text = rezult[0, 1].ToString();
  145. r02.Text = rezult[0, 2].ToString();
  146. r03.Text = rezult[0, 3].ToString();
  147. r10.Text = rezult[1, 0].ToString();
  148. r11.Text = rezult[1, 1].ToString();
  149. r12.Text = rezult[1, 2].ToString();
  150. r13.Text = rezult[1, 3].ToString();
  151. r20.Text = rezult[2, 0].ToString();
  152. r21.Text = rezult[2, 1].ToString();
  153. r22.Text = rezult[2, 2].ToString();
  154. r23.Text = rezult[2, 3].ToString();
  155. }
  156. }
  157. public void Transpor(int[,] transpor)
  158. {
  159. int nn = Convert.ToInt32(n.Text);
  160. int mm = Convert.ToInt32(m.Text);
  161. if (nn == 2 && mm == 3)
  162. {
  163. r00.Text = transpor[0, 0].ToString();
  164. r01.Text = transpor[0, 1].ToString();
  165. r10.Text = transpor[1, 0].ToString();
  166. r11.Text = transpor[1, 1].ToString();
  167. r20.Text = transpor[2, 0].ToString();
  168. r21.Text = transpor[2, 1].ToString();
  169. }
  170. else
  171. {
  172. r00.Text = transpor[0, 0].ToString();
  173. r01.Text = transpor[0, 1].ToString();
  174. r02.Text = transpor[0, 2].ToString();
  175. r10.Text = transpor[1, 0].ToString();
  176. r11.Text = transpor[1, 1].ToString();
  177. r12.Text = transpor[1, 2].ToString();
  178. r20.Text = transpor[2, 0].ToString();
  179. r21.Text = transpor[2, 1].ToString();
  180. r22.Text = transpor[2, 2].ToString();
  181. r30.Text = transpor[3, 0].ToString();
  182. r31.Text = transpor[3, 1].ToString();
  183. r32.Text = transpor[3, 2].ToString();
  184. }
  185. }
  186. private void Button_Click1(object sender, RoutedEventArgs e)
  187. {
  188. int nn = Convert.ToInt32(n.Text);
  189. int mm = Convert.ToInt32(m.Text);
  190. if (string.IsNullOrEmpty(i00.Text)
  191. && string.IsNullOrEmpty(b00.Text))
  192. {
  193. Print();
  194. }
  195. else
  196. {
  197. int per;
  198. Matrix(ref a, ref b);
  199. int[,] sum = new int[nn, mm];
  200. for (int i = 0; i < nn; i++)
  201. {
  202. for (int j = 0; j < mm; j++)
  203. {
  204. sum[i, j] = a[i, j] + b[i, j];
  205. per = sum[i, j];
  206. Mat mat = new Mat()
  207. {
  208. matrix = per
  209. };
  210. db.Mat.Add(mat);
  211. db.SaveChanges();
  212. }
  213. }
  214. Zapis();
  215. Itog(sum);
  216. Vipolneno();
  217. }
  218. }
  219. private void Button_Click2(object sender, RoutedEventArgs e)
  220. {
  221. int nn = Convert.ToInt32(n.Text);
  222. int mm = Convert.ToInt32(m.Text);
  223. if (string.IsNullOrEmpty(i00.Text)
  224. && string.IsNullOrEmpty(b00.Text))
  225. {
  226. Print();
  227. }
  228. else
  229. {
  230. int per;
  231. Matrix(ref a, ref b);
  232. int[,] sum = new int[nn, mm];
  233. for (int i = 0; i < nn; i++)
  234. {
  235. for (int j = 0; j < mm; j++)
  236. {
  237. sum[i, j] = a[i, j] - b[i, j];
  238. per = sum[i, j];
  239. //Mat mat = new Mat()
  240. //{
  241. // matrix = per
  242. //};
  243. //db.Mat.Add(mat);
  244. //db.SaveChanges();
  245. }
  246. }
  247. //Zapis();
  248. Itog(sum);
  249. Vipolneno();
  250. }
  251. }
  252. private void Button_Click3(object sender, RoutedEventArgs e)
  253. {
  254. int nn = Convert.ToInt32(n.Text);
  255. int mm = Convert.ToInt32(m.Text);
  256. if (string.IsNullOrEmpty(i00.Text)
  257. && string.IsNullOrEmpty(Scalar.Text))
  258. {
  259. Print();
  260. }
  261. else
  262. {
  263. try
  264. {
  265. int Chislo = Convert.ToInt32(Scalar.Text);
  266. int per;
  267. Matrix(ref a, ref b);
  268. int[,] sum = new int[nn, mm];
  269. for (int i = 0; i < nn; i++)
  270. {
  271. for (int j = 0; j < mm; j++)
  272. {
  273. sum[i, j] = a[i, j] * Chislo;
  274. per = sum[i, j];
  275. //Mat mat = new Mat()
  276. //{
  277. // matrix = per
  278. //};
  279. //db.Mat.Add(mat);
  280. //db.SaveChanges();
  281. }
  282. }
  283. //Zapis();
  284. Itog(sum);
  285. Vipolneno();
  286. }
  287. catch
  288. {
  289. MessageBox.Show("Введите скалярное значение!!!");
  290. }
  291. }
  292. }
  293. private void Button_Click4(object sender, RoutedEventArgs e)
  294. {
  295. int nn = Convert.ToInt32(n.Text);
  296. int mm = Convert.ToInt32(m.Text);
  297. if (string.IsNullOrEmpty(i00.Text)
  298. && string.IsNullOrEmpty(b00.Text))
  299. {
  300. Print();
  301. }
  302. else
  303. {
  304. int per;
  305. Matrix(ref a, ref b);
  306. int[,] sum = new int[nn+1, mm];
  307. for (int i = 0; i < mm; i++)
  308. {
  309. for (int j = 0; j < nn; j++)
  310. {
  311. sum[i, j] = a[j, i];
  312. per = sum[i, j];
  313. //Mat mat = new Mat()
  314. //{
  315. // matrix = per
  316. //};
  317. //db.Mat.Add(mat);
  318. //db.SaveChanges();
  319. }
  320. }
  321. //Zapis();
  322. Transpor(sum);
  323. Vipolneno();
  324. }
  325. }
  326. private void Button_Click5(object sender, RoutedEventArgs e)
  327. {
  328. int nn = Convert.ToInt32(n.Text);
  329. int mm = Convert.ToInt32(m.Text);
  330. if (string.IsNullOrEmpty(i00.Text)
  331. && string.IsNullOrEmpty(b00.Text))
  332. {
  333. Print();
  334. }
  335. else
  336. {
  337. int per;
  338. Matrix(ref a, ref b);
  339. int[,] sum = new int[nn+1, mm+1];
  340. for (int i = 0; i < mm; i++)
  341. {
  342. for (int j = 0; j < nn; j++)
  343. {
  344. sum[i, j] = a[j, i] * a[j, i];
  345. per = sum[i, j];
  346. //Mat mat = new Mat()
  347. //{
  348. // matrix = per
  349. //};
  350. //db.Mat.Add(mat);
  351. //db.SaveChanges();
  352. }
  353. }
  354. //Zapis();
  355. Transpor(sum);
  356. Vipolneno();
  357. }
  358. }
  359. private void Button_Click6(object sender, RoutedEventArgs e)
  360. {
  361. int nn = Convert.ToInt32(n.Text);
  362. int mm = Convert.ToInt32(m.Text);
  363. if (string.IsNullOrEmpty(i00.Text)
  364. && string.IsNullOrEmpty(b00.Text))
  365. {
  366. Print();
  367. }
  368. else
  369. {
  370. int per, buf;
  371. Matrix(ref a, ref b);
  372. int[,] sum = new int[nn, mm];
  373. // перестановка строк
  374. for (int s = 0; s < nn; s++)
  375. for (int k = 0; k < mm; k++)
  376. {
  377. buf = a[s, k];
  378. a[s, k] = a[s + 1, k];
  379. a[s + 1, k] = buf;
  380. //per = sum[nn, mm];
  381. }
  382. //Mat mat = new Mat()
  383. //{
  384. // matrix = per
  385. //};
  386. //db.Mat.Add(mat);
  387. //db.SaveChanges();
  388. // перестановка столбцов
  389. //for (int s = 0; s < mm+1; s++)
  390. // for (int k = 0; k < nn+1; k++)
  391. // {
  392. // buf = a[s, k];
  393. // a[s, k] = a[s, k + 1];
  394. // a[s, k + 1] = buf;
  395. // }
  396. //Zapis();
  397. Itog(a);
  398. Vipolneno();
  399. }
  400. }
  401. private void _placeSingleColorColumn(Grid grid, Color color, int height, int colNum, int maxHeight)
  402. {
  403. Brush brush = new SolidColorBrush(color);
  404. Rectangle rect = new Rectangle();
  405. rect.Fill = brush;
  406. Grid.SetColumn(rect, colNum);
  407. Grid.SetRow(rect, maxHeight - height);
  408. Grid.SetRowSpan(rect, height);
  409. grid.Children.Add(rect);
  410. }
  411. //private void _createLabels(Grid grid, string[] labels)
  412. //{
  413. // RowDefinition rowDefnLabels = new RowDefinition();
  414. // grid.RowDefinitions.Add(rowDefnLabels);
  415. // for (int i = 0; i < labels.Length; i++)
  416. // {
  417. // TextBlock block = new TextBlock();
  418. // block.Text = labels[i];
  419. // block.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
  420. // Grid.SetColumn(block, i);
  421. // Grid.SetRow(block, grid.RowDefinitions.Count);
  422. // grid.Children.Add(block);
  423. // }
  424. //}
  425. public void createGrid10x10()
  426. {
  427. //string[] aLabels = "Dogs,Cats,Birds,Snakes,Rabbits,Hamsters,Horses,Rats,Bats,Unicorns".Split(',');
  428. //_createLabels(this.myGrid, aLabels);
  429. }
  430. private void Button_Click7(object sender, RoutedEventArgs e)
  431. {
  432. //int nn = Convert.ToInt32(str.Text);
  433. //int mm = Convert.ToInt32(stl.Text);
  434. Matrix(ref a, ref b);
  435. Random random = new Random();
  436. for (int i = 0; i < 10; i++)
  437. {
  438. ColumnDefinition colDef = new ColumnDefinition();
  439. myGrid.ColumnDefinitions.Add(colDef);
  440. RowDefinition rowDef = new RowDefinition();
  441. myGrid.RowDefinitions.Add(rowDef);
  442. Color color = i % 2 == 0 ? Colors.Red : Colors.Blue;
  443. _placeSingleColorColumn(this.myGrid, color, random.Next(1, 11), i, 10);
  444. }
  445. //Zapis();
  446. Vipolneno();
  447. }
  448. private void Button_ClickClear(object sender, RoutedEventArgs e)
  449. {
  450. i00.Clear();
  451. i01.Clear();
  452. i02.Clear();
  453. i03.Clear();
  454. i10.Clear();
  455. i11.Clear();
  456. i12.Clear();
  457. i13.Clear();
  458. i20.Clear();
  459. i21.Clear();
  460. i22.Clear();
  461. i23.Clear();
  462. b00.Clear();
  463. b01.Clear();
  464. b02.Clear();
  465. b03.Clear();
  466. b10.Clear();
  467. b11.Clear();
  468. b12.Clear();
  469. b13.Clear();
  470. b20.Clear();
  471. b21.Clear();
  472. b22.Clear();
  473. b23.Clear();
  474. stl.Clear();
  475. str.Clear();
  476. n.Clear();
  477. m.Clear();
  478. }
  479. private void Print()
  480. {
  481. if (string.IsNullOrEmpty(i00.Text)
  482. || string.IsNullOrEmpty(b00.Text))
  483. {
  484. MessageBox.Show("Введите матрицы");
  485. }
  486. else
  487. {
  488. MessageBox.Show("Выполнено");
  489. }
  490. }
  491. private void Vipolneno()
  492. {
  493. MessageBox.Show("Выполнено");
  494. }
  495. //private void Razmer(int nn, int mm)
  496. //{
  497. // try
  498. // {
  499. // nn = Convert.ToInt32(n.Text);
  500. // mm = Convert.ToInt32(m.Text);
  501. // }
  502. // catch
  503. // {
  504. // MessageBox.Show("Введите размеры матриц");
  505. // }
  506. //}
  507. public int[,] TestSumm(int [,] rezult)
  508. {
  509. var array = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
  510. var array2 = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
  511. for (int i = 0; i < 2; i++)
  512. {
  513. for (int j = 0; j < 3; j++)
  514. {
  515. rezult[i, j] = array[i, j] + array2[i, j];
  516. }
  517. }
  518. return rezult;
  519. }
  520. public int[,] TestMinus(int[,] rezult)
  521. {
  522. var array = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
  523. var array2 = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
  524. for (int i = 0; i < 2; i++)
  525. {
  526. for (int j = 0; j < 3; j++)
  527. {
  528. rezult[i, j] = array[i, j] - array2[i, j];
  529. }
  530. }
  531. return rezult;
  532. }
  533. public int[,] TestScalar(int[,] rezult)
  534. {
  535. var array = new int[2, 3] { { 1, 4, 3 }, { 15, 50, 60 } };
  536. for (int i = 0; i < 2; i++)
  537. {
  538. for (int j = 0; j < 3; j++)
  539. {
  540. rezult[i, j] = array[i, j] * 5;
  541. }
  542. }
  543. return rezult;
  544. }
  545. public int[,] TestTranspor(int[,] rezult)
  546. {
  547. var array = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
  548. for (int i = 0; i < 3; i++)
  549. {
  550. for (int j = 0; j < 2; j++)
  551. {
  552. rezult[i, j] = array[j, i];
  553. }
  554. }
  555. return rezult;
  556. }
  557. public int[,] TestTransporUMN(int[,] rezult)
  558. {
  559. var array = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
  560. for (int i = 0; i < 3; i++)
  561. {
  562. for (int j = 0; j < 2; j++)
  563. {
  564. rezult[i, j] = array[j, i] * array[j, i];
  565. }
  566. }
  567. return rezult;
  568. }
  569. public int[,] TestPerestanovka(int[,] rezult)
  570. {
  571. var array = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
  572. int buf;
  573. for (int s = 0; s < 2; s++)
  574. for (int k = 0; k < 3; k++)
  575. {
  576. buf = array[s, k];
  577. array[s, k] = array[s + 1, k];
  578. array[s + 1, k] = buf;
  579. rezult[s, k] = buf;
  580. }
  581. return rezult;
  582. }
  583. //public bool TestIns(int ID, string Login, string Password)
  584. //{
  585. // if (ID == null || Login == "" || Password == "")
  586. // {
  587. // MessageBox.Show("Вы ввели не все данные");
  588. // return false;
  589. // }
  590. // else if (db.Reg.Select(item => item.ID).Contains(ID))
  591. // {
  592. // MessageBox.Show("Данный логин уже занят. Введите другой");
  593. // return false;
  594. // }
  595. // Reg newReg = new Reg()
  596. // {
  597. // ID = ID,
  598. // Login = Login,
  599. // Password = Password,
  600. // };
  601. // PersonBD BD = new PersonBD()
  602. // {
  603. // FirstName_person = FirstName,
  604. // LastName_person = LastName,
  605. // MiddleName_person = MiddleName
  606. // };
  607. // if (newReg == null)
  608. // {
  609. // MessageBox.Show("Аккаунт пользователя не создан");
  610. // return false;
  611. // }
  612. // db.Reg.Add(newReg);
  613. // db.PersonBD.Add(BD);
  614. // try
  615. // {
  616. // db.SaveChanges();
  617. // }
  618. // catch (Exception e)
  619. // {
  620. // MessageBox.Show(e.Message);
  621. // return false;
  622. // }
  623. // MessageBox.Show("Вы успешно зарегистрировались");
  624. // return true;
  625. //}
  626. }
  627. }