Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.50 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 Lesson_4
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string[,] names = new string[0, 3];
  14.             string[] stafs = new string[0];
  15.             bool exit = false;
  16.  
  17.             while (!exit)
  18.             {
  19.                 Console.Clear();
  20.  
  21.                 ShowMenu();
  22.                 Console.Write("Выберите действие: ");
  23.  
  24.                 ConsoleKeyInfo action = Console.ReadKey();
  25.                 Console.WriteLine();
  26.  
  27.                 switch (action.Key)
  28.                 {
  29.                     case ConsoleKey.D1:
  30.                         Add(ref names, ref stafs);
  31.  
  32.                         Console.WriteLine("Нажмите любую клавишу для продолжения...");
  33.                         Console.ReadKey();
  34.                         break;
  35.  
  36.                     case ConsoleKey.D2:
  37.                         Console.WriteLine();
  38.                         ShowAll(names, stafs);
  39.                         Console.WriteLine();
  40.  
  41.                         Console.WriteLine("Нажмите любую клавишу для продолжения...");
  42.                         Console.ReadKey();
  43.                         break;
  44.  
  45.                     case ConsoleKey.D3:
  46.                         Remove(ref names, ref stafs);
  47.  
  48.                         Console.WriteLine("Нажмите любую клавишу для продолжения...");
  49.                         Console.ReadKey();
  50.                         break;
  51.  
  52.                     case ConsoleKey.D4:
  53.                         Console.Write("Кого вы хотите найти: ");
  54.                         string surname = Console.ReadLine();
  55.                         Console.WriteLine();
  56.  
  57.                         int[] result = Search(names, stafs, surname);
  58.  
  59.                         Console.WriteLine($"По Вашему запрсу найдено {result.Length} результатов:");
  60.                         ShowInfo(names, stafs, result);
  61.  
  62.                         Console.WriteLine("Нажмите любую клавишу для продолжения...");
  63.                         Console.ReadKey();
  64.                         break;
  65.  
  66.                     case ConsoleKey.D0:
  67.                         exit = true;
  68.                         break;
  69.                 }
  70.             }
  71.         }
  72.  
  73.         static void ShowMenu()
  74.         {
  75.             Console.WriteLine("1. Добавить досье;\n2. Вывесть все досье;\n3. Удалить досье;\n4. Найти досье;\n0. Выход.");
  76.         }
  77.  
  78.         static int[] Search(string[,] names, string[] stafs, string surname)
  79.         {
  80.             int[] result = new int[0];
  81.             int count = 0;
  82.  
  83.             for (int i = 0; i < names.GetLength(0); i++)
  84.             {
  85.                 if (names[i, 0] == surname)
  86.                 {
  87.                     result = Expansion(result);
  88.                     result[count++] = i;
  89.                 }
  90.             }
  91.  
  92.             return result;
  93.         }
  94.  
  95.         static int[] Expansion(int[] array)
  96.         {
  97.             int[] temp = new int[array.Length + 1];
  98.  
  99.             for (int i = 0; i < array.Length; i++)
  100.             {
  101.                 temp[i] = array[i];
  102.             }
  103.  
  104.             return temp;
  105.         }
  106.  
  107.         static string[] Expansion(string[] array)
  108.         {
  109.             string[] temp = new string[array.Length + 1];
  110.  
  111.             for (int i = 0; i < array.Length; i++)
  112.             {
  113.                 temp[i] = array[i];
  114.             }
  115.  
  116.             return temp;
  117.         }
  118.  
  119.         static string[,] Expansion(string[,] array)
  120.         {
  121.             string[,] temp = new string[array.GetLength(0) + 1, array.GetLength(1)];
  122.  
  123.             for (int i = 0; i < array.GetLength(0); i++)
  124.             {
  125.                 for (int j = 0; j < array.GetLength(1); j++)
  126.                 {
  127.                     temp[i, j] = array[i, j];
  128.                 }
  129.             }
  130.  
  131.             return temp;
  132.         }
  133.  
  134.         static void Add(ref string[,] names, ref string[] stafs)
  135.         {
  136.             Console.Write("Введите имя: ");
  137.             string name = Console.ReadLine();
  138.             Console.Write("Введите фамилию: ");
  139.             string surname = Console.ReadLine();
  140.             Console.Write("Введите отчество: ");
  141.             string patronymic = Console.ReadLine();
  142.             Console.Write("Введите должность: ");
  143.             string staf = Console.ReadLine();
  144.  
  145.             names = Expansion(names);
  146.             stafs = Expansion(stafs);
  147.  
  148.             names[names.GetLength(0) - 1, 0] = surname;
  149.             names[names.GetLength(0) - 1, 1] = name;
  150.             names[names.GetLength(0) - 1, 2] = patronymic;
  151.  
  152.             stafs[stafs.Length - 1] = staf;
  153.  
  154.         }
  155.  
  156.         static void Remove(ref string[,] names, ref string[] stafs)
  157.         {
  158.             if (stafs.Length != 0)
  159.             {
  160.                 ShowAll(names, stafs);
  161.  
  162.                 Console.Write("Кого Вы хотите удилить: ");
  163.                 string surname = Console.ReadLine();
  164.  
  165.                 int[] result = Search(names, stafs, surname);
  166.  
  167.                 if (result.Length != 0)
  168.                 {
  169.                     string[,] tempNames = new string[names.GetLength(0) - result.Length, names.GetLength(1)];
  170.                     string[] tempStafs = new string[stafs.Length - result.Length];
  171.  
  172.                     int offset = 0;
  173.  
  174.                     for (int i = 0; i < tempNames.GetLength(0); i++)
  175.                     {
  176.                         if (Contains(result,i))
  177.                         {
  178.                             offset++;
  179.                         }
  180.                        
  181.                         for (int j = 0; j < tempNames.GetLength(1); j++)
  182.                         {
  183.                             tempNames[i, j] = names[i+offset, j];
  184.                         }
  185.  
  186.                         tempStafs[i] = stafs[i+offset];
  187.                     }
  188.  
  189.                     names = tempNames;
  190.                     stafs = tempStafs;
  191.                 }
  192.             }
  193.             else
  194.             {
  195.                 Console.WriteLine("Досье отсутствуют!");
  196.                 Console.ReadKey();
  197.             }
  198.         }
  199.  
  200.         static bool Contains(int[] array, int number)
  201.         {
  202.             for (int i = 0; i < array.Length; i++)
  203.             {
  204.                 if (array[i] == number)
  205.                 {
  206.                     return true;
  207.                 }
  208.             }
  209.  
  210.             return false;
  211.         }
  212.  
  213.         static void ShowAll(string[,] names, string[] stafs)
  214.         {
  215.             for (int i = 0; i < stafs.Length; i++)
  216.             {
  217.                 char ending = i == stafs.Length - 1 ? '.' : ';';
  218.                 Console.WriteLine($"{i + 1}. {names[i, 0]} {names[i, 1]} {names[i, 2]} - {stafs[i]}{ending}");
  219.             }
  220.         }
  221.  
  222.         static void ShowInfo(string[,] names, string[] stafs, int[] numbers)
  223.         {
  224.             if (names.Length != 0)
  225.             {
  226.                 for (int i = 0; i < numbers.Length; i++)
  227.                 {
  228.                     char ending = i == names.Length - 1 ? '.' : ';';
  229.                     Console.WriteLine($"{i + 1}. {names[numbers[i], 0]} {names[numbers[i], 1]} {names[numbers[i], 2]} - {stafs[numbers[i]]}{ending}");
  230.                 }
  231.             }
  232.         }
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement