Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication17
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] a = new int[0];
- int[] b = new int[0];
- menu(ref a,ref b);
- }
- static void menu(ref int[] a,ref int [] b)
- {
- int command = 0;
- bool close = false;
- while (!close)
- {
- Console.Clear();
- string s = "1) Ввести массив.\n" +
- "2) Присвоить элементам массива случайные числа.\n"
- +"3) Вывести массив\n"
- +"4) Удалить элемент в массиве\n"
- +"5) Добавить элемент в массив\n"
- +"6) Перемешать элементы массива\n"
- +"7) Присвоить существующий массив новому и вывести его\n"
- +"8) Выход\n"+
- "Введите номер команды: ";
- command = ReadInt(s);
- switch (command)
- {
- case 1:
- input(ref a);
- break;
- case 2:
- rnd(ref a);
- break;
- case 3:
- Console.WriteLine(output(a));
- Console.ReadLine();
- break;
- case 4:
- RemoveAt(ref a);
- break;
- case 5:
- include(ref a);
- break;
- case 6:
- Shuffle(ref a);
- break;
- case 7:
- equivalence(ref a , ref b);
- break;
- case 8:
- close = true;
- break;
- }
- }
- }
- static void RemoveAt(ref int [] a)
- {
- output(a);
- int[] temp = new int[a.Length - 1];
- string s;
- s = "Введите номер откуда вы хотели удалить элемент из массива: ";
- int idx = ReadInt(output(a)+"\n"+s)-1;
- for (int i = 0; i < a.Length; i++)
- {
- if (i < idx)
- {
- temp[i] = a[i];
- }
- else if (i > idx)
- {
- temp[i - 1] = a[i];
- }
- }
- a = temp;
- }
- static void equivalence(ref int []a,ref int []b)
- {
- b = a;
- Console.WriteLine( output(b));
- Console.ReadKey();
- }
- static void Shuffle(ref int []a)
- {
- Random rand = new Random();
- a = a.OrderBy(x => rand.Next()).ToArray();
- Console.WriteLine(output(a));
- }
- static void rnd(ref int[] a)
- {
- Random rand = new Random();
- string s = "Введите количество элементов которые бы вы хотели в массиве";
- int n = ReadInt(s);
- int[] temp = new int[n];
- a = temp;
- for (int i = 0; i < a.Length; i++)
- a[i] = rand.Next(0,10);
- Console.WriteLine(output(a));
- }
- static void input(ref int[] a)
- {
- string s = "Введите количество элементов которые бы вы хотели ввести в массив";
- int n = ReadInt(s);
- int[] temp = new int[n];
- a = temp;
- s = "Введите массив" ;
- for (int i = 0; i < a.Length; i++)
- a[i] = ReadInt(output(a)+"\n"+s);
- }
- static string output(int [] a)
- {
- string s =
- "Массив:\n";
- for (int i = 0; i < a.Length; i++)
- s+=a[i] + " ";
- return s;
- }
- static void include(ref int[] a )
- {
- int[] temp =new int [a.Length + 1];
- string s;
- s = "Введите число которое хотели бы добавить в массив";
- int b = ReadInt(s);
- s = "Введите номер куда вы хотели добавить новый элемент"+b+" в массив";
- int idx =ReadInt(output(a) + "\n" + s)-1;
- for (int i = 0; i < a.Length; i++)
- {
- if (i < idx)
- {
- temp[i] = a[i];
- }
- else if (i >= idx)
- {
- temp[i + 1] = a[i];
- }
- }
- temp[idx] = b;
- a = temp;
- }
- static int ReadInt(string s="")
- {
- bool success = false;
- int a = 0;
- while (!success)
- {
- Console.Clear();
- Console.WriteLine(s);
- Console.Write("Введите число: ");
- success = Int32.TryParse(Console.ReadLine(), out a);
- if (!success)
- {
- Console.WriteLine("Ошибка ввода!\n");
- Console.ReadKey();
- }
- }
- return a;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement