Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Readline
- {
- class Program
- {
- static void delelement(ref int num,ref string[] a)
- {
- string[] temp = new string[a.Length-1];
- for (int i = num; i <a.Length; i++)
- {
- a[i-1] = a[i];//после символа с индексом удаления, мы присваиваем предыдущим элементам значение последующих,смещая элементы влево на 1
- }
- for (int i = 0; i < temp.Length; i++)
- {
- temp[i] = a[i]; //и затем, когда нужные элементы умещаются в длину-1, перемещаем их в temp
- }
- a = temp;
- }
- static void addelement(string element,ref string[] b)
- {
- string[] tempb = new string[b.Length + 1]; //увеличение массива
- for (int i = 0; i < b.Length; i++)
- {
- tempb[i] = b[i];
- }
- tempb[tempb.Length - 1] = element;
- b = tempb;
- }
- static void addmas(ref string [] c, string[] d)
- {
- string[] e = new string[c.Length + d.Length];
- for (int i = 0; i < c.Length; i++)
- e[i] = c[i];
- for (int i = 0; i < d.Length; i++)
- e[i+c.Length] = d[i];
- c = e;
- }
- static void Main(string[] args)
- {
- while (true)
- {
- Console.WriteLine("Выберите фунцкию: \n1.Удалить элемент из массива\n2.Добавить элемент в массив\n3.Перенос массива в массив");
- int sw = Convert.ToInt32(Console.ReadLine());
- switch (sw)
- {
- case 1:
- Console.WriteLine("Будет удален элемент из массива:");
- string[] a = { "1", "2", "3", "4", "5" };
- for(int i=0;i<a.Length;i++)
- {
- Console.Write(a[i]+" ");
- }
- Console.WriteLine();
- Console.Write("Введите номер элемента: ");
- int num = Convert.ToInt32(Console.ReadLine());
- delelement(ref num,ref a);
- for (int i = 0; i < a.Length; i++)
- {
- Console.Write(a[i]+" ");
- }
- Console.WriteLine();
- break;
- case 2:
- Console.WriteLine("Будет удален элемент из массива:");
- string[] b = { "1 ", "2 ", "3 ", " ", " " };
- for (int i = 0; i < b.Length; i++)
- {
- Console.Write(b[i]);
- }
- Console.WriteLine();
- Console.Write("Введите новый элемент: ");
- string element = Console.ReadLine();
- addelement(element, ref b);
- for (int i = 0; i < b.Length; i++)
- {
- Console.Write(b[i]);
- }
- Console.WriteLine();
- break;
- case 3:
- Console.WriteLine("К этому массиву будем добавлять другой:");
- string[] c = { "1", "2", "3", "4", "5" };
- for (int i = 0; i < c.Length; i++)
- {
- Console.Write(c[i]);
- }
- Console.WriteLine();
- Console.WriteLine("Введите длину массива для вставки:");
- int lenmas = Convert.ToInt32(Console.ReadLine());
- string[] d = new string[lenmas];
- Console.WriteLine("Введите элементы массива:");
- for (int i=0;i<lenmas;i++)
- {
- d[i] = Console.ReadLine();
- }
- addmas(ref c, d);
- for (int i = 0; i < c.Length; i++)
- {
- Console.Write(c[i]);
- }
- Console.WriteLine();
- break;
- default:
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment