Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static void PrintMenu()
  11. {
  12. Console.WriteLine("Выберите действие:");
  13. Console.WriteLine();
  14. Console.WriteLine("1.Работа с одномерным массивом");
  15. Console.WriteLine("2.Работа с двумерным массивом");
  16. Console.WriteLine("3.Работа с рваным массивом");
  17. Console.WriteLine("0.Выход");
  18. }
  19. static void PrintMenuMass()
  20. {
  21. Console.WriteLine();
  22. Console.WriteLine("1.Создать массив");
  23. Console.WriteLine("2.Напечатать массив");
  24. Console.WriteLine("3.Добавить 0 после каждого четного элемента");
  25. Console.WriteLine("0.Назад");
  26. }
  27. static void PrintMenuMass2()
  28. {
  29. Console.WriteLine();
  30. Console.WriteLine("1.Создать массив");
  31. Console.WriteLine("2.Напечатать массив");
  32. Console.WriteLine("3.Удалить строки с нулевыми элементами");
  33. Console.WriteLine("0.Назад");
  34. }
  35. static void PrintMenuMass3()
  36. {
  37. Console.WriteLine();
  38. Console.WriteLine("1.Создать массив");
  39. Console.WriteLine("2.Напечатать массив");
  40. Console.WriteLine("3.Добавить К строк в конец массива");
  41. Console.WriteLine("0.Назад");
  42. }
  43. static void PrintMenuForm()
  44. {
  45. Console.WriteLine();
  46. Console.WriteLine("1. Вручную");
  47. Console.WriteLine("2. С помощью ДСЧ");
  48. Console.WriteLine("0. Назад");
  49. }
  50. static int NumberEntry() // функция ввода целого числа
  51. {
  52. int x;
  53. string str;
  54. bool ok;
  55. do
  56. {
  57. str = Console.ReadLine();
  58. ok = Int32.TryParse(str, out x);
  59. if (!ok) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое число");
  60. } while (!ok);
  61.  
  62. return x;
  63. }
  64.  
  65. //Работа с одномерным массивом
  66. static bool MassivFormConsole(out int size, out int[] a)
  67. {
  68. Console.WriteLine("Введите количество элементов массива");
  69. do
  70. {
  71. size = NumberEntry();
  72. if (size <= 0)
  73. Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число");
  74. } while (size <= 0);
  75. a = new int[size];
  76. for (int i = 0; i < size; i++)
  77. {
  78. Console.WriteLine("Введите {0} элемент массива", i + 1);
  79. a[i] = NumberEntry();
  80. }
  81. Console.WriteLine();
  82. Console.WriteLine("Одномерный массив создан.");
  83. Console.WriteLine();
  84. return true;
  85. }
  86. static bool MassivFormRandom(out int size, out int[] a)
  87. {
  88. Console.WriteLine("Введите количество элементов массива");
  89. size = NumberEntry();
  90. a = new int[size];
  91. Random rand = new Random();
  92. for (int i = 0; i < size; i++)
  93. {
  94. a[i] = rand.Next(-100, 100);
  95. }
  96. Console.WriteLine();
  97. Console.WriteLine("Одномерный массив создан.");
  98. Console.WriteLine();
  99. return true;
  100. }
  101. static void MassivPrint(int size, int[] a)
  102. {
  103. Console.WriteLine();
  104. for (int i = 0; i < size; i++)
  105. Console.Write(a[i] + " ");
  106. Console.WriteLine();
  107. }
  108. static void MassivAddElem(ref int size, ref int[] a)
  109. {
  110. int cnt = 0;
  111. for (int i = 0; i < size; i++)
  112. {
  113. if (a[i] % 2 == 0) cnt = cnt + 1;
  114. }
  115. int [] a2 = new int[cnt + size];
  116. int j = 0;
  117. for (int i = 0; i < size; i++)
  118. {
  119. if (a[i] % 2 == 0)
  120. {
  121. a2[j] = a[i];
  122. j++;
  123. a2[j] = 0;
  124. j++;
  125. }
  126. else
  127. {
  128. a2[j] = a[i];
  129. j++;
  130. }
  131. }
  132. a = a2;
  133. size += cnt;
  134. Console.WriteLine();
  135. if (cnt == 0) Console.WriteLine("Четных элементов в массиве нет");
  136. else Console.WriteLine("КРЯ КРЯ");
  137. }
  138. static void Massiv() // организация меню для одномерного массива
  139. {
  140. int[] a = null;
  141. int size = 0;
  142. bool formedMassiv = false;
  143. int otv;
  144. int otvForm;
  145. do
  146. {
  147. PrintMenuMass();
  148. otv = NumberEntry();
  149. switch (otv)
  150. {
  151. case 1:
  152. {
  153. PrintMenuForm();
  154. do
  155. {
  156. otvForm = NumberEntry();
  157. switch (otvForm)
  158. {
  159. case 1:
  160. {
  161. formedMassiv = MassivFormConsole(out size, out a);
  162. break;
  163. }
  164. case 2:
  165. {
  166. formedMassiv = MassivFormRandom(out size, out a);
  167. break;
  168. }
  169. case 4: break;
  170. default:
  171. {
  172. Console.WriteLine("Такого пункта нет в меню");
  173. break;
  174. }
  175. }
  176. } while ((otvForm != 0) && (otvForm != 1) && (otvForm != 2));
  177. break;
  178. }
  179. case 2:
  180. {
  181. if (formedMassiv) MassivPrint(size, a);
  182. else Console.WriteLine("Массив не создан");
  183. break;
  184. }
  185. case 3:
  186. {
  187. if (formedMassiv) MassivAddElem(ref size, ref a);
  188. else Console.WriteLine("Массив не создан");
  189. break;
  190. }
  191. case 0: break;
  192. default:
  193. {
  194. Console.WriteLine("Такого пункта нет в меню");
  195. break;
  196. }
  197. }
  198.  
  199. } while (otv != 0);
  200. }
  201.  
  202. // Работа с двумерным массивом (Matr)
  203. static bool MassivFormConsole(out int strings, out int columns, out int[,] matr)
  204. {
  205. Console.WriteLine("Введите количество строк массива");
  206. do
  207. {
  208. strings = NumberEntry();
  209. if (strings <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
  210. } while (strings <= 0);
  211. Console.WriteLine("Введите количество столбцов массива");
  212. do
  213. {
  214. columns = NumberEntry();
  215. if (columns <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
  216. } while (columns <= 0);
  217. matr = new int[strings, columns];
  218. for (int i = 0; i < strings; i++)
  219. {
  220. for (int j = 0; j < columns; j++)
  221. {
  222. Console.WriteLine("Введите элемент массива c координатами ({0};{1})", i + 1, j + 1);
  223. matr[i, j] = NumberEntry();
  224. }
  225. Console.WriteLine();
  226. }
  227. Console.WriteLine();
  228. Console.WriteLine("Двумерный массив создан.");
  229. Console.WriteLine();
  230. return true;
  231. }
  232. static bool MassivFormRandom(out int strings, out int columns, out int[,] matr)
  233. {
  234. Console.WriteLine("Введите количество строк массива");
  235. do
  236. {
  237. strings = NumberEntry();
  238. if (strings <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
  239. } while (strings <= 0);
  240. Console.WriteLine("Введите количество столбцов массива");
  241. do
  242. {
  243. columns = NumberEntry();
  244. if (columns <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
  245. } while (columns <= 0);
  246. matr = new int[strings, columns];
  247. Random rand = new Random();
  248. for (int i = 0; i < strings; i++)
  249. for (int j = 0; j < columns; j++)
  250. matr[i, j] = rand.Next(0, 10);
  251. Console.WriteLine();
  252. Console.WriteLine("Двумерный массив создан.");
  253. Console.WriteLine();
  254. return true;
  255. }
  256. static void MassivPrint(int strings, int columns, int[,] matr)
  257. {
  258. Console.WriteLine();
  259. for (int i = 0; i < strings; i++)
  260. {
  261. for (int j = 0; j < columns; j++)
  262. {
  263. Console.Write("{0,5}",matr[i, j]);
  264. }
  265. Console.WriteLine();
  266. }
  267. }
  268. static void MatrDeleteString0(ref int strings, int columns, ref int[,] matr)
  269. {
  270. int count = 0;
  271. foreach (int elem in matr)
  272. {
  273. if (elem == 0) count++;
  274. }
  275. if (count != 0)
  276. {
  277. while (count != 0)
  278. {
  279. int k;
  280. for (int i = 0; i < strings; i++)
  281. {
  282. for (int j = 0; j < columns; j++)
  283. {
  284. if (matr[i, j] == 0)
  285. {
  286. k = i;
  287. for (i = k; i < strings - 1; i++)
  288. {
  289. for (j = 0; j < columns; j++)
  290. {
  291. matr[i, j] = matr[i + 1, j];
  292. }
  293. }
  294. for (j = 0; j < columns; j++)
  295. matr[i, j] = 0;
  296. strings--;
  297. }
  298. }
  299. }
  300. count--;
  301. }
  302. Console.WriteLine();
  303. Console.WriteLine("Удаление выполнено");
  304. }
  305. else Console.WriteLine("Нулевых элементов в массиве нет");
  306. if (strings == 0) Console.WriteLine("Массив пустой");
  307. }
  308. static void Matr() // организация меню для двумерного массива
  309. {
  310. int[,] matr = null;
  311. int strings = 0;
  312. int columns = 0;
  313. bool formedMatr = false;
  314. int otv;
  315. int otvForm;
  316. do
  317. {
  318. PrintMenuMass2();
  319. otv = NumberEntry();
  320. switch (otv)
  321. {
  322. case 1:
  323. {
  324. PrintMenuForm();
  325. do
  326. {
  327. otvForm = NumberEntry();
  328. switch (otvForm)
  329. {
  330. case 1:
  331. {
  332. formedMatr = MassivFormConsole(out strings, out columns, out matr);
  333. break;
  334. }
  335. case 2:
  336. {
  337. formedMatr = MassivFormRandom(out strings, out columns, out matr);
  338. break;
  339. }
  340. case 0: break;
  341. default:
  342. {
  343. Console.WriteLine("Такого пункта нет в меню");
  344. break;
  345. }
  346. }
  347. } while ((otvForm != 0) && (otvForm != 1) && (otvForm != 2));
  348. break;
  349. }
  350. case 2:
  351. {
  352. if (formedMatr) MassivPrint(strings, columns, matr);
  353. else Console.WriteLine("Матрица не создана");
  354. break;
  355. }
  356. case 3:
  357. {
  358. if (formedMatr) MatrDeleteString0(ref strings, columns, ref matr);
  359. else Console.WriteLine("Матрица не создана");
  360. break;
  361. }
  362. case 0: break;
  363. default:
  364. {
  365. Console.WriteLine("Такого пункта нет в меню");
  366. break;
  367. }
  368. }
  369.  
  370. } while (otv != 0);
  371. }
  372.  
  373. // работа с рваным массивом (Jagged)
  374. static bool MassivFormConsole(out int strings, out int[][] jagged)
  375. {
  376. int i, j;
  377. int columns;
  378. Console.WriteLine("Введите количество строк рваного массива");
  379. do
  380. {
  381. strings = NumberEntry();
  382. if (strings <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
  383. } while (strings <= 0);
  384. jagged = new int[strings][];
  385. for (i = 0; i < strings; i++)
  386. {
  387. Console.WriteLine("Введите количество элементов в строке");
  388. do
  389. {
  390. columns = NumberEntry();
  391. if (columns <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
  392. } while (columns <= 0);
  393. jagged[i] = new int[columns];
  394. for (j = 0; j < columns; j++)
  395. {
  396. Console.WriteLine("Введите элемент матрицы c координатами ({0};{1})", i + 1, j + 1);
  397. jagged[i][j] = NumberEntry();
  398. }
  399. }
  400. Console.WriteLine();
  401. Console.WriteLine("Рваный массив создан.");
  402. Console.WriteLine();
  403. return true;
  404. }
  405. static bool MassivFormRandom(out int strings, out int[][] jagged)
  406. {
  407. int i, j;
  408. int columns;
  409. Random rnd = new Random();
  410. Console.WriteLine("Введите количество строк рваного массива");
  411. do
  412. {
  413. strings = NumberEntry();
  414. if (strings <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
  415. } while (strings <= 0);
  416. jagged = new int[strings][];
  417. for (i = 0; i < strings; i++)
  418. {
  419. Console.WriteLine("Введите количество элементов в строке");
  420. do
  421. {
  422. columns = NumberEntry();
  423. if (columns <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
  424. } while (columns <= 0);
  425. jagged[i] = new int[columns];
  426. for (j = 0; j < columns; j++)
  427. {
  428. jagged[i][j] = rnd.Next(-100, 100);
  429. }
  430. }
  431. Console.WriteLine();
  432. Console.WriteLine("Рваный массив создан.");
  433. Console.WriteLine();
  434. return true;
  435. }
  436. static void MassivPrint(int strings, int[][] jagged)
  437. {
  438. Console.WriteLine();
  439. for (int i = 0; i < strings; i++)
  440. {
  441. for (int j = 0; j < jagged[i].Length; j++)
  442. Console.Write("{0,5}", jagged[i][j]);
  443. Console.WriteLine();
  444. }
  445. }
  446. static void JaggedAddKStringsInTheEnd(ref int strings, ref int[][] jagged)
  447. {
  448. Random rnd = new Random();
  449. int k, p;
  450. Console.WriteLine("Введите количество строк");
  451. do
  452. {
  453. k = NumberEntry();
  454. if (k <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
  455. } while (k <= 0);
  456. int[][] temp = new int[k][];
  457. for (int i = 0; i < k; i++)
  458. {
  459. Console.WriteLine("Введите количество элементов в строке");
  460. do
  461. {
  462. p = NumberEntry();
  463. if (p <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
  464. } while (p <= 0);
  465. temp[i] = new int[p];
  466. for (int j = 0; j < p; j++)
  467. {
  468. temp[i][j] = rnd.Next(-100, 100);
  469. }
  470. }
  471. int[][] temp2 = new int[strings + k][];
  472.  
  473. for (int i = 0; i < strings; i++)
  474. {
  475. temp2[i] = new int[jagged[i].Length];
  476. for (int j = 0; j < jagged[i].Length; j++)
  477. {
  478. temp2[i][j] = jagged[i][j];
  479. }
  480. }
  481. for (int i = 0; i < k; i++)
  482. {
  483. temp2[i + strings] = new int[temp[i].Length];
  484. for (int j = 0; j < temp[i].Length; j++)
  485. {
  486. temp2[i + strings][j] = temp[i][j];
  487. }
  488. }
  489.  
  490. jagged = temp2;
  491. strings = strings + k;
  492.  
  493. Console.WriteLine("Добавление выполнено");
  494. }
  495. static void Jagged() // организация меню для рваного массива
  496. {
  497. int[][] jagged = new int[0][];
  498. int strings = 0;
  499. bool formedJagged = false;
  500. int otv;
  501. int otvForm;
  502. do
  503. {
  504. PrintMenuMass3();
  505. otv = NumberEntry();
  506. switch (otv)
  507. {
  508. case 1:
  509. {
  510. PrintMenuForm();
  511. do
  512. {
  513. otvForm = NumberEntry();
  514. switch (otvForm)
  515. {
  516. case 1:
  517. {
  518. formedJagged = MassivFormConsole(out strings, out jagged);
  519. break;
  520. }
  521. case 2:
  522. {
  523. formedJagged = MassivFormRandom(out strings, out jagged);
  524. break;
  525. }
  526. case 0: break;
  527. default:
  528. {
  529. Console.WriteLine("Такого пункта нет в меню");
  530. break;
  531. }
  532. }
  533. } while ((otvForm != 0) && (otvForm != 1) && (otvForm != 2));
  534. break;
  535. }
  536. case 2:
  537. {
  538. if (formedJagged) MassivPrint(strings, jagged);
  539. else Console.WriteLine("Рваный массив не создан");
  540. break;
  541. }
  542. case 3:
  543. {
  544. if (formedJagged) JaggedAddKStringsInTheEnd(ref strings, ref jagged);
  545. else Console.WriteLine("Рваный массив не создан");
  546. break;
  547. }
  548. case 0: break;
  549. default:
  550. {
  551. Console.WriteLine("Такого пункта нет в меню");
  552. break;
  553. }
  554. }
  555.  
  556. } while (otv != 0);
  557. }
  558. static void Main(string[] args)
  559. {
  560. Console.WriteLine();
  561. int otv;
  562. do
  563. {
  564. PrintMenu();
  565. otv = NumberEntry();
  566. switch (otv)
  567. {
  568. case 1:
  569. {
  570. Massiv();
  571. break;
  572. }
  573. case 2:
  574. {
  575. Matr();
  576. break;
  577. }
  578. case 3:
  579. {
  580. Jagged();
  581. break;
  582. }
  583. case 0: break;
  584. default:
  585. {
  586. Console.WriteLine("Такого пункта нет в меню");
  587. break;
  588. }
  589. }
  590. } while (otv != 0);
  591. Console.ReadKey();
  592. }
  593. }
  594. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement