Esempio n. 1
0
 public static void Sdvig(ref int[] arr)
 //Сдвиг одномерного массива влево на заданное кол-во элементов
 {
     if (arr.Length == 0)
     {
         Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine("\n Массив пуст, операция невозможна.");
         Console.ForegroundColor = ConsoleColor.White;
     }
     else
     {
         Console.ForegroundColor = ConsoleColor.White;
         Console.WriteLine("\n На сколько элементов нужно сдвинуть массив?");
         int M = CreatingArrays.EnterForArray();
         for (int p = 0; p < M; p++)
         {
             int mind = arr[0];
             for (int i = 1; i < arr.Length; i++)
             {
                 arr[i - 1] = arr[i];
             }
             arr[arr.Length - 1] = mind;
         }
     }
     OutputArrays.OutputArray(arr);
     Console.WriteLine();
 }
Esempio n. 2
0
 public static void Sort(ref int[] arr)
 //Сортировка массива простым включением
 {
     if (arr.Length == 0)
     {
         Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine("\n Массив пуст, операция невозможна.");
         Console.ForegroundColor = ConsoleColor.White;
     }
     else
     {
         int[] result = new int[arr.Length];
         for (int i = 0; i < arr.Length; i++)
         {
             int j = i;
             while (j > 0 && result[j - 1] > arr[i])
             {
                 result[j] = result[j - 1];
                 j--;
             }
             result[j] = arr[i];
         }
         arr = result;
     }
     OutputArrays.OutputArray(arr);
     Console.WriteLine();
 }
Esempio n. 3
0
        public static int[][] CreateIntRag()
        //Создание рваного массива
        {
            int column, strings, k, i, j;

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(" Введите количество строк в массиве:");
            do
            {
                strings = EnterForArray();
                if (strings == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("\n Извините, работа с пустым массивом невозможна. Введите число больше нуля:");
                    Console.ForegroundColor = ConsoleColor.White;
                }
            } while (strings < 1);
            int[][] arr = new int[strings][];
            for (i = 0; i < strings; i++)
            {
                Console.WriteLine(" Введите количество столбцов в {0}-й строке массива:", i + 1);
                column = EnterForArray();
                arr[i] = new int[column];
            }
            k = EnterSelection();
            if (k == 1)
            {
                for (i = 0; i < strings; i++)
                {
                    for (j = 0; j < arr[i].Length; j++)
                    {
                        bool ok = false;
                        Console.WriteLine("\n Введите {0}-й элемент массива, {1}-й строки", j + 1, i + 1);
                        do
                        {
                            ok = int.TryParse(Console.ReadLine(), out arr[i][j]);
                            if (!ok)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("\n Ввод неправильный, нужно ввести целое число. Повторите попытку:");
                                Console.ForegroundColor = ConsoleColor.White;
                            }
                        } while (!ok);
                    }
                }
            }
            else
            {
                Random rnd = new Random();
                for (i = 0; i < strings; i++)
                {
                    for (j = 0; j < arr[i].Length; j++)
                    {
                        arr[i][j] = rnd.Next(-10, 10);
                    }
                }
            }
            OutputArrays.OutputArray(arr);
            return(arr);
        }
Esempio n. 4
0
 public static void DeleteString(ref int[][] arr)
 //Удаление строк, содержащих нули
 {
     if (arr.GetLength(0) == 0)
     {
         Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine(" Массив пуст, операция невозможна.\n");
         Console.ForegroundColor = ConsoleColor.White;
     }
     else
     {
         int k, num = 0, kol = 0, flag = 0;
         int length = arr.GetLength(0);
         for (int i = 0; i < arr.GetLength(0); i++)
         {
             for (int j = 0; j < arr[i].Length; j++)
             {
                 if (arr[i][j] == 0)
                 {
                     arr[i] = null;
                     kol++;
                     flag++;
                     break;
                 }
             }
         }
         while (kol != 0)
         {
             length--;
             int[][] newarr = new int[length][];
             for (int i = 0; i < arr.GetLength(0); i++)
             {
                 if (arr[i] == null)
                 {
                     kol--;
                     num = i;
                     break;
                 }
             }
             for (k = 0; k < num; k++)
             {
                 newarr[k] = arr[k];
             }
             for (k = num; k < length; k++)
             {
                 newarr[k] = arr[k + 1];
             }
             arr = newarr;
         }
         if (flag == 0)
         {
             Console.ForegroundColor = ConsoleColor.Green;
             Console.WriteLine(" В массиве нет строк, содержащих нули.");
             Console.ForegroundColor = ConsoleColor.White;
         }
         Console.WriteLine();
         OutputArrays.OutputArray(arr);
     }
     //Console.WriteLine();
 }
Esempio n. 5
0
        public static void AddString(ref int[,] arr)
        //Добавление строки в конец массива
        {
            int[,] newarr = new int[arr.GetLength(0) + 1, arr.GetLength(1)];
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    newarr[i, j] = arr[i, j];
                }
            }
            int k;

            k = FuncFor1Arrays.AddSelection();
            if (k == 1)
            {
                ArrayFilling.Hand(arr.GetLength(0) + 1, arr.GetLength(1), ref newarr, arr.GetLength(0));
            }
            else
            {
                ArrayFilling.Random(arr.GetLength(0) + 1, arr.GetLength(1), ref newarr, arr.GetLength(0));
            }
            arr = newarr;
            OutputArrays.OutputArray(arr);
            //Console.WriteLine();
        }
Esempio n. 6
0
 public static void DeleteKey(ref int[] arr)
 //Удаление элемента с заданным значением
 {
     if (arr.Length == 0)
     {
         Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine(" Массив пуст, операция невозможна.");
         Console.ForegroundColor = ConsoleColor.White;
     }
     else
     {
         int  i, key, num = 0, flag = 0;
         bool ok;
         Console.ForegroundColor = ConsoleColor.White;
         Console.WriteLine(" Введите значение элемента, который хотите удалить:");
         do
         {
             ok = int.TryParse(Console.ReadLine(), out key);
             if (!ok)
             {
                 Console.ForegroundColor = ConsoleColor.Red;
                 Console.WriteLine("\n Ошибка, ожидалось целое число. Попробуйте ещё раз:");
                 Console.ForegroundColor = ConsoleColor.White;
             }
         } while (!ok);
         for (i = 0; i < arr.Length; i++)
         {
             if (arr[i] == key)
             {
                 num = i;
                 flag++;
             }
         }
         if (flag > 0)
         {
             int   m      = arr.Length - 1;
             int[] newarr = new int[m];
             for (i = 0; i < num; i++)
             {
                 newarr[i] = arr[i];
             }
             for (i = num; i < m; i++)
             {
                 newarr[i] = arr[i + 1];
             }
             arr = newarr;
         }
         else
         {
             Console.ForegroundColor = ConsoleColor.Red;
             Console.WriteLine("\n Элемента с заданным значением не существует.");
             Console.ForegroundColor = ConsoleColor.White;
         }
     }
     Console.WriteLine();
     OutputArrays.OutputArray(arr);
     //Console.WriteLine();
 }
Esempio n. 7
0
 public static void DeleteString(ref char[][] arr)
 //Удаление первой строки с 3мя и более гласными
 {
     char[] glas = { 'а', 'у', 'о', 'ы', 'и', 'э', 'я', 'ю', 'е', 'ё', 'А', 'У', 'О', 'Ы', 'И', 'Э', 'Я', 'Ю', 'Е', 'Ё' };
     if (arr.GetLength(0) == 0)
     {
         Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine(" Массив пуст, операция невозможна.\n");
         Console.ForegroundColor = ConsoleColor.White;
     }
     else
     {
         int k, num = -1, kol = 0;
         for (int i = 0; i < arr.GetLength(0); i++)
         {
             for (int j = 0; j < arr[i].Length; j++)
             {
                 if (glas.Contains(arr[i][j]))
                 {
                     kol++;
                 }
             }
             if (kol >= 3)
             {
                 num = i;
                 break;
             }
             else
             {
                 kol = 0;
             }
         }
         if (num > -1)
         {
             char[][] newarr = new char[arr.GetLength(0) - 1][];
             //Array.Copy(arr, 0, newarr, 0, num - 1);
             //Array.Copy(arr, num, newarr, num, arr.GetLength(0) - num - 2);
             for (k = 0; k < num; k++)
             {
                 newarr[k] = arr[k];
             }
             for (k = num; k < arr.GetLength(0) - 1; k++)
             {
                 newarr[k] = arr[k + 1];
             }
             arr = newarr;
         }
         else
         {
             Console.ForegroundColor = ConsoleColor.Green;
             Console.WriteLine(" В массиве нет строк, с тремя и более гласными.");
             Console.ForegroundColor = ConsoleColor.White;
         }
         Console.WriteLine();
         OutputArrays.OutputArray(arr);
     }
     //Console.WriteLine();
 }
Esempio n. 8
0
        public static void Add(ref int[] arr)
        //Добавление заданного кол-ва элементов с заданного номера
        {
            if (arr.Length == 0)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("\n Массив пуст, поэтому нужно добавлять элементы с номера '1'.");
                Console.ForegroundColor = ConsoleColor.White;
            }
            int adds, num, k, i = 0;

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("\n Введите кол-во элементов, которое хотите добавить:");
            adds = CreatingArrays.EnterForArray();
            Console.WriteLine("\n Введите номер элемента, с которого нужно вставить элементы");
            do
            {
                num = CreatingArrays.EnterForArray();
                if ((num > arr.Length + 1) || (num < 1))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("\n Извините, такого номера в данном массиве не существует. Попробуйте ещё раз:");
                    Console.ForegroundColor = ConsoleColor.White;
                }
            } while ((num > arr.Length + 1) || (num < 1));
            num--;
            int[] newarr = new int[arr.Length + adds];
            for (i = 0; i < num; i++)
            {
                newarr[i] = arr[i];
            }
            for (i = num; i < arr.Length; i++)
            {
                newarr[i + adds] = arr[i];
            }
            arr = newarr;
            k   = AddSelection();
            if (k == 1)
            {
                ArrayFilling.Hand(num + adds, ref arr, num);
            }
            else
            {
                ArrayFilling.Random(num + adds, ref arr, num);
            }
            OutputArrays.OutputArray(arr);
            Console.WriteLine();
        }
Esempio n. 9
0
 public static void FindKey(ref int[] arr)
 //Поиск элемента с заданным значением в одномерном массиве
 {
     if (arr.Length == 0)
     {
         Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine("\n Массив пуст, операция невозможна.");
         Console.ForegroundColor = ConsoleColor.White;
     }
     else
     {
         bool ok, t = false;
         int  key;
         Console.WriteLine("\n Введите значение элемента, который хотите найти:");
         do
         {
             ok = int.TryParse(Console.ReadLine(), out key);
             if (!ok)
             {
                 Console.ForegroundColor = ConsoleColor.Red;
                 Console.WriteLine("\n Ошибка, ожидалось целое число. Попробуйте ещё раз:");
                 Console.ForegroundColor = ConsoleColor.White;
             }
         } while (!ok);
         for (int i = 0; i < arr.Length; i++)
         {
             if (arr[i] == key)
             {
                 Console.ForegroundColor = ConsoleColor.Green;
                 Console.WriteLine("\n Найден элемент с заданным значением = {0}. Он находится под номером {1}.", arr[i], i + 1);
                 Console.WriteLine("\n Количество сравнений = " + (i + 1));
                 Console.WriteLine();
                 t = true;
                 break;
             }
         }
         if (!t)
         {
             Console.ForegroundColor = ConsoleColor.Red;
             Console.WriteLine("Элементов с заданным значением в этом массиве не существует");
             Console.ForegroundColor = ConsoleColor.White;
         }
     }
     OutputArrays.OutputArray(arr);
     Console.WriteLine();
 }
Esempio n. 10
0
        public static int[,] Create2()
        //Создание двумерного массива
        {
            int k, high = 0, width = 0;

            AskSize(ref high, ref width);
            int[,] arr = new int[high, width];
            k          = EnterSelection();
            if (k == 1)
            {
                ArrayFilling.Hand(high, width, ref arr);
            }
            else
            {
                ArrayFilling.Random(high, width, ref arr);
            }
            OutputArrays.OutputArray(arr);
            return(arr);
        }
Esempio n. 11
0
        public static int[] Create1()
        //Создание одномерного массива
        {
            int k, kolvo;

            kolvo = AskSize();
            int[] arr = new int[kolvo];
            k = EnterSelection();
            if (k == 1)
            {
                ArrayFilling.Hand(kolvo, ref arr);
            }
            else
            {
                ArrayFilling.Random(kolvo, ref arr);
            }
            OutputArrays.OutputArray(arr);
            return(arr);
        }
Esempio n. 12
0
 public static void DeleteNum(ref int[] arr)
 //Удаление элемента массива с заданным номером
 {
     if (arr.Length == 0)
     {
         Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine("\n Массив пуст, операция невозможна.");
         Console.ForegroundColor = ConsoleColor.White;
     }
     else
     {
         int i, num;
         Console.ForegroundColor = ConsoleColor.White;
         Console.WriteLine("\n Введите номер элемента, который хотите удалить:");
         do
         {
             num = CreatingArrays.EnterForArray();
             if ((num > arr.Length) || (num == 0))
             {
                 Console.ForegroundColor = ConsoleColor.Red;
                 Console.WriteLine("\n Ошибка, ожидался номер элемента, существующий в данном массиве. Попробуйте ещё раз:");
                 Console.ForegroundColor = ConsoleColor.White;
             }
         }while ((num > arr.Length) || (num == 0));
         int   m      = arr.Length - 1;
         int[] newarr = new int[m];
         for (i = 0; i < num - 1; i++)
         {
             newarr[i] = arr[i];
         }
         for (i = num - 1; i < m; i++)
         {
             newarr[i] = arr[i + 1];
         }
         arr = newarr;
     }
     OutputArrays.OutputArray(arr);
     Console.WriteLine();
 }
Esempio n. 13
0
        public static void Bin(ref int[] arr)
        //Бинарный поиск элемента с заданным значением в отсортированном массиве
        {
            if (arr.Length == 0)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\n Массив пуст, операция невозможна.");
                Console.ForegroundColor = ConsoleColor.White;
            }
            else
            {
                bool ok;
                int  el, k = -2, u = 0;
                for (int i = 0; i < arr.Length; i++)
                {
                    for (int j = i + 1; j < arr.Length - i; j++)
                    {
                        if (arr[i] > arr[j])
                        {
                            u++;
                        }
                    }
                }
                if ((arr[arr.Length - 1] < arr[0]) || (u > 0))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("\n Невозможно выполнить поиск. Сначала отсортируйте массив.");
                    Console.ForegroundColor = ConsoleColor.White;
                }
                else
                {
                    Console.WriteLine("\n Введите значение элемента, который хотите найти:");
                    do
                    {
                        ok = int.TryParse(Console.ReadLine(), out el);
                        if (!ok)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("\n Ошибка, ожидалось целое число. Попробуйте ещё раз:");
                            Console.ForegroundColor = ConsoleColor.White;
                        }
                    } while (!ok);
                    if ((el < arr[0]) || (el > arr[arr.Length - 1]))
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("\n Элемент с заданным значением на найден.");
                        Console.ForegroundColor = ConsoleColor.White;
                    }
                    else
                    {
                        int first = 0;
                        int last  = arr.Length;
                        while (first < last)
                        {
                            int mid = first + (last - first) / 2;
                            if (el <= arr[mid])
                            {
                                last = mid;
                                k++;
                            }

                            else
                            {
                                first = mid + 1;
                                k++;
                            }
                        }

                        if (arr[last] == el)
                        {
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine("\n Найден элемент с заданным значением = {0}. Он находится под номером {1}.", el, last + 1);
                            Console.WriteLine("\n Количество сравнений = " + k);
                            Console.WriteLine();
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("\n Элемент с заданным значением на найден.");
                            Console.ForegroundColor = ConsoleColor.White;
                        }
                        Console.WriteLine();
                    }
                }
            }
            OutputArrays.OutputArray(arr);
            Console.WriteLine();
        }
Esempio n. 14
0
        public static char[][] CreateCharRag()
        //Создание рваного массива
        {
            int column, strings, k, i, j;

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(" Введите количество строк в массиве:");
            do
            {
                strings = EnterForArray();
                if (strings == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("\n Извините, работа с пустым массивом невозможна. Введите число больше нуля:");
                    Console.ForegroundColor = ConsoleColor.White;
                }
            } while (strings < 1);
            char[][] arr = new char[strings][];
            for (i = 0; i < strings; i++)
            {
                Console.WriteLine(" Введите количество столбцов в {0}-й строке массива:", i + 1);
                column = EnterForArray();
                arr[i] = new char[column];
            }
            k = EnterSelection();
            if (k == 1)
            {
                for (i = 0; i < strings; i++)
                {
                    for (j = 0; j < arr[i].Length; j++)
                    {
                        bool ok = false;
                        Console.WriteLine("\n Введите {0}-й элемент массива, {1}-й строки", j + 1, i + 1);
                        do
                        {
                            ok = char.TryParse(Console.ReadLine(), out arr[i][j]);
                            if (!ok)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("\n Ввод неправильный, нужно ввести целое число. Повторите попытку:");
                                Console.ForegroundColor = ConsoleColor.White;
                            }
                        } while (!ok);
                    }
                }
            }
            else
            {
                Random rnd   = new Random();
                string chars = "$%#@!*абвгдеёжзийклмнопрстуфхцчшщъыьэюя1234567890?;:АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ^&";
                for (i = 0; i < strings; i++)
                {
                    for (j = 0; j < arr[i].Length; j++)
                    {
                        arr[i][j] = chars[rnd.Next(0, chars.Length - 1)];
                    }
                }
            }
            Console.WriteLine();
            OutputArrays.OutputArray(arr);
            return(arr);
        }