Advertisement
mon0l1t

4.2 функции с массивами

May 22nd, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.92 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 ConsoleApplication17
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[] a = new int[0];
  14.             int[] b = new int[0];
  15.             menu(ref a,ref b);
  16.         }
  17.         static void menu(ref int[] a,ref int [] b)
  18.         {
  19.             int command = 0;
  20.             bool close = false;
  21.             while (!close)
  22.             {
  23.                 Console.Clear();
  24.                
  25.                     string s = "1) Ввести массив.\n" +
  26.                 "2) Присвоить элементам массива случайные числа.\n"
  27.                 +"3) Вывести массив\n"
  28.                 +"4) Удалить элемент в массиве\n"
  29.                 +"5) Добавить элемент в массив\n"
  30.                 +"6) Перемешать элементы массива\n"
  31.                 +"7) Присвоить существующий массив новому и вывести его\n"                
  32.                 +"8) Выход\n"+
  33.                 "Введите номер команды: ";
  34.                  command = ReadInt(s);
  35.  
  36.  
  37.  
  38.                
  39.  
  40.  
  41.  
  42.                 switch (command)
  43.                 {
  44.                     case 1:
  45.                         input(ref a);
  46.                         break;
  47.                     case 2:
  48.                         rnd(ref a);
  49.                         break;
  50.                     case 3:
  51.                         Console.WriteLine(output(a));
  52.                         Console.ReadLine();
  53.                         break;
  54.                     case 4:
  55.                         RemoveAt(ref a);
  56.                         break;
  57.                     case 5:
  58.                         include(ref a);
  59.                         break;
  60.                     case 6:
  61.                         Shuffle(ref a);
  62.                         break;
  63.                     case 7:
  64.                         equivalence(ref a , ref b);
  65.                         break;
  66.                     case 8:
  67.  
  68.                         close = true;
  69.                         break;
  70.                 }
  71.  
  72.             }
  73.         }
  74.             static void RemoveAt(ref int [] a)
  75.         {
  76.             output(a);
  77.             int[] temp = new int[a.Length - 1];
  78.          
  79.             string s;
  80.  
  81.          
  82.  
  83.             s = "Введите номер откуда вы хотели удалить элемент из массива: ";
  84.             int idx = ReadInt(output(a)+"\n"+s)-1;
  85.  
  86.  
  87.             for (int i = 0; i < a.Length; i++)
  88.             {
  89.                 if (i < idx)
  90.                 {
  91.                     temp[i] = a[i];
  92.                 }
  93.                 else if (i > idx)
  94.                 {
  95.                     temp[i - 1] = a[i];
  96.                 }
  97.             }
  98.             a = temp;
  99.  
  100.         }
  101.       static void equivalence(ref int []a,ref int []b)
  102.         {
  103.            
  104.             b = a;
  105.           Console.WriteLine(  output(b));
  106.             Console.ReadKey();
  107.         }
  108.     static void Shuffle(ref int []a)
  109.         {
  110.  
  111.             Random rand = new Random();
  112.             a = a.OrderBy(x => rand.Next()).ToArray();
  113.             Console.WriteLine(output(a));
  114.  
  115.         }
  116.         static void rnd(ref int[] a)
  117.         {
  118.             Random rand = new Random();
  119.             string s = "Введите количество элементов которые бы вы хотели в массиве";
  120.             int n = ReadInt(s);
  121.             int[] temp = new int[n];
  122.             a = temp;
  123.             for (int i = 0; i < a.Length; i++)
  124.                 a[i] = rand.Next(0,10);
  125.             Console.WriteLine(output(a));
  126.         }
  127.         static void input(ref int[] a)
  128.         {
  129.            
  130.            
  131.             string s = "Введите количество элементов которые бы вы хотели ввести в массив";
  132.             int n = ReadInt(s);
  133.             int[] temp = new int[n];
  134.             a = temp;
  135.             s = "Введите массив" ;
  136.  
  137.             for (int i = 0; i < a.Length; i++)
  138.                 a[i] = ReadInt(output(a)+"\n"+s);
  139.         }
  140.         static string output(int [] a)
  141.         {
  142.            
  143.             string s =
  144.             "Массив:\n";
  145.             for (int i = 0; i < a.Length; i++)
  146.                 s+=a[i] + " ";
  147.            
  148.             return s;
  149.         }
  150.         static void include(ref int[] a )
  151.         {
  152.             int[] temp =new int [a.Length + 1];
  153.            
  154.            
  155.             string s;
  156.  
  157.             s = "Введите число которое хотели бы добавить в массив";
  158.             int b = ReadInt(s);
  159.  
  160.             s = "Введите номер куда вы хотели добавить новый элемент"+b+" в массив";
  161.             int idx =ReadInt(output(a) + "\n" + s)-1;
  162.  
  163.  
  164.             for (int i = 0; i < a.Length; i++)
  165.             {
  166.                 if (i < idx)
  167.                 {
  168.                     temp[i] = a[i];
  169.                 }
  170.                 else if (i >= idx)
  171.                 {
  172.                     temp[i + 1] = a[i];
  173.                 }
  174.             }
  175.             temp[idx] = b;
  176.             a = temp;
  177.  
  178.         }
  179.         static int ReadInt(string s="")
  180.         {
  181.  
  182.             bool success = false;
  183.             int a = 0;
  184.             while (!success)
  185.             {
  186.                 Console.Clear();
  187.                 Console.WriteLine(s);
  188.                 Console.Write("Введите число: ");
  189.                 success = Int32.TryParse(Console.ReadLine(), out a);
  190.                 if (!success)
  191.                 {
  192.                     Console.WriteLine("Ошибка ввода!\n");
  193.                     Console.ReadKey();
  194.                 }
  195.             }
  196.             return a;
  197.         }
  198.  
  199.     }
  200.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement