-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLAB(11)(+)_Dict.cpp
More file actions
312 lines (297 loc) · 7.96 KB
/
Copy pathLAB(11)(+)_Dict.cpp
File metadata and controls
312 lines (297 loc) · 7.96 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <Windows.h>
using namespace std;
struct translation
{
string english;
string russian;
};
bool sort_sorter(translation i, translation j){
return i.english < j.english;
}
int compare_en(string search_word, vector<translation> dictionary, int mid)
{
int m = 0, lenght;
if (dictionary[mid].english.size() > search_word.size())
{
lenght = search_word.size();
m = 2;
}
else if (search_word.size() > dictionary[mid].english.size())
{
lenght = dictionary[mid].english.size();
m = 1;
}
else lenght = search_word.size();
int stop = 0, direction = 0;
for (int i = 0; i < lenght; i++)
{
if (stop == 1) break;
if (int(search_word[i]) < int(dictionary[mid].english[i]))
{
direction = 1;
stop = 1;
}
if (int(dictionary[mid].english[i]) < int(search_word[i]))
{
direction = 2;
stop = 1;
}
}
if (direction != 0) return (direction);
else return (m);
}
int compare_ru(string search_word, vector<translation> dictionary, int mid)
{
int lenght1 = dictionary[mid].russian.size() - 1;
int lenght2 = search_word.size() - 1;
if (lenght1 = lenght2)
{
for (int i = 0; i < lenght1; i++)
{
if (search_word[i] != dictionary[mid].russian[i]) return -1;
}
return 0;
}
}
void search_word_en(vector<translation> dictionary)
{
string search_word;
cin >> search_word;
int left = 0, right = dictionary.size() - 1, mid = 0;
while (left <= right)
{
mid = left + (right - left) / 2;
if (compare_en(search_word, dictionary, mid) == 1) right = mid - 1;
else if (compare_en(search_word, dictionary, mid) == 2) left = mid + 1;
else if ((compare_en(search_word, dictionary, mid) == 0) && (dictionary[mid].english.size() == search_word.size()))
{
cout << "Найдено слово и его перевод" << endl << dictionary[mid].english << endl << dictionary[mid].russian << endl;
break;
}
}
}
void search_word_ru(vector<translation> dictionary)
{
string search_word;
cin >> search_word;
int right = dictionary.size() - 1, mid = 0;
for (; mid <= right;mid++)
{
if (0 == compare_ru(search_word, dictionary, mid))
{
cout << "Найдено слово и его перевод" << endl << dictionary[mid].russian << endl << dictionary[mid].english << endl;
break;
}
}
}
void dictionary_update(vector<translation> &dictionary, translation buffer)
{
int z;
next:
cout << "Вводите EN - RU, или пустую строку, если закончили: " << endl;
while ((!cin.eof()) && (cin.good()))
{
getline(cin, buffer.english);
if (buffer.english.size() > 0)
{
for (int i = 0; i < buffer.english.size(); i++)
{
if (int(buffer.english[i]) < 97) buffer.english[i] = int(buffer.english[i])+ 32;
}
for (int i = 0; i < dictionary.size(); i++)
{
if (dictionary[i].english == buffer.english)
{
cout << "Это слово уже есть в словаре!" << endl;
cout << "Добавить синоним? 1/0: ";
cin >> z;
cin.get();
if (z == 0) goto next;
}
}
}
if (buffer.english.size() == 0) break;
getline(cin, buffer.russian);
if (buffer.russian.size() > 0)
{
for (int i = 0; i < buffer.russian.size(); i++)
{
if (int(buffer.russian[i]) < -32) buffer.russian[i] = int(buffer.russian[i])+32;
}
for (int i = 0; i < dictionary.size(); i++)
{
if (dictionary[i].russian == buffer.russian)
{
cout << "Это слово уже есть в словаре!" << endl;
cout << "Добавить синоним? 1/0: ";
cin >> z;
cin.get();
if (z == 0) goto next;
}
}
}
dictionary.push_back(buffer);
};
sort(dictionary.begin(), dictionary.end(), sort_sorter);
ofstream dict1("dict.txt", ofstream::out | ofstream::binary | ofstream::trunc);
for (int i = 0; i < dictionary.size(); i++)
{
dict1 << dictionary[i].english;
dict1 << endl;
dict1 << dictionary[i].russian;
if (i + 1 == dictionary.size()) break;
dict1 << endl;
}
for (int i = 0; i < dictionary.size(); i++)
{
cout << dictionary[i].english;
cout << " - ";
cout << dictionary[i].russian;
//if (i + 1 == dictionary.size()) break;
cout << endl;
}
dict1.close();
}
void delete_en(vector<translation> &dictionary)
{
string search_word;
cin >> search_word;
int right = dictionary.size() - 1, mid = 0;
for (; mid <= right; mid++)
{
if ((compare_en(search_word, dictionary, mid) == 0) && (dictionary[mid].english.size() == search_word.size())) break;
}
dictionary.erase(dictionary.begin() + mid);
sort(dictionary.begin(), dictionary.end(), sort_sorter);
ofstream dict1("dict.txt", ofstream::out | ofstream::binary | ofstream::trunc);
for (int i = 0; i < dictionary.size(); i++)
{
dict1 << dictionary[i].english;
dict1 << endl;
dict1 << dictionary[i].russian;
if (i + 1 == dictionary.size()) break;
dict1 << endl;
}
dict1.close();
cout << "Слово удалено!" << endl;
}
void delete_ru(vector<translation> &dictionary)
{
string search_word;
cin >> search_word;
int right = dictionary.size() - 1, mid = 0;
for (; mid <= right; mid++)
{
if (0 == compare_ru(search_word, dictionary, mid)) break;
}
dictionary.erase(dictionary.begin() + mid);
sort(dictionary.begin(), dictionary.end(), sort_sorter);
ofstream dict1("dict.txt", ofstream::out | ofstream::binary | ofstream::trunc);
for (int i = 0; i < dictionary.size(); i++)
{
dict1 << dictionary[i].english;
dict1 << endl;
dict1 << dictionary[i].russian;
if (i + 1 == dictionary.size()) break;
dict1 << endl;
}
dict1.close();
cout << "Слово удалено!" << endl;
}
int main()
{
setlocale(LC_ALL, "Russian");
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
vector<translation> dictionary;
translation buffer;
ifstream dict("dict.txt", ifstream::in | ifstream::binary);
while ((!dict.eof()) && (dict.good()))
{
getline(dict, buffer.english);
getline(dict, buffer.russian);
dictionary.push_back(buffer);
};
dict.close();
sort(dictionary.begin(), dictionary.end(), sort_sorter);
ofstream dict1("dict.txt", ofstream::out | ofstream::binary | ofstream::trunc);
for (int i = 0; i < dictionary.size(); i++)
{
dict1 << dictionary[i].english;
dict1 << endl;
dict1 << dictionary[i].russian;
if (i + 1 == dictionary.size()) break;
dict1 << endl;
}
dict1.close();
int n = 0;
while (true)
{
while (n == 0){
cout << "Что вы хотите сделать?\n" << endl;
cout << "0. Показать меню\n"
<< "1. Добавить слова\n"
<< "2. Найти слово на английском\n"
<< "3. Найти слово на русском\n"
<< "4. Показать словарь\n"
<< "5. Удалить слово EN\n"
<< "6. Удалить слово RU\n"
<< "7. выйти\n" << endl;
cin >> n;
}
while (n == 1){
cin.get();
cout << "Добавьте слово и его перевод EN - RU: " << endl;
dictionary_update(dictionary, buffer);
cin >> n;
}
while (n == 2){
cout << "Введите слово на английском:" << endl;
cin.get();
search_word_en(dictionary);
cin.get();
cin >> n;
}
while (n == 3){
cout << "Введите слово на русском:" << endl;
cin.get();
search_word_ru(dictionary);
cin.get();
cin >> n;
}
while (n == 4){
cout << "Вывод словаря на экран..." << endl;
for (int i = 0; i < dictionary.size(); i++)
{
cout << dictionary[i].english;
cout << " - ";
cout << dictionary[i].russian;
//if (i + 1 == dictionary.size()) break;
cout << endl;
}
cin >> n;
}
while (n == 5){
cout << "Введите слово на Английском: " << endl;
delete_en(dictionary);
cin.get();
cin >> n;
}
if (n == 6){
cout << "Введите слово на Русском: " << endl;
delete_ru(dictionary);
cin.get();
cin >> n;
}
while ((n > 6) || (n < 0)) {
cout << "Вы ввели неправильное число" << endl;
cin >> n;
}
}
return 0;
}