Benjail

4.6

Sep 3rd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.06 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Readline
  4. {
  5.     class Program
  6.     {
  7.         static void delelement(ref int num,ref string[] a)
  8.         {
  9.             string[] temp = new string[a.Length-1];
  10.                 for (int i = num; i <a.Length; i++)
  11.                 {
  12.                 a[i-1] = a[i];//после символа с индексом удаления, мы присваиваем предыдущим элементам значение последующих,смещая элементы влево на 1
  13.                 }
  14.             for (int i = 0; i < temp.Length; i++)
  15.             {
  16.                 temp[i] = a[i]; //и затем, когда нужные элементы умещаются в длину-1, перемещаем их в temp
  17.             }
  18.                 a = temp;            
  19.         }
  20.         static void addelement(string element,ref string[] b)
  21.         {
  22.             string[] tempb = new string[b.Length + 1]; //увеличение массива
  23.             for (int i = 0; i < b.Length; i++)
  24.             {
  25.                 tempb[i] = b[i];
  26.             }
  27.             tempb[tempb.Length - 1] = element;
  28.             b = tempb;
  29.         }
  30.         static void addmas(ref string [] c,  string[] d)
  31.         {
  32.             string[] e = new string[c.Length + d.Length];
  33.             for (int i = 0; i < c.Length; i++)
  34.                 e[i] = c[i];
  35.             for (int i = 0; i < d.Length; i++)
  36.                 e[i+c.Length] = d[i];
  37.             c = e;
  38.         }
  39.             static void Main(string[] args)
  40.         {
  41.             while (true)
  42.             {
  43.                 Console.WriteLine("Выберите фунцкию: \n1.Удалить элемент из массива\n2.Добавить элемент в массив\n3.Перенос массива в массив");
  44.                 int sw = Convert.ToInt32(Console.ReadLine());
  45.                 switch (sw)
  46.                 {
  47.                     case 1:
  48.                         Console.WriteLine("Будет удален элемент из массива:");
  49.                         string[] a = { "1", "2", "3", "4", "5" };
  50.                         for(int i=0;i<a.Length;i++)
  51.                         {
  52.                             Console.Write(a[i]+" ");
  53.                         }
  54.                         Console.WriteLine();
  55.                         Console.Write("Введите номер элемента: ");
  56.                         int num = Convert.ToInt32(Console.ReadLine());
  57.                         delelement(ref num,ref  a);
  58.                         for (int i = 0; i < a.Length; i++)
  59.                         {
  60.                             Console.Write(a[i]+" ");                            
  61.                         }
  62.                         Console.WriteLine();
  63.                         break;
  64.                        
  65.                     case 2:
  66.                         Console.WriteLine("Будет удален элемент из массива:");
  67.                         string[] b = { "1 ", "2 ", "3 ", " ", " " };
  68.                         for (int i = 0; i < b.Length; i++)
  69.                         {
  70.                             Console.Write(b[i]);
  71.                         }
  72.                         Console.WriteLine();
  73.                         Console.Write("Введите новый элемент: ");
  74.                         string element = Console.ReadLine();
  75.                         addelement(element, ref b);
  76.                         for (int i = 0; i < b.Length; i++)
  77.                         {
  78.                             Console.Write(b[i]);
  79.                         }
  80.                         Console.WriteLine();
  81.                         break;
  82.                     case 3:
  83.                         Console.WriteLine("К этому массиву будем добавлять другой:");
  84.                         string[] c = { "1", "2", "3", "4", "5" };
  85.                         for (int i = 0; i < c.Length; i++)
  86.                         {
  87.                             Console.Write(c[i]);
  88.                         }
  89.                         Console.WriteLine();
  90.  
  91.                         Console.WriteLine("Введите длину массива для вставки:");
  92.                         int lenmas = Convert.ToInt32(Console.ReadLine());
  93.                         string[] d = new string[lenmas];
  94.                         Console.WriteLine("Введите элементы массива:");
  95.                         for (int i=0;i<lenmas;i++)
  96.                         {
  97.                             d[i] = Console.ReadLine();
  98.                         }
  99.                         addmas(ref c, d);
  100.                         for (int i = 0; i < c.Length; i++)
  101.                         {
  102.                             Console.Write(c[i]);
  103.                         }
  104.                         Console.WriteLine();
  105.                         break;
  106.                     default:
  107.                         break;                      
  108.                 }
  109.                 Console.ReadKey();
  110.                 Console.Clear();                
  111.             }
  112.         }      
  113.     }
  114. }
Add Comment
Please, Sign In to add comment