IGRODELOFF

ExplanatoryDictionary

Feb 27th, 2026
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.28 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. class Program
  6. {
  7.     private const string ExitCommandEng = "exit";
  8.     private const string ExitCommandRus = "выход";
  9.  
  10.     private const string WarningMessageEmptyInput = "Пустой ввод. Попробуйте снова.";
  11.     private const string ErrorMessageWordNotFound = "Слово \"{0}\" не найдено в словаре.";
  12.     private const string Title = "ENG ⇄ RUS Dictionary";
  13.     private const string InfoMessageProgramEnded = "Программа завершена.";
  14.     private const string StartMessage = "Введите слово(или 'exit'/'выход' для выхода): ";
  15.     private const string BoarderLine = "======================================";
  16.     private const string HeaderTitle = "        ENG ⇄ RUS Dictionary          ";
  17.  
  18.     public static void Main()
  19.     {
  20.         Console.Clear();
  21.         Console.OutputEncoding = Encoding.UTF8;
  22.         Console.Title = Title;
  23.  
  24.         var dictionary = new Dictionary<string, string>
  25.         {
  26.             { "apple", "яблоко" },
  27.             { "book", "книга" },
  28.             { "car", "машина" },
  29.             { "house", "дом" },
  30.             { "computer", "компьютер" }
  31.         };
  32.  
  33.         PrintHeader();
  34.  
  35.         bool isRunning = true;
  36.  
  37.         while (isRunning == true)
  38.         {
  39.             Console.Write(StartMessage);
  40.  
  41.             string? input = Console.ReadLine();
  42.  
  43.             if (string.IsNullOrWhiteSpace(input))
  44.             {
  45.                 PrintWarning(WarningMessageEmptyInput);
  46.  
  47.                 continue;
  48.             }
  49.  
  50.             string trimmedInput = input.Trim();
  51.             string normalizedInput = trimmedInput.ToLower();
  52.  
  53.             if (normalizedInput == ExitCommandEng ||
  54.                 normalizedInput == ExitCommandRus)
  55.             {
  56.                 isRunning = false;
  57.                 PrintInfo(InfoMessageProgramEnded);
  58.                 continue;
  59.             }
  60.  
  61.             if (dictionary.TryGetValue(normalizedInput, out string russianTranslation))
  62.             {
  63.                 PrintSuccess($"{trimmedInput} → {russianTranslation}");
  64.                 continue;
  65.             }
  66.  
  67.             bool isFound = false;
  68.  
  69.             foreach (KeyValuePair<string, string> pair in dictionary)
  70.             {
  71.                 if (string.Equals(pair.Value, trimmedInput, StringComparison.OrdinalIgnoreCase))
  72.                 {
  73.                     PrintSuccess($"{trimmedInput} → {pair.Key}");
  74.                     isFound = true;
  75.  
  76.                     break;
  77.                 }
  78.             }
  79.  
  80.             if (isFound == false)
  81.             {
  82.                 PrintError(string.Format(ErrorMessageWordNotFound, trimmedInput));
  83.             }
  84.         }
  85.     }
  86.  
  87.     private static void PrintHeader()
  88.     {
  89.         Console.ForegroundColor = ConsoleColor.Cyan;
  90.         Console.WriteLine(BoarderLine);
  91.         Console.WriteLine(HeaderTitle);
  92.         Console.WriteLine(BoarderLine);
  93.         Console.ResetColor();
  94.         Console.WriteLine();
  95.     }
  96.  
  97.     private static void PrintSuccess(string message)
  98.     {
  99.         Console.ForegroundColor = ConsoleColor.Green;
  100.         Console.WriteLine($"✔ {message}");
  101.         Console.ResetColor();
  102.         Console.WriteLine();
  103.     }
  104.  
  105.     private static void PrintWarning(string message)
  106.     {
  107.         Console.ForegroundColor = ConsoleColor.Yellow;
  108.         Console.WriteLine($"! {message}");
  109.         Console.ResetColor();
  110.         Console.WriteLine();
  111.     }
  112.  
  113.     private static void PrintError(string message)
  114.     {
  115.         Console.ForegroundColor = ConsoleColor.Red;
  116.         Console.WriteLine($"✘ {message}");
  117.         Console.ResetColor();
  118.         Console.WriteLine();
  119.     }
  120.  
  121.     private static void PrintInfo(string message)
  122.     {
  123.         Console.ForegroundColor = ConsoleColor.Cyan;
  124.         Console.WriteLine(message);
  125.         Console.ResetColor();
  126.         Console.WriteLine();
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment