Здравствуйте, есть приложение, переводящее текст с русского на английский и обратно, нужно сделать так, чтобы можно было редактировать слова из словаря.
Сам словарь
dictionary.txt
C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication4
{
class Translator
{
Dictionary<string, string> dict = new Dictionary<string, string>();
public Translator(int direction) //конструктор, в котором заполняется словарь из файла
{
string[] dictionary = File.ReadAllLines(@"Dictionary.txt", Encoding.Default);
foreach (string line in dictionary)
{
string[] words = line.Split(' ');
if (direction == 1)
dict.Add(words[0], words[1]);
else
dict.Add(words[1], words[0]);
}
}
public string Translate(string text) //метод перевода
{
string[] textArray = text.Split(' ');
string result = string.Empty;
foreach (string word in textArray)
{
if (dict.ContainsKey(word))
result += dict[word] + " ";
else
result += "<слово отсутствует> "; //если нет в словаре
}
return result;
}
public static void addword()//добавить слово
{
Console.WriteLine("Введите английское слово и его перевод через пробел: ");
FileStream Dic = new FileStream(@"Dictionary.txt", FileMode.Append);
StreamWriter writer = new StreamWriter(Dic, Encoding.Default);
writer.WriteLine(Console.ReadLine());
writer.Close();
Console.WriteLine();
}
public static void slovar()//просмотр словаря
{
string[] Slovar = File.ReadAllLines(@"Dictionary.txt", Encoding.Default);
for (int i = 0; i < Slovar.Length; i++)
{
Console.WriteLine(Slovar[i]);
}
Console.WriteLine();
}
public static void Vvodrus()//перевод с русского
{
Console.WriteLine("Вводите текст на русском");
Translator ruen = new Translator(2);
string text2 = Console.ReadLine();
Console.WriteLine(ruen.Translate(text2));
Console.WriteLine();
}
public static void VvodEng()//перевод с английского
{
Console.WriteLine("Вводите текст на английском");
Translator enru = new Translator(1);
string text = Console.ReadLine();
Console.WriteLine(enru.Translate(text));
Console.WriteLine();
}
}
public class Support
{
static public void Menu()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Выберите нужный вариант и введите его номер");
Console.WriteLine("1) Перевод с английского на русский\n2) Перевод с русского на английский\n3) Добавить новое слово\n4) Просмотреть словарь\n5) Справка\n6) Выход\n\n");
Console.WriteLine();
}
static public void spravka()
{
Console.WriteLine("Данная программа создана для перевода слов и фраз с английского на русский и обратно.");
Console.WriteLine();
}
}
class Program
{
static void Main(string[] args)
{
Support.Menu();
uint n = 0;
while (n != 6)
{
do
{
try
{
n = uint.Parse(Console.ReadLine());
}
catch (Exception e)
{
Console.WriteLine(e.Message);
n = 0;
}
}
while (n == 0);
switch (n)
{
case 1:
Console.Clear();
Translator.VvodEng();
Support.Menu();
break;
case 2:
Console.Clear();
Translator.Vvodrus();
Support.Menu();
break;
case 3:
Console.Clear();
Translator.addword();
Support.Menu();
break;
case 4:
Console.Clear();
Translator.slovar();
Support.Menu();
break;
case 5:
Console.Clear();
Support.spravka();
Support.Menu();
break;
case 6:
Console.Clear();
Environment.Exit(0);
break;;
}
}
}
}
} |
|