Advertisement
Guest User

Misha spasi plis

a guest
Dec 10th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.14 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. using ConsoleApplication1;
  7.  
  8.  
  9. namespace Plastya_loser
  10. {
  11.     class Program
  12.     {
  13.         static void ReadIntNum(string messege, out int number)
  14.         {
  15.             bool OK;
  16.             do
  17.             {
  18.                 Console.WriteLine(messege);
  19.                 OK = Int32.TryParse(Console.ReadLine(), out number);
  20.                 if (!OK) Console.WriteLine("Ошибка ввода, введите число снова");
  21.             } while (!OK);
  22.  
  23.  
  24.         }
  25.         public static int[] FormMasRandom(int size) //массив задается датчиком случайных чисел
  26.         {
  27.             Random rand = new Random(0);
  28.             int[] mas = new int[size];
  29.             for (int i = 0; i < size; i++)
  30.             {
  31.                 mas[i] = rand.Next(-100, 100);
  32.             }
  33.  
  34.  
  35.             return mas;
  36.         }
  37.         public static int[] FormMasConsole(int size) //пользователь создает массив
  38.         {
  39.             bool OK, OK2; int j = 1;
  40.             int[] mas = new int[size];
  41.             for (int i = 0; i < size; i++)
  42.             {
  43.                 do
  44.                 {
  45.                     Console.Write("Введите {0}-й элемент массива: ", j);
  46.                     OK = Int32.TryParse(Console.ReadLine(), out mas[i]);
  47.                     if (!OK) Console.WriteLine("Ошибка ввода");
  48.                 } while (!OK);
  49.                 j++;
  50.             }
  51.             OK2 = (CheckMas(mas, size) == true);
  52.             if (!OK2) Console.WriteLine("Вы создали нулевой массив, вам следует перезапустить программу");
  53.             return mas;
  54.  
  55.  
  56.         }
  57.         static bool CheckMas(int[] mas, int size)
  58.         {
  59.             int count = 0;
  60.             foreach (int el in mas) { if (el == 0) count++; };
  61.             if (count == size) return false;
  62.             else return true;
  63.         } //проверка на "нулевость
  64.         public static void PrintMas(int[] mas, int size) //вывод массива на экран
  65.         {
  66.             foreach (int el in mas) Console.Write(el + " ");
  67.         }
  68.         public static int[] DelEvenNum(int[] mas, int size) //удалить четные элементв
  69.  
  70.         {
  71.             int count = 0;
  72.             foreach (int el in mas)
  73.             {
  74.                 if (el % 2 != 0) count++;
  75.             }
  76.             if (count == 0) Console.WriteLine("все элементы в массиве четные, следовательно они все будут удалены\n\t\t\t па-бам! пустота!");
  77.             int[] oddMas = new int[count];
  78.             int j = 0;
  79.             for (int i = 0; i < size; i++)
  80.             {
  81.                 if (mas[i] % 2 != 0)
  82.                 {
  83.                     oddMas[j] = mas[i];
  84.                     j++;
  85.                 }
  86.             }
  87.             return oddMas;
  88.         }
  89.         public static int[] AddKtoMass( int[] mas, int size) //добавить к элементов к массиву
  90.         {
  91.             int k; bool neok;
  92.             if () { }
  93.             do
  94.             {
  95.                 string messege = "Введите число k, чтобы увеличить массив на k элементов";
  96.                 ReadIntNum(messege, out k);
  97.                 neok = (k <= 0);
  98.                 if (neok) Console.WriteLine("Число k введено некорректно");
  99.             } while (neok);
  100.             int newsize = k + size;
  101.             int[] Kmas = new int[newsize];
  102.             Random rnd = new Random();
  103.             for (int i = 0; i < newsize; i++)
  104.             {
  105.                 if (i < size) Kmas[i] = mas[i];
  106.                 if (i >= size) Kmas[i] = rnd.Next(-100, 100);
  107.             }
  108.            
  109.             mas = Kmas;
  110.             return mas;
  111.         }
  112.         public static void ShiftMas(ref int[] mas, int size) //передвинуть массив
  113.         {
  114.             int i = 0, j = size - 1;
  115.             int temp;
  116.             while (i < j)
  117.             {
  118.                 while (i < j && mas[i] >= 0)
  119.                     i++;
  120.                 while (i < j && mas[j] < 0)
  121.                     j--;
  122.                 if (i < j)
  123.                 {
  124.                     temp = mas[i]; mas[i] = mas[j];
  125.                     mas[j] = temp;
  126.                     i++; j--;
  127.                 }
  128.  
  129.             }
  130.         }
  131.         public static void FindFirstMinus(int[] mas, int size) //найти первый отрицательный элемент
  132.         {
  133.             int a = 0;
  134.             for (int i = 0; i < size; i++)
  135.             {
  136.                 if (mas[i] < 0)
  137.                 { Console.WriteLine(" Первый отрицательный элемент это {0}-ый элемент: {1}", i + 1, mas[i]); break; }
  138.                 else a++;
  139.  
  140.  
  141.             }
  142.             if (a == size) Console.WriteLine("В изначальном массиве нет отрицательных элементов");
  143.         }
  144.         public static int[] SortMas(int[] mas, int size)
  145.         {
  146.             int min, temp;
  147.             for (int i = 0; i < size - 1; i++)
  148.             {
  149.                 min = i;
  150.                 for (int j = i + 1; j < size; j++)
  151.                 {
  152.                     if (mas[j] < mas[min])
  153.                     {
  154.                         min = j;
  155.                     }
  156.                 }
  157.                 temp = mas[min];
  158.                 mas[min] = mas[i];
  159.                 mas[i] = temp;
  160.             }
  161.  
  162.             return mas;
  163.  
  164.         }
  165.         static void SwitchMenu(int[] mas, int size)
  166.         {
  167.             int answer; int[] oddMas, massiv;
  168.             do
  169.             {
  170.                 Console.WriteLine();
  171.                 Console.WriteLine("\nЧто вы хотите сделать с массивом?\nВведите число");
  172.                 Console.WriteLine("1.\t Удалить чётные элементы");
  173.                 Console.WriteLine("2.\t Добавить К элементов в конец массива");
  174.                 Console.WriteLine("3.\t + элементы переставить в начало массива, - в конец");
  175.                 Console.WriteLine("4.\t Найти первый отрицательный элемент");
  176.                 Console.WriteLine("5.\t Отсортировать методом простого выбора");
  177.                 Console.WriteLine("0.\t Чтобы завершить работу программы");
  178.  
  179.                 Int32.TryParse(Console.ReadLine(), out answer);
  180.                 if (answer > 5 || answer < 0) Console.WriteLine("Вы ввели неправильное число!.\nПравила даны не просто так!");
  181.                 switch (answer)
  182.                 {
  183.                     case 1:
  184.                         mas = DelEvenNum(mas, size);
  185.  
  186.                         PrintMas(mas, size);
  187.                         break;
  188.                     case 2:
  189.                         mas=AddKtoMass( mas, size); PrintMas(mas, size);
  190.                         break;
  191.                     case 3:
  192.                         ShiftMas(ref mas, size); PrintMas(mas, size);
  193.                         break;
  194.                     case 4:
  195.                         FindFirstMinus(mas, size);
  196.                         break;
  197.                     case 5:
  198.                         SortMas(mas, size); PrintMas(mas, size);
  199.                         break;
  200.                     case 0: Console.WriteLine("До новых встреч в лабе №5!!!"); break;
  201.                 }
  202.             } while (answer != 0);
  203.         }
  204.         static void Main(string[] args)
  205.         {
  206.             int size, answer; int[] mas;
  207.             do
  208.             {
  209.  
  210.                 ReadIntNum("Создайте массив.\nВведите количество элементов в массиве", out size);
  211.                 if (size <= 0) Console.WriteLine("Колво элементов должно быть неотрицательным целым числом\nПовторите ввод.");
  212.             } while (size <= 0);
  213.             do
  214.             {
  215.                 Console.WriteLine("Введите:\n 1.\tчтобы заполнить массив самостоятельно\n 2.\tЧтобы заполнить массив случайными числами");
  216.                 Int32.TryParse(Console.ReadLine(), out answer);
  217.   if (answer > 2 || answer < 1) Console.WriteLine("Вы ввели неправильное число!.\nПравила даны не просто так!");
  218.                 switch (answer)
  219.                 {
  220.                     case 1:
  221.                         mas = FormMasConsole(size); PrintMas(mas, size); SwitchMenu(mas, size);
  222.                         break;
  223.                     case 2:
  224.                         mas = FormMasRandom(size); PrintMas(mas, size); SwitchMenu(mas, size);
  225.                         break;
  226.                 }
  227.             } while (answer != 1 && answer != 2);
  228.  
  229.  
  230.  
  231.         }
  232.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement