Advertisement
illiden

Lexicon

Jul 1st, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5. namespace Lexicon
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Dictionary<string, string> words = LoadDictionaryFromFile("words.txt");
  12.             MainMenu(words);
  13.         }
  14.  
  15.         static void MainMenu(Dictionary<string, string> words)
  16.         {
  17.             bool work = true;
  18.             while (work)
  19.             {
  20.                 Console.Clear();
  21.                 Console.WriteLine("Введите команду: " +
  22.                     "\n1. Толкование слова" +
  23.                     "\n2. Вывести все слова" +
  24.                     "\n3. Вывести все слова с толкованием" +
  25.                     "\n4. Выход");
  26.  
  27.                 switch (Console.ReadLine())
  28.                 {
  29.                     case "1":
  30.                         Console.Write("\nВведите слово без пробелов: ");
  31.                         if (TrySearchWord(words, Console.ReadLine(), out string result))
  32.                         {
  33.                             Console.WriteLine("\n" + result);
  34.                         }
  35.                         else
  36.                         {
  37.                             Console.WriteLine("Такого слова не найдено");
  38.                         }
  39.                         break;
  40.                     case "2":
  41.                         foreach (string word in OutputAllWords(words))
  42.                         {
  43.                             Console.WriteLine(word);
  44.                         }
  45.                         break;
  46.                     case "3":
  47.                         List<string> allWords = OutputAllThesaurus(words);
  48.                         foreach (string word in allWords)
  49.                         {
  50.                             Console.WriteLine(word);
  51.                         }
  52.                         break;
  53.                     case "4":
  54.                         Console.WriteLine("Спасибо, что воспользовались нашим словарем!");
  55.                         work = false;
  56.                         break;
  57.                     default:
  58.                         Console.WriteLine("Такой команды не существует!");
  59.                         break;
  60.                 }
  61.  
  62.                 Console.ReadKey();
  63.             }
  64.         }
  65.  
  66.         static List<string> OutputAllWords(Dictionary<string, string> words)
  67.         {
  68.             List<string> outputWords = new List<string>();
  69.             foreach (string word in words.Keys)
  70.             {
  71.                 outputWords.Add(word);
  72.             }
  73.             return outputWords;
  74.         }
  75.  
  76.         static List<string> OutputAllThesaurus(Dictionary<string, string> words)
  77.         {
  78.             List<string> output = new List<string>();
  79.             foreach (var word in words)
  80.             {
  81.                 output.Add(word.Key + "-" + word.Value);
  82.             }
  83.             return output;
  84.         }
  85.  
  86.         static bool TrySearchWord(Dictionary<string, string> words, string word, out string result)
  87.         {
  88.             if (words.ContainsKey(word + " "))
  89.             {
  90.                 result = word + " -" + words[word + " "];
  91.                 return true;
  92.             }
  93.  
  94.             result = "";
  95.             return false;
  96.         }
  97.  
  98.         static Dictionary<string, string> LoadDictionaryFromFile(string fileName)
  99.         {
  100.             Dictionary<string, string> words = new Dictionary<string, string>();
  101.             string[] file = File.ReadAllLines(fileName);
  102.             foreach (string line in file)
  103.             {
  104.                 string[] word = line.Split(new char[] { '-' });
  105.                 words.Add(word[0].ToLower(), word[1].ToLower());
  106.             }
  107.  
  108.             return words;
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement