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;
- using ConsoleApplication1;
- namespace Plastya_loser
- {
- class Program
- {
- static void ReadIntNum(string messege, out int number)
- {
- bool OK;
- do
- {
- Console.WriteLine(messege);
- OK = Int32.TryParse(Console.ReadLine(), out number);
- if (!OK) Console.WriteLine("Ошибка ввода, введите число снова");
- } while (!OK);
- }
- public static int[] FormMasRandom(int size) //массив задается датчиком случайных чисел
- {
- Random rand = new Random(0);
- int[] mas = new int[size];
- for (int i = 0; i < size; i++)
- {
- mas[i] = rand.Next(-100, 100);
- }
- return mas;
- }
- public static int[] FormMasConsole(int size) //пользователь создает массив
- {
- bool OK, OK2; int j = 1;
- int[] mas = new int[size];
- for (int i = 0; i < size; i++)
- {
- do
- {
- Console.Write("Введите {0}-й элемент массива: ", j);
- OK = Int32.TryParse(Console.ReadLine(), out mas[i]);
- if (!OK) Console.WriteLine("Ошибка ввода");
- } while (!OK);
- j++;
- }
- OK2 = (CheckMas(mas, size) == true);
- if (!OK2) Console.WriteLine("Вы создали нулевой массив, вам следует перезапустить программу");
- return mas;
- }
- static bool CheckMas(int[] mas, int size)
- {
- int count = 0;
- foreach (int el in mas) { if (el == 0) count++; };
- if (count == size) return false;
- else return true;
- } //проверка на "нулевость
- public static void PrintMas(int[] mas, int size) //вывод массива на экран
- {
- foreach (int el in mas) Console.Write(el + " ");
- }
- public static int[] DelEvenNum(int[] mas, int size) //удалить четные элементв
- {
- int count = 0;
- foreach (int el in mas)
- {
- if (el % 2 != 0) count++;
- }
- if (count == 0) Console.WriteLine("все элементы в массиве четные, следовательно они все будут удалены\n\t\t\t па-бам! пустота!");
- int[] oddMas = new int[count];
- int j = 0;
- for (int i = 0; i < size; i++)
- {
- if (mas[i] % 2 != 0)
- {
- oddMas[j] = mas[i];
- j++;
- }
- }
- return oddMas;
- }
- public static int[] AddKtoMass( int[] mas, int size) //добавить к элементов к массиву
- {
- int k; bool neok;
- if () { }
- do
- {
- string messege = "Введите число k, чтобы увеличить массив на k элементов";
- ReadIntNum(messege, out k);
- neok = (k <= 0);
- if (neok) Console.WriteLine("Число k введено некорректно");
- } while (neok);
- int newsize = k + size;
- int[] Kmas = new int[newsize];
- Random rnd = new Random();
- for (int i = 0; i < newsize; i++)
- {
- if (i < size) Kmas[i] = mas[i];
- if (i >= size) Kmas[i] = rnd.Next(-100, 100);
- }
- mas = Kmas;
- return mas;
- }
- public static void ShiftMas(ref int[] mas, int size) //передвинуть массив
- {
- int i = 0, j = size - 1;
- int temp;
- while (i < j)
- {
- while (i < j && mas[i] >= 0)
- i++;
- while (i < j && mas[j] < 0)
- j--;
- if (i < j)
- {
- temp = mas[i]; mas[i] = mas[j];
- mas[j] = temp;
- i++; j--;
- }
- }
- }
- public static void FindFirstMinus(int[] mas, int size) //найти первый отрицательный элемент
- {
- int a = 0;
- for (int i = 0; i < size; i++)
- {
- if (mas[i] < 0)
- { Console.WriteLine(" Первый отрицательный элемент это {0}-ый элемент: {1}", i + 1, mas[i]); break; }
- else a++;
- }
- if (a == size) Console.WriteLine("В изначальном массиве нет отрицательных элементов");
- }
- public static int[] SortMas(int[] mas, int size)
- {
- int min, temp;
- for (int i = 0; i < size - 1; i++)
- {
- min = i;
- for (int j = i + 1; j < size; j++)
- {
- if (mas[j] < mas[min])
- {
- min = j;
- }
- }
- temp = mas[min];
- mas[min] = mas[i];
- mas[i] = temp;
- }
- return mas;
- }
- static void SwitchMenu(int[] mas, int size)
- {
- int answer; int[] oddMas, massiv;
- do
- {
- Console.WriteLine();
- Console.WriteLine("\nЧто вы хотите сделать с массивом?\nВведите число");
- Console.WriteLine("1.\t Удалить чётные элементы");
- Console.WriteLine("2.\t Добавить К элементов в конец массива");
- Console.WriteLine("3.\t + элементы переставить в начало массива, - в конец");
- Console.WriteLine("4.\t Найти первый отрицательный элемент");
- Console.WriteLine("5.\t Отсортировать методом простого выбора");
- Console.WriteLine("0.\t Чтобы завершить работу программы");
- Int32.TryParse(Console.ReadLine(), out answer);
- if (answer > 5 || answer < 0) Console.WriteLine("Вы ввели неправильное число!.\nПравила даны не просто так!");
- switch (answer)
- {
- case 1:
- mas = DelEvenNum(mas, size);
- PrintMas(mas, size);
- break;
- case 2:
- mas=AddKtoMass( mas, size); PrintMas(mas, size);
- break;
- case 3:
- ShiftMas(ref mas, size); PrintMas(mas, size);
- break;
- case 4:
- FindFirstMinus(mas, size);
- break;
- case 5:
- SortMas(mas, size); PrintMas(mas, size);
- break;
- case 0: Console.WriteLine("До новых встреч в лабе №5!!!"); break;
- }
- } while (answer != 0);
- }
- static void Main(string[] args)
- {
- int size, answer; int[] mas;
- do
- {
- ReadIntNum("Создайте массив.\nВведите количество элементов в массиве", out size);
- if (size <= 0) Console.WriteLine("Колво элементов должно быть неотрицательным целым числом\nПовторите ввод.");
- } while (size <= 0);
- do
- {
- Console.WriteLine("Введите:\n 1.\tчтобы заполнить массив самостоятельно\n 2.\tЧтобы заполнить массив случайными числами");
- Int32.TryParse(Console.ReadLine(), out answer);
- if (answer > 2 || answer < 1) Console.WriteLine("Вы ввели неправильное число!.\nПравила даны не просто так!");
- switch (answer)
- {
- case 1:
- mas = FormMasConsole(size); PrintMas(mas, size); SwitchMenu(mas, size);
- break;
- case 2:
- mas = FormMasRandom(size); PrintMas(mas, size); SwitchMenu(mas, size);
- break;
- }
- } while (answer != 1 && answer != 2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement