IGRODELOFF

ExplanatoryDictionary_v2

Mar 3rd, 2026
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.15 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. class Program
  6. {
  7.     public static void Main()
  8.     {
  9.         Work();
  10.     }
  11.  
  12.     private static void Work()
  13.     {
  14.         Console.Clear();
  15.         Console.OutputEncoding = Encoding.UTF8;
  16.         Console.Title = GetText("Title");
  17.  
  18.         Dictionary<string, string> dictionary = CreateDictionary();
  19.  
  20.         PrintHeader();
  21.  
  22.         bool isRunning = true;
  23.  
  24.         while (isRunning == true)
  25.         {
  26.             string input = RequestInput();
  27.  
  28.             if (IsExitCommand(input) == true)
  29.             {
  30.                 ShowInfo(GetText("ProgramEnded"));
  31.                 isRunning = false;
  32.             }
  33.             else
  34.             {
  35.                 ProcessQuery(dictionary, input);
  36.             }
  37.         }
  38.     }
  39.  
  40.     private static Dictionary<string, string> CreateDictionary()
  41.     {
  42.         Dictionary<string, string> dictionary = new Dictionary<string, string>();
  43.  
  44.         dictionary.Add("apple", "яблоко");
  45.         dictionary.Add("book", "книга");
  46.         dictionary.Add("car", "машина");
  47.         dictionary.Add("house", "дом");
  48.         dictionary.Add("computer", "компьютер");
  49.  
  50.         return dictionary;
  51.     }
  52.  
  53.     private static string RequestInput()
  54.     {
  55.         string? input;
  56.         bool isValid;
  57.  
  58.         do
  59.         {
  60.             Console.Write(GetText("InputPrompt"));
  61.             input = Console.ReadLine();
  62.  
  63.             isValid = !string.IsNullOrWhiteSpace(input);
  64.  
  65.             if (isValid == false)
  66.             {
  67.                 ShowWarning(GetText("EmptyInput"));
  68.                 Console.WriteLine();
  69.             }
  70.  
  71.         } while (isValid == false);
  72.  
  73.         return input!.Trim();
  74.     }
  75.  
  76.     private static void ProcessQuery(Dictionary<string, string> dictionary, string input)
  77.     {
  78.         bool found = TryFindTranslation(dictionary, input, out string result);
  79.  
  80.         if (found == true)
  81.         {
  82.             ShowSuccess(result);
  83.         }
  84.         else
  85.         {
  86.             string message = string.Format(GetText("WordNotFound"), input);
  87.             ShowError(message);
  88.         }
  89.  
  90.         Console.WriteLine();
  91.     }
  92.  
  93.     private static bool TryFindTranslation(
  94.         Dictionary<string, string> dictionary,
  95.         string input,
  96.         out string result)
  97.     {
  98.         string normalized = input.ToLower();
  99.  
  100.         bool foundByKey = dictionary.TryGetValue(normalized, out string russianWord);
  101.  
  102.         if (foundByKey == true)
  103.         {
  104.             result = input + " → " + russianWord;
  105.             return true;
  106.         }
  107.  
  108.         foreach (KeyValuePair<string, string> pair in dictionary)
  109.         {
  110.             bool equals = string.Equals(pair.Value, input, StringComparison.OrdinalIgnoreCase);
  111.  
  112.             if (equals == true)
  113.             {
  114.                 result = input + " → " + pair.Key;
  115.                 return true;
  116.             }
  117.         }
  118.  
  119.         result = string.Empty;
  120.         return false;
  121.     }
  122.  
  123.     private static bool IsExitCommand(string input)
  124.     {
  125.         string normalized = input.ToLower();
  126.  
  127.         bool isExitEng = normalized == "exit";
  128.         bool isExitRus = normalized == "выход";
  129.  
  130.         return isExitEng || isExitRus;
  131.     }
  132.  
  133.     private static void PrintHeader()
  134.     {
  135.         ShowHeader(GetText("Border"));
  136.         ShowHeader(GetText("Header"));
  137.         ShowHeader(GetText("Border"));
  138.         Console.WriteLine();
  139.     }
  140.  
  141.     private static void Display(string message, ConsoleColor color)
  142.     {
  143.         Console.ForegroundColor = color;
  144.         Console.WriteLine(message);
  145.         Console.ResetColor();
  146.     }
  147.  
  148.     private static void ShowHeader(string message)
  149.     {
  150.         Display(message, ConsoleColor.Cyan);
  151.     }
  152.  
  153.     private static void ShowSuccess(string message)
  154.     {
  155.         Display("✔ " + message, ConsoleColor.Green);
  156.     }
  157.  
  158.     private static void ShowError(string message)
  159.     {
  160.         Display("✘ " + message, ConsoleColor.Red);
  161.     }
  162.  
  163.     private static void ShowWarning(string message)
  164.     {
  165.         Display("! " + message, ConsoleColor.Yellow);
  166.     }
  167.  
  168.     private static void ShowInfo(string message)
  169.     {
  170.         Display(message, ConsoleColor.Cyan);
  171.     }
  172.  
  173.     private static string GetText(string key)
  174.     {
  175.         if (key == "Title")
  176.             return "ENG ⇄ RUS Dictionary";
  177.  
  178.         if (key == "InputPrompt")
  179.             return "Введите слово (или 'exit'/'выход' для выхода): ";
  180.  
  181.         if (key == "EmptyInput")
  182.             return "Пустой ввод. Попробуйте снова.";
  183.  
  184.         if (key == "WordNotFound")
  185.             return "Слово \"{0}\" не найдено в словаре.";
  186.  
  187.         if (key == "ProgramEnded")
  188.             return "Программа завершена.";
  189.  
  190.         if (key == "Border")
  191.             return "======================================";
  192.  
  193.         if (key == "Header")
  194.             return "        ENG ⇄ RUS Dictionary          ";
  195.  
  196.         return string.Empty;
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment