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;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void PrintMenu()
- {
- Console.WriteLine("Выберите действие:");
- Console.WriteLine();
- Console.WriteLine("1.Работа с одномерным массивом");
- Console.WriteLine("2.Работа с двумерным массивом");
- Console.WriteLine("3.Работа с рваным массивом");
- Console.WriteLine("0.Выход");
- }
- static void PrintMenuMass()
- {
- Console.WriteLine();
- Console.WriteLine("1.Создать массив");
- Console.WriteLine("2.Напечатать массив");
- Console.WriteLine("3.Добавить 0 после каждого четного элемента");
- Console.WriteLine("0.Назад");
- }
- static void PrintMenuMass2()
- {
- Console.WriteLine();
- Console.WriteLine("1.Создать массив");
- Console.WriteLine("2.Напечатать массив");
- Console.WriteLine("3.Удалить строки с нулевыми элементами");
- Console.WriteLine("0.Назад");
- }
- static void PrintMenuMass3()
- {
- Console.WriteLine();
- Console.WriteLine("1.Создать массив");
- Console.WriteLine("2.Напечатать массив");
- Console.WriteLine("3.Добавить К строк в конец массива");
- Console.WriteLine("0.Назад");
- }
- static void PrintMenuForm()
- {
- Console.WriteLine();
- Console.WriteLine("1. Вручную");
- Console.WriteLine("2. С помощью ДСЧ");
- Console.WriteLine("0. Назад");
- }
- static int NumberEntry() // функция ввода целого числа
- {
- int x;
- string str;
- bool ok;
- do
- {
- str = Console.ReadLine();
- ok = Int32.TryParse(str, out x);
- if (!ok) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое число");
- } while (!ok);
- return x;
- }
- //Работа с одномерным массивом
- static bool MassivFormConsole(out int size, out int[] a)
- {
- Console.WriteLine("Введите количество элементов массива");
- do
- {
- size = NumberEntry();
- if (size <= 0)
- Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число");
- } while (size <= 0);
- a = new int[size];
- for (int i = 0; i < size; i++)
- {
- Console.WriteLine("Введите {0} элемент массива", i + 1);
- a[i] = NumberEntry();
- }
- Console.WriteLine();
- Console.WriteLine("Одномерный массив создан.");
- Console.WriteLine();
- return true;
- }
- static bool MassivFormRandom(out int size, out int[] a)
- {
- Console.WriteLine("Введите количество элементов массива");
- size = NumberEntry();
- a = new int[size];
- Random rand = new Random();
- for (int i = 0; i < size; i++)
- {
- a[i] = rand.Next(-100, 100);
- }
- Console.WriteLine();
- Console.WriteLine("Одномерный массив создан.");
- Console.WriteLine();
- return true;
- }
- static void MassivPrint(int size, int[] a)
- {
- Console.WriteLine();
- for (int i = 0; i < size; i++)
- Console.Write(a[i] + " ");
- Console.WriteLine();
- }
- static void MassivAddElem(ref int size, ref int[] a)
- {
- int cnt = 0;
- for (int i = 0; i < size; i++)
- {
- if (a[i] % 2 == 0) cnt = cnt + 1;
- }
- int [] a2 = new int[cnt + size];
- int j = 0;
- for (int i = 0; i < size; i++)
- {
- if (a[i] % 2 == 0)
- {
- a2[j] = a[i];
- j++;
- a2[j] = 0;
- j++;
- }
- else
- {
- a2[j] = a[i];
- j++;
- }
- }
- a = a2;
- size += cnt;
- Console.WriteLine();
- if (cnt == 0) Console.WriteLine("Четных элементов в массиве нет");
- else Console.WriteLine("КРЯ КРЯ");
- }
- static void Massiv() // организация меню для одномерного массива
- {
- int[] a = null;
- int size = 0;
- bool formedMassiv = false;
- int otv;
- int otvForm;
- do
- {
- PrintMenuMass();
- otv = NumberEntry();
- switch (otv)
- {
- case 1:
- {
- PrintMenuForm();
- do
- {
- otvForm = NumberEntry();
- switch (otvForm)
- {
- case 1:
- {
- formedMassiv = MassivFormConsole(out size, out a);
- break;
- }
- case 2:
- {
- formedMassiv = MassivFormRandom(out size, out a);
- break;
- }
- case 4: break;
- default:
- {
- Console.WriteLine("Такого пункта нет в меню");
- break;
- }
- }
- } while ((otvForm != 0) && (otvForm != 1) && (otvForm != 2));
- break;
- }
- case 2:
- {
- if (formedMassiv) MassivPrint(size, a);
- else Console.WriteLine("Массив не создан");
- break;
- }
- case 3:
- {
- if (formedMassiv) MassivAddElem(ref size, ref a);
- else Console.WriteLine("Массив не создан");
- break;
- }
- case 0: break;
- default:
- {
- Console.WriteLine("Такого пункта нет в меню");
- break;
- }
- }
- } while (otv != 0);
- }
- // Работа с двумерным массивом (Matr)
- static bool MassivFormConsole(out int strings, out int columns, out int[,] matr)
- {
- Console.WriteLine("Введите количество строк массива");
- do
- {
- strings = NumberEntry();
- if (strings <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
- } while (strings <= 0);
- Console.WriteLine("Введите количество столбцов массива");
- do
- {
- columns = NumberEntry();
- if (columns <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
- } while (columns <= 0);
- matr = new int[strings, columns];
- for (int i = 0; i < strings; i++)
- {
- for (int j = 0; j < columns; j++)
- {
- Console.WriteLine("Введите элемент массива c координатами ({0};{1})", i + 1, j + 1);
- matr[i, j] = NumberEntry();
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- Console.WriteLine("Двумерный массив создан.");
- Console.WriteLine();
- return true;
- }
- static bool MassivFormRandom(out int strings, out int columns, out int[,] matr)
- {
- Console.WriteLine("Введите количество строк массива");
- do
- {
- strings = NumberEntry();
- if (strings <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
- } while (strings <= 0);
- Console.WriteLine("Введите количество столбцов массива");
- do
- {
- columns = NumberEntry();
- if (columns <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
- } while (columns <= 0);
- matr = new int[strings, columns];
- Random rand = new Random();
- for (int i = 0; i < strings; i++)
- for (int j = 0; j < columns; j++)
- matr[i, j] = rand.Next(0, 10);
- Console.WriteLine();
- Console.WriteLine("Двумерный массив создан.");
- Console.WriteLine();
- return true;
- }
- static void MassivPrint(int strings, int columns, int[,] matr)
- {
- Console.WriteLine();
- for (int i = 0; i < strings; i++)
- {
- for (int j = 0; j < columns; j++)
- {
- Console.Write("{0,5}",matr[i, j]);
- }
- Console.WriteLine();
- }
- }
- static void MatrDeleteString0(ref int strings, int columns, ref int[,] matr)
- {
- int count = 0;
- foreach (int elem in matr)
- {
- if (elem == 0) count++;
- }
- if (count != 0)
- {
- while (count != 0)
- {
- int k;
- for (int i = 0; i < strings; i++)
- {
- for (int j = 0; j < columns; j++)
- {
- if (matr[i, j] == 0)
- {
- k = i;
- for (i = k; i < strings - 1; i++)
- {
- for (j = 0; j < columns; j++)
- {
- matr[i, j] = matr[i + 1, j];
- }
- }
- for (j = 0; j < columns; j++)
- matr[i, j] = 0;
- strings--;
- }
- }
- }
- count--;
- }
- Console.WriteLine();
- Console.WriteLine("Удаление выполнено");
- }
- else Console.WriteLine("Нулевых элементов в массиве нет");
- if (strings == 0) Console.WriteLine("Массив пустой");
- }
- static void Matr() // организация меню для двумерного массива
- {
- int[,] matr = null;
- int strings = 0;
- int columns = 0;
- bool formedMatr = false;
- int otv;
- int otvForm;
- do
- {
- PrintMenuMass2();
- otv = NumberEntry();
- switch (otv)
- {
- case 1:
- {
- PrintMenuForm();
- do
- {
- otvForm = NumberEntry();
- switch (otvForm)
- {
- case 1:
- {
- formedMatr = MassivFormConsole(out strings, out columns, out matr);
- break;
- }
- case 2:
- {
- formedMatr = MassivFormRandom(out strings, out columns, out matr);
- break;
- }
- case 0: break;
- default:
- {
- Console.WriteLine("Такого пункта нет в меню");
- break;
- }
- }
- } while ((otvForm != 0) && (otvForm != 1) && (otvForm != 2));
- break;
- }
- case 2:
- {
- if (formedMatr) MassivPrint(strings, columns, matr);
- else Console.WriteLine("Матрица не создана");
- break;
- }
- case 3:
- {
- if (formedMatr) MatrDeleteString0(ref strings, columns, ref matr);
- else Console.WriteLine("Матрица не создана");
- break;
- }
- case 0: break;
- default:
- {
- Console.WriteLine("Такого пункта нет в меню");
- break;
- }
- }
- } while (otv != 0);
- }
- // работа с рваным массивом (Jagged)
- static bool MassivFormConsole(out int strings, out int[][] jagged)
- {
- int i, j;
- int columns;
- Console.WriteLine("Введите количество строк рваного массива");
- do
- {
- strings = NumberEntry();
- if (strings <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
- } while (strings <= 0);
- jagged = new int[strings][];
- for (i = 0; i < strings; i++)
- {
- Console.WriteLine("Введите количество элементов в строке");
- do
- {
- columns = NumberEntry();
- if (columns <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
- } while (columns <= 0);
- jagged[i] = new int[columns];
- for (j = 0; j < columns; j++)
- {
- Console.WriteLine("Введите элемент матрицы c координатами ({0};{1})", i + 1, j + 1);
- jagged[i][j] = NumberEntry();
- }
- }
- Console.WriteLine();
- Console.WriteLine("Рваный массив создан.");
- Console.WriteLine();
- return true;
- }
- static bool MassivFormRandom(out int strings, out int[][] jagged)
- {
- int i, j;
- int columns;
- Random rnd = new Random();
- Console.WriteLine("Введите количество строк рваного массива");
- do
- {
- strings = NumberEntry();
- if (strings <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
- } while (strings <= 0);
- jagged = new int[strings][];
- for (i = 0; i < strings; i++)
- {
- Console.WriteLine("Введите количество элементов в строке");
- do
- {
- columns = NumberEntry();
- if (columns <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
- } while (columns <= 0);
- jagged[i] = new int[columns];
- for (j = 0; j < columns; j++)
- {
- jagged[i][j] = rnd.Next(-100, 100);
- }
- }
- Console.WriteLine();
- Console.WriteLine("Рваный массив создан.");
- Console.WriteLine();
- return true;
- }
- static void MassivPrint(int strings, int[][] jagged)
- {
- Console.WriteLine();
- for (int i = 0; i < strings; i++)
- {
- for (int j = 0; j < jagged[i].Length; j++)
- Console.Write("{0,5}", jagged[i][j]);
- Console.WriteLine();
- }
- }
- static void JaggedAddKStringsInTheEnd(ref int strings, ref int[][] jagged)
- {
- Random rnd = new Random();
- int k, p;
- Console.WriteLine("Введите количество строк");
- do
- {
- k = NumberEntry();
- if (k <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
- } while (k <= 0);
- int[][] temp = new int[k][];
- for (int i = 0; i < k; i++)
- {
- Console.WriteLine("Введите количество элементов в строке");
- do
- {
- p = NumberEntry();
- if (p <= 0) Console.WriteLine("Ошибка ввода. Попробуйте ввести целое положительное число.");
- } while (p <= 0);
- temp[i] = new int[p];
- for (int j = 0; j < p; j++)
- {
- temp[i][j] = rnd.Next(-100, 100);
- }
- }
- int[][] temp2 = new int[strings + k][];
- for (int i = 0; i < strings; i++)
- {
- temp2[i] = new int[jagged[i].Length];
- for (int j = 0; j < jagged[i].Length; j++)
- {
- temp2[i][j] = jagged[i][j];
- }
- }
- for (int i = 0; i < k; i++)
- {
- temp2[i + strings] = new int[temp[i].Length];
- for (int j = 0; j < temp[i].Length; j++)
- {
- temp2[i + strings][j] = temp[i][j];
- }
- }
- jagged = temp2;
- strings = strings + k;
- Console.WriteLine("Добавление выполнено");
- }
- static void Jagged() // организация меню для рваного массива
- {
- int[][] jagged = new int[0][];
- int strings = 0;
- bool formedJagged = false;
- int otv;
- int otvForm;
- do
- {
- PrintMenuMass3();
- otv = NumberEntry();
- switch (otv)
- {
- case 1:
- {
- PrintMenuForm();
- do
- {
- otvForm = NumberEntry();
- switch (otvForm)
- {
- case 1:
- {
- formedJagged = MassivFormConsole(out strings, out jagged);
- break;
- }
- case 2:
- {
- formedJagged = MassivFormRandom(out strings, out jagged);
- break;
- }
- case 0: break;
- default:
- {
- Console.WriteLine("Такого пункта нет в меню");
- break;
- }
- }
- } while ((otvForm != 0) && (otvForm != 1) && (otvForm != 2));
- break;
- }
- case 2:
- {
- if (formedJagged) MassivPrint(strings, jagged);
- else Console.WriteLine("Рваный массив не создан");
- break;
- }
- case 3:
- {
- if (formedJagged) JaggedAddKStringsInTheEnd(ref strings, ref jagged);
- else Console.WriteLine("Рваный массив не создан");
- break;
- }
- case 0: break;
- default:
- {
- Console.WriteLine("Такого пункта нет в меню");
- break;
- }
- }
- } while (otv != 0);
- }
- static void Main(string[] args)
- {
- Console.WriteLine();
- int otv;
- do
- {
- PrintMenu();
- otv = NumberEntry();
- switch (otv)
- {
- case 1:
- {
- Massiv();
- break;
- }
- case 2:
- {
- Matr();
- break;
- }
- case 3:
- {
- Jagged();
- break;
- }
- case 0: break;
- default:
- {
- Console.WriteLine("Такого пункта нет в меню");
- break;
- }
- }
- } while (otv != 0);
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement