-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGermanToEnglishDictionary.cpp
More file actions
300 lines (248 loc) · 7 KB
/
Copy pathGermanToEnglishDictionary.cpp
File metadata and controls
300 lines (248 loc) · 7 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
// German to English Dictionary
// Michael Feuerstein
// 10/10/2015
#include "GermanToEnglishDictionary.h"
void main()
{
const int SIZE = 5;
int choice, lSize = 0;
bool fileExists = false;
GermanToEnglishWord dictionary[SIZE];
const char * file = "dictionary.dat";
char temp;
if (_access(file, 00) == 0){
loadDictionary(dictionary, SIZE, file);
fileExists = true;
}
while (true)
{
printMenu();
cin >> choice; cin.ignore(80, '\n');
if (choice == 1 && fileExists)
cout << "\n\t\t Error. Dictionary file already exists.\n";
else if (choice > 1 && !fileExists)
cout << "\n\t\t Error. Dictionary file does not exist. Please create a new list.\n";
else
{
system("cls");
switch (choice)
{
case 1: fileExists = loadGtEWordsArray(dictionary, SIZE); sortDictionary(dictionary, SIZE); saveDictionary(dictionary, SIZE, file); break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7: printDictionary(dictionary, SIZE, choice - 1); break;
case 8: lookUpDefinition(dictionary, SIZE); break;
case 9: cout << "\n\n\n\n\n\t\t Exit program? (y/n): ";
temp = _getch();
if (temp == 'Y' || temp == 'y')
exit(0);
break;
default: cout << "Incorrect. Please enter a choice(1 - 9): ";
}
}
cout << "\n\nPress any key to get back to menu...";
system("pause>nul");
system("cls");
}
_getch();
}
void printMenu()
{
cout << "\n\n\n\n"
<< "\t\t 1. Create the word list\n"
<< "\t\t 2. German to English nouns\n"
<< "\t\t 3. German to English verbs\n"
<< "\t\t 4. German to English prepositions\n"
<< "\t\t 5. German to English adjectives\n"
<< "\t\t 6. German to English adverbs\n"
<< "\t\t 7. German to English cardinal numbers\n"
<< "\t\t 8. Search for a word\n"
<< "\t\t 9. Quit\n"
<< "\n\t\t Enter your choice(1 - 9): ";
}
bool loadGtEWordsArray(GermanToEnglishWord dict[], int size)
{
int lSize;
for (lSize = 0; lSize < size; lSize++){
dict[lSize] = getGtEW();
system("cls");
}
return true;
}
GermanToEnglishWord getGtEW(){
int wordType;
string eng, ger;
GermanToEnglishWord word;
cout << "\n\n\n\n"
<< "\t\t 1. Nouns\n"
<< "\t\t 2. Verbs\n"
<< "\t\t 3. Prepositions\n"
<< "\t\t 4. Adjectives\n"
<< "\t\t 5. Adverbs\n"
<< "\t\t 6. Cardinal numbers\n\n"
<< "\t\t Enter part of speech(1 - 6): ";
cin >> wordType; cin.ignore(80, '\n');
cout << "\n\t\tF1 : " << char(132) << ", F2 : " << char(142) << ", F3 : " << char(148) << ", F4 : " << char(153) << ", F5 : " << char(129) << ", F6 : " << char(154);
cout << "\n\n\t\t Enter German word: ";
ger = getGermanWord();
cout << "\n\n\t\t Enter English translation: ";
getline(cin, eng);
word.wordType = wordType;
word.germanTrans = ger;
word.englishTrans = eng;
return word;
}
string getGermanWord()
{
unsigned char key1, key2, letter;
string word = "";
while ((key1 = _getch()) != '\r')
{
if (key1 == 0)
{
key2 = _getch();
switch (int(key2))
{
case 59: letter = char(132); break;
case 60: letter = char(142); break;
case 61: letter = char(148); break;
case 62: letter = char(153); break;
case 63: letter = char(129); break;
case 64: letter = char(154); break;
}
word += letter;
cout << letter;
}
else
{
switch (key1)
{
// enables backspacing for german word entry - array keys not accounted for
case '\b': if (word != "")
{
cout << '\b';// << char(255) << '\b';
word.erase(word.end() - 1);
}break;
default: letter = key1;
cout << letter;
word += letter;
}
}
}
return word;
}
void sortDictionary(GermanToEnglishWord dict[], int size)
{
GermanToEnglishWord temp;
for (int i = 0; i < size - 1; i++){
for (int j = i + 1; j < size; j++)
{
if (cleanGermanWord(dict[i].germanTrans, dict[i].wordType) >
cleanGermanWord(dict[j].germanTrans, dict[j].wordType) )
{
temp = dict[i];
dict[i] = dict[j];
dict[j] = temp;
}
}
}
}
void saveDictionary(GermanToEnglishWord dict[], int size, const char *file)
{
ofstream fout(file, ios_base::out | ios_base::app | ios_base::binary);
for (int i = 0; i < size; i++)
{
fout.write( (char *)&dict[i], sizeof dict[i] );
}
fout.close();
}
void loadDictionary(GermanToEnglishWord dict[], int size, const char *file)
{
ifstream fin(file, ios_base::in | ios_base::binary);
for (int i = 0; i < size; i++)
{
fin.read((char *)&dict[i], sizeof dict[i]);
}
fin.close();
}
string cleanGermanWord(string word, int type){
if (type == 1)
{
word = ignoreArticle(word);
}
return exchangeGermanLettering(word);
}
string ignoreArticle(string word)
{
return word.substr(4, word.length());
}
string exchangeGermanLettering(string word){
for (int i = 0; i < word.length(); i++)
{
switch (int(word[i]))
{
case 132: word[i] = 'a'; break;
case 142: word[i] = 'A'; break;
case 148: word[i] = 'o'; break;
case 153: word[i] = 'O'; break;
case 129: word[i] = 'u'; break;
case 154: word[i] = 'U'; break;
}
}
return word;
}
void lookUpDefinition(GermanToEnglishWord dict[], int size){
string word = "";
int index;
cout << "\n\n\n\n\t\t Enter a word: ";
getline(cin, word);
index = getWordIndex(dict, size, word);
if (index == -1)
cout << "\n\t\t Definition not found.";
else
printDefinition(dict, index);
}
int getWordIndex(GermanToEnglishWord dict[], int size, string word){
for (int i = 0; i < size; i++)
{
if (dict[i].wordType == 1)
if (word == ignoreArticle(dict[i].germanTrans))
return i;
if (word == dict[i].germanTrans)
return i;
}
return -1;
}
void printDictionary(GermanToEnglishWord dict[], int size, int type)
{
system("cls");
cout << right
<< "\n\n" << setw(21) << "German Word" << setw(25) << "Part of speech" << setw(25) << "English meaning" << "\n"
<< setw(21) << "-----------" << setw(25) << " --------------" << setw(25) << "---------------" << "\n\n";
for (int i = 0; i < size; i++)
{
if (dict[i].wordType == type || type == 0)
cout << addSpaces(10) << dict[i].germanTrans << addSpaces(22 - dict[i].germanTrans.length()) << wordTypes[dict[i].wordType - 1]
<< addSpaces(24 - wordTypes[dict[i].wordType - 1].length()) << dict[i].englishTrans << "\n";
}
}
void printDefinition(GermanToEnglishWord dict[], int i)
{
cout << right
<< "\n\n" << setw(21) << "German Word" << setw(25) << "Part of speech" << setw(25) << "English meaning" << "\n"
<< setw(21) << "-----------" << setw(25) << " --------------" << setw(25) << "---------------" << "\n\n";
cout << addSpaces(10) << dict[i].germanTrans << addSpaces(22 - dict[i].germanTrans.length()) << wordTypes[dict[i].wordType - 1]
<< addSpaces(24 - wordTypes[dict[i].wordType - 1].length()) << dict[i].englishTrans << "\n";
}
string addSpaces(int numSpaces)
{
string spaces = "";
for (int i = 0; i < numSpaces; i++)
{
spaces += " ";
}
return spaces;
}