Advertisement
Guest User

Full code

a guest
Oct 22nd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.12 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace Ekz1
  5. {
  6.     class Program
  7.     {
  8.         static Random rand = new Random();
  9.         static void CountEach(ref char[] arr)
  10.         {
  11.             int i = 1;
  12.             int numb = 1, j = 0, last = 0;
  13.             char[] str = new char[arr.Length];
  14.             Array.Copy(arr, str, arr.Length);
  15.             Array.Sort(str);
  16.             char letter = str[0];
  17.             for (i = 0; i < str.Length; ++i)
  18.             {
  19.                 if (str[i] != letter)
  20.                 {
  21.                     letter = str[i];
  22.                     ++numb;
  23.                 }
  24.             }
  25.             char[] letters = new char[numb];
  26.             int[] count = new int[numb];
  27.             letter = str[0];
  28.             Console.WriteLine($"Количество встреч каждой буквы:");
  29.             for (i = 0; i < str.Length; ++i)
  30.             {
  31.                 if (str[i] != letter)
  32.                 {
  33.                     letter = str[i];
  34.                     letters[j + 1] = str[i];
  35.                     count[j++] = i - last;
  36.                     last = i;
  37.                     Console.Write($"{str[i]} встречено {count[j - 1]}");
  38.                     if (count[j - 1] % 10 > 1 && count[j - 1] % 10 < 5)
  39.                     {
  40.                         if (count[j - 1] > 10 && count[j - 1] < 20)
  41.                         {
  42.                             Console.WriteLine(" раз.");
  43.                         } else
  44.                         {
  45.                             Console.WriteLine(" раза.");
  46.                         }
  47.                     } else
  48.                     {
  49.                         Console.WriteLine(" раз.");
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.         /*
  55.          b встречено 354 раза.
  56. c встречено 369 раза.
  57. d встречено 368 раза.
  58. e встречено 391 раза.
  59. f встречено 375 раза.
  60. g встречено 389 раза.
  61. h встречено 406 раза.
  62. i встречено 394 раза.
  63. j встречено 390 раза.
  64. k встречено 374 раза.
  65. l встречено 404 раза.
  66. m встречено 393 раза.
  67. n встречено 387 раза.
  68. o встречено 390 раза.
  69. p встречено 385 раза.
  70. q встречено 382 раза.
  71. r встречено 385 раза.
  72. s встречено 375 раза.
  73. t встречено 423 раза.
  74. u встречено 363 раза.
  75. v встречено 390 раза.
  76. w встречено 369 раза.
  77. x встречено 366 раза.
  78. y встречено 367 раза.*/
  79.         static void ChInterval(char ch1, char ch2, ref char[] arr)
  80.         {
  81.             Console.Write($"Выбрано от {ch1} до {ch2}: ");
  82.             try
  83.             {
  84.                 for (int i = 0; i < arr.Length; ++i)
  85.                 {
  86.                     if (arr[i] > ch1 && arr[i] < ch2)
  87.                     {
  88.                         Console.Write($"{arr[i]} ");
  89.                     }
  90.                 }
  91.             }
  92.             catch (Exception ex)
  93.             {
  94.                 Console.WriteLine(ex.Message);
  95.             }
  96.         }
  97.         static void GoodForK(int k, ref char[] arr)
  98.         {
  99.             try
  100.             {
  101.                 Console.WriteLine();
  102.                 Console.Write($"Кратные {k} элементы: ");
  103.                 for (int i = 0; i < arr.Length; ++i)
  104.                 {
  105.                     if ((arr[i] - 97) % k == 0)
  106.                     {
  107.                         Console.Write($"{arr[i]} ");
  108.                     }
  109.                 }
  110.                 Console.WriteLine();
  111.             }
  112.             catch (Exception ex)
  113.             {
  114.                 Console.WriteLine(ex.Message);
  115.             }
  116.         }
  117.         static void GenerateArray(ref char[] arr)
  118.         {
  119.             Console.Write($"Сгенерированный массив длины {arr.Length}: ");
  120.             for (int i = 0; i < arr.Length; ++i)
  121.             {
  122.                 arr[i] = (char)rand.Next(97, 123);
  123.                 Console.Write($"{arr[i]} ");
  124.             }
  125.             Console.WriteLine();
  126.         }
  127.         static void MostFrequent(char[] arr)
  128.         {
  129.             try
  130.             {
  131.                 int i = 1;
  132.                 int numb = 1, res = 1, j = 0, last = 0;
  133.                 char[] str = new char[arr.Length];
  134.                 Array.Copy(arr, str, arr.Length);
  135.                 Array.Sort(str);
  136.                 Console.Write($"Sorted: ");
  137.                 Console.WriteLine(str);
  138.                 char letter = str[0];
  139.                 for (i = 0; i < str.Length; ++i)
  140.                 {
  141.                     if (str[i] != letter)
  142.                     {
  143.                         letter = str[i];
  144.                         ++numb;
  145.                     }
  146.                 }
  147.                 char[] letters = new char[numb];
  148.                 int[] count = new int[numb];
  149.                 letter = str[0];
  150.                 for (i = 0; i < str.Length; ++i)
  151.                 {
  152.                     if (str[i] != letter)
  153.                     {
  154.                         letter = str[i];
  155.                         letters[j + 1] = str[i];
  156.                         count[j++] = i - last;
  157.                         last = i;
  158.                     }
  159.                 }
  160.                 count[j] = i - last;
  161.                 for (i = 0; i < count.Length; ++i)
  162.                 {
  163.                     if (count[i] > res)
  164.                     {
  165.                         res = count[i];
  166.                     }
  167.                 }
  168.                 for (i = 0; i < count.Length; ++i)
  169.                 {
  170.                     if (count[i] == res)
  171.                     {
  172.                         ++numb;
  173.                     }
  174.                 }
  175.                 numb = 0;
  176.                 bool next = false;
  177.                 for (i = 0; i < count.Length; ++i)
  178.                 {
  179.                     if (count[i] == res)
  180.                     {
  181.                         if (numb > 1)
  182.                         {
  183.                             if (next == true)
  184.                             {
  185.                                 Console.Write($", {letters[i + 1]}");
  186.                             }
  187.                             else
  188.                             {
  189.                                 Console.Write($"Чаще всего встречаются {letters[i + 1]}");
  190.                                 next = true;
  191.                             }
  192.                         }
  193.                         else
  194.                         {
  195.                             Console.Write($"Чаще всего встречается {letters[i + 1]}");
  196.                         }
  197.                     }
  198.                 }
  199.                 Console.Write(".");
  200.             }
  201.             catch (Exception ex)
  202.             {
  203.                 Console.WriteLine(ex.Message);
  204.             }
  205.         }
  206.         static void Main(string[] args)
  207.         {
  208.             try
  209.             {
  210.                 int n, k;
  211.                 char ch1, ch2;
  212.                 do
  213.                 {
  214.                     Console.Write("Введите n: ");
  215.                     while (!int.TryParse(Console.ReadLine(), out n) || n < 1)
  216.                     {
  217.                         Console.WriteLine("Некорректный ввод! Повторите попытку.");
  218.                         Console.Write("Введите n: ");
  219.                     }
  220.                     Console.Write("Введите k: ");
  221.                     while (!int.TryParse(Console.ReadLine(), out k))
  222.                     {
  223.                         Console.WriteLine("Некорректный ввод! Повторите попытку.");
  224.                         Console.Write("Введите k: ");
  225.                     }
  226.                     Console.Write("Введите ch1: ");
  227.                     while (!char.TryParse(Console.ReadLine(), out ch1) || ch1 < 97 || ch1 >= 122)
  228.                     {
  229.                         Console.WriteLine("Некорректный ввод! Повторите попытку.");
  230.                         Console.Write("Введите ch1: ");
  231.                     }
  232.                     Console.Write("Введите ch2: ");
  233.                     while (!char.TryParse(Console.ReadLine(), out ch2) || ch2 - ch1 - 1 <= 0 || ch2 <= 97 || ch2 > 122)
  234.                     {
  235.                         Console.WriteLine("Некорректный ввод! Повторите попытку.");
  236.                         Console.Write("Введите ch2: ");
  237.                     }
  238.                     char[] arr = new char[n];
  239.                     GenerateArray(ref arr);
  240.                     MostFrequent(arr);
  241.                     GoodForK(k, ref arr);
  242.                     CountEach(ref arr);
  243.                     ChInterval(ch1, ch2, ref arr);
  244.                     Console.WriteLine();
  245.                     Console.WriteLine($"Нажмите Escape, чтобы выйти.");
  246.                 } while (Console.ReadKey(intercept: true).Key != ConsoleKey.Escape);
  247.             }
  248.             catch (Exception ex)
  249.             {
  250.                 Console.WriteLine(ex.Message);
  251.             }
  252.         }
  253.     }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement