Garloon

4.6

Sep 4th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CSLight
  8. {
  9.     class Menu
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[] array = new int[0];
  14.             int[] array2 = new int[0];
  15.  
  16.             bool arrayOperations = true;
  17.             bool addInArray, replaceInArray, delInArray;
  18.            
  19.             while (arrayOperations == true)
  20.             {
  21.                 Console.Clear();
  22.                 Console.Write("Первый массив: ");
  23.                 for (int i = 0; i < array.Length; i++)
  24.                 {
  25.                     Console.Write(array[i] + " ");
  26.                 }
  27.                 Console.Write("\nВторой массив: ");
  28.                 for (int i = 0; i < array2.Length; i++)
  29.                 {
  30.                     Console.Write(array2[i] + " ");
  31.                 }
  32.                 Console.WriteLine("\n\nДоступные команды: \n\nadd - добавить элемент в массив \ndel - удалить элемент массива \nreplace - перенести один массив в другой \nexit - выход из программы\n");
  33.                 Console.Write("\nВаше действие: ");
  34.                
  35.                 switch (Console.ReadLine())
  36.                 {
  37.                     case "add":
  38.                         addInArray = true;
  39.                         while (addInArray == true)
  40.                         {
  41.                             Console.Clear();
  42.                             Console.Write("\nВведите номер массива, в который вы хотите добавить число: ");
  43.                             switch (Convert.ToInt32(Console.ReadLine()))
  44.                             {
  45.                                 case 1:
  46.                                     AddInArray(ref array);
  47.                                     addInArray = false;
  48.                                     break;
  49.                                 case 2:
  50.                                     AddInArray(ref array2);
  51.                                     addInArray = false;
  52.                                     break;
  53.                                 default:
  54.                                     Console.WriteLine("Такой массив отсутствует!");
  55.                                     break;
  56.                             }
  57.                         }
  58.                         break;
  59.                     case "replace":
  60.                         replaceInArray = true;
  61.                         while (replaceInArray == true)
  62.                         {
  63.                             Console.Clear();
  64.                             Console.Write("\nВведите номер массива, из которого вы перенести число: ");
  65.                             switch (Convert.ToInt32(Console.ReadLine()))
  66.                             {
  67.                                 case 1:
  68.                                     Replace(ref array, ref array2);
  69.                                     replaceInArray = false;
  70.                                     break;
  71.                                 case 2:
  72.                                     Replace(ref array2, ref array);
  73.                                     replaceInArray = false;
  74.                                     break;
  75.                                 default:
  76.                                     Console.WriteLine("Такой массив отсутствует!");
  77.                                     break;
  78.                             }
  79.                         }
  80.                         break;
  81.                     case "del":
  82.                         delInArray = true;
  83.                         while (delInArray == true)
  84.                         {
  85.                             Console.Clear();
  86.                             Console.Write("\nВведите номер массива, из которого вы хотите удалить число: ");
  87.                             switch (Convert.ToInt32(Console.ReadLine()))
  88.                             {
  89.                                 case 1:
  90.                                     Remove(ref array);
  91.                                     delInArray = false;
  92.                                     break;
  93.                                 case 2:
  94.                                     Remove(ref array2);
  95.                                     delInArray = false;
  96.                                     break;
  97.                                 default:
  98.                                     Console.WriteLine("Такой массив отсутствует!");
  99.                                     break;
  100.                             }
  101.                         }
  102.                         break;
  103.                     case "exit":
  104.                         arrayOperations = false;
  105.                         break;
  106.                     default:
  107.                         ConsoleMessage("\nНеизвестная команда!");
  108.                         break;
  109.                 }
  110.             }
  111.         }
  112.  
  113.         static void ConsoleMessage(string message)
  114.         {
  115.             Console.ForegroundColor = ConsoleColor.Red;
  116.             Console.WriteLine(message);
  117.             Console.ResetColor();
  118.             Console.ReadKey();
  119.             Console.Clear();
  120.         }
  121.  
  122.         static void Replace(ref int[] array, ref int[] array2)
  123.         {
  124.             Console.Write("\nВведите индекс элемента который хотите перенести: ");
  125.             int input = Convert.ToInt32(Console.ReadLine());
  126.             int index = 0;
  127.             int count = 0;
  128.             for (int i = 0; i < array.Length; ++i)
  129.             {
  130.                 if (i == input)
  131.                 {
  132.                     index = i;
  133.                     count = array[i];
  134.                     break;
  135.                 }
  136.             }
  137.  
  138.             int[] tempName = new int[array.Length - 1];
  139.             for (int i = 0, j = 0; i < tempName.Length; ++i, ++j)
  140.             {
  141.                 if (j == index)
  142.                     ++j;
  143.  
  144.                 tempName[i] = array[j];
  145.             }
  146.             array = tempName;
  147.  
  148.             int[] tempName2 = new int[array2.Length + 1];
  149.             for (int i = 0; i < array2.Length; i++)
  150.             {
  151.                 tempName2[i] = array2[i];
  152.             }
  153.             tempName2[tempName2.Length - 1] = count;
  154.             array2 = tempName2;
  155.         }
  156.        
  157.         static void AddInArray(ref int[] array)
  158.         {
  159.             Console.Write("\nВведите число: ");
  160.             int userInput = Convert.ToInt32(Console.ReadLine());
  161.             int[] tempName = new int[array.Length + 1];
  162.             for (int i = 0; i < array.Length; i++)
  163.             {
  164.                 tempName[i] = array[i];
  165.             }
  166.             tempName[tempName.Length - 1] = userInput;
  167.             array = tempName;
  168.  
  169.             Console.Write("Успешно добавлено!");
  170.             Console.ReadKey();
  171.         }
  172.  
  173.         static void Remove(ref int[] array)
  174.         {
  175.             Console.Write("\nВведите индекс элемента который хотите удалить: ");
  176.             int input = Convert.ToInt32(Console.ReadLine());
  177.             int index = 0;
  178.  
  179.             for (int i = 0; i < array.Length; ++i)
  180.             {
  181.                 if (i == input)
  182.                 {
  183.                     index = i;
  184.                     break;
  185.                 }
  186.             }
  187.  
  188.             int[] tempName = new int[array.Length - 1];
  189.  
  190.             for (int i = 0, j = 0; i < tempName.Length; ++i, ++j)
  191.             {
  192.                 if (j == index)
  193.                     ++j;
  194.  
  195.                 tempName[i] = array[j];
  196.             }
  197.             array = tempName;
  198.  
  199.             Console.Write("Успешно удалено!");
  200.             Console.ReadKey();
  201.         }
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment