This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpreters.cpp
308 lines (253 loc) · 7.6 KB
/
Interpreters.cpp
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
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<stdio.h>
#include<cstdlib>
#include<string>
using namespace std;
#pragma region Structures
struct Interpreter {
char Id[10];
char Name[20];
};
struct Language {
char Id[10];
char Name[20];
};
struct InterpreterLanguage {
char InterpreterId[10];
char LanguageId[10];
int LevelOfKnowledge;
};
#pragma endregion
int interpreters_count = 0,
languages_count = 0,
interpretersLanguages_count = 0;
Interpreter * interpreters;
Language * languages;
InterpreterLanguage * interpretersLanguages;
const char * storage_table_interpreters = "INTERPRETERS";
const char * storage_table_languages = "LANGUAGES";
const char * storage_table_interpretersLanguages = "INTERPRETERSLANGUAGES";
#pragma region Functions prototypes
template <typename T>
T * add_record(T * data, const char * table_name);
template <typename T>
T * deserialize_data(const char * name, int & counter);
void add_interpreter_diaglogue_window();
void add_language_diaglogue_window();
void add_interpreterlanguage_diaglogue_window();
void show_all_languages_of_interpreter_diaglogue_window();
void show_info_about_language_interpreters_diaglogue_window();
void prepare_program();
char * get_name_by_id(string interpreter_id);
char * get_id_by_language(string language_name);
char * get_id_by_name(string interpreter_name);
bool main_menu();
void display_interpreters_n_languages();
#pragma endregion
int main() {
bool check = true;
while (check)
{
check = main_menu();
}
system("pause");
return 0;
}
#pragma region Functions implementations
template <typename T>
T * add_record(T * data, const char * table_name) {
FILE * fp = fopen(table_name, "ab");
int size = sizeof(T);
char * binary_data;
if (fp != NULL) {
binary_data = (char *)data;
for (int i = 0; i < size; i++) {
putc(*binary_data++, fp);
}
fclose(fp);
}
return data;
}
template <typename T>
T * deserialize_data(const char * name, int & counter)
{
FILE * fp = fopen(name, "rb");
char * binary_data;
int iterator = 0;
int inner_counter = 0;
T * ptr = (T *)malloc(sizeof(T) * 100);
if (ptr != NULL)
{
binary_data = (char *)ptr;
while ((iterator = getc(fp)) != EOF)
{
*binary_data = iterator;
binary_data++;
inner_counter++;
if (inner_counter == sizeof(T)) {
counter++;
inner_counter = 0;
}
}
}
fclose(fp);
return ptr;
}
void add_interpreter_diaglogue_window() {
Interpreter * record = new Interpreter;
cout << "Please enter an interpreter ID:...[10 symbols]:" << endl;
cin >> record->Id;
cout << "Please enter an interpreter name:...[20 symbols]:" << endl;
cin >> record->Name;
add_record<Interpreter>(record, storage_table_interpreters);
}
void add_language_diaglogue_window() {
Language * record = new Language;
cout << "Please enter a language ID:...[10 symbols]:" << endl;
cin >> record->Id;
cout << "Please enter a language name:...[20 symbols]:" << endl;
cin >> record->Name;
add_record<Language>(record, storage_table_languages);
}
void add_interpreterlanguage_diaglogue_window() {
InterpreterLanguage * record = new InterpreterLanguage;
display_interpreters_n_languages();
cout << "Please enter an interpreter ID:...[10 symbols]:" << endl;
cin >> record->InterpreterId;
cout << "Please enter a language ID:...[10 symbols]:" << endl;
cin >> record->LanguageId;
cout << "Please enter a language knowledge level:..." << endl;
cin >> record->LevelOfKnowledge;
add_record<InterpreterLanguage>(record, storage_table_interpretersLanguages);
}
void show_all_languages_of_interpreter_diaglogue_window() {
prepare_program();
cout << "Enter the interpreters name: ";
string interpreter_name;
cin >> interpreter_name;
for (int i = 0; i < interpretersLanguages_count; i++) {
string first(interpretersLanguages->InterpreterId);
string second = get_id_by_name(interpreter_name);
if (first == second) {
cout << "LANGUAGE: " << interpretersLanguages->LanguageId <<
" | LEVEL: " << interpretersLanguages->LevelOfKnowledge << endl;
}
interpretersLanguages++;
}
}
void show_info_about_language_interpreters_diaglogue_window() {
prepare_program();
cout << "Enter the language name [English, Russian, Kazakh]: ";
string language_name;
cin >> language_name;
for (int i = 0; i < interpretersLanguages_count; i++) {
string first(interpretersLanguages->LanguageId);
string second = get_id_by_language(language_name);
string interpreters_name = get_name_by_id(interpretersLanguages->InterpreterId);
if (first == second) {
cout << "NAME: " << interpreters_name << " | LEVEL: " << interpretersLanguages->LevelOfKnowledge << endl;
}
interpretersLanguages++;
}
}
void prepare_program() {
interpreters = deserialize_data<Interpreter>(storage_table_interpreters, interpreters_count);
languages = deserialize_data<Language>(storage_table_languages, languages_count);
interpretersLanguages = deserialize_data<InterpreterLanguage>(storage_table_interpretersLanguages, interpretersLanguages_count);
}
char * get_name_by_id(string interpreter_id)
{
interpreters = deserialize_data<Interpreter>(storage_table_interpreters, interpreters_count);
for (int i = 0; i < interpreters_count; i++)
{
string first = interpreters->Id;
string second = interpreter_id;
if (first == second)
{
return interpreters->Name;
}
interpreters++;
}
}
char * get_id_by_language(string language_name)
{
languages = deserialize_data<Language>(storage_table_languages, languages_count);
for (int i = 0; i < languages_count; i++)
{
string first = languages->Name;
string second = language_name;
if (first == second)
{
return languages->Id;
}
languages++;
}
}
char * get_id_by_name(string interpreter_name)
{
interpreters = deserialize_data<Interpreter>(storage_table_interpreters, interpreters_count);
for (int i = 0; i < languages_count; i++)
{
string first = interpreters->Name;
string second = interpreter_name;
if (first == second)
{
return interpreters->Id;
}
interpreters++;
}
}
void display_interpreters_n_languages()
{
interpreters = deserialize_data<Interpreter>(storage_table_interpreters, interpreters_count);
languages = deserialize_data<Language>(storage_table_languages, languages_count);
for (int i = 0; i < interpreters_count; i++)
{
cout << "ID: " << interpreters->Id << " | NAME: " << interpreters->Name << endl;
interpreters++;
}
for (int i = 0; i < languages_count; i++)
{
cout << "ID: " << languages->Id << " | NAME: " << languages->Name << endl;
languages++;
}
interpreters_count = 0;
languages_count = 0;
}
bool main_menu()
{
cout << "1 - Add an interpreter" << endl;
cout << "2 - Add a language" << endl;
cout << "3 - Add an info to interpreters and languages" << endl;
cout << "4 - Show info about interpreters languages knowledge" << endl;
cout << "5 - Show information about language interpreters" << endl;
cout << "0 - EXIT" << endl;
int command;
cin >> command;
switch (command)
{
case 0:
return false;
case 1:
add_interpreter_diaglogue_window();
break;
case 2:
add_language_diaglogue_window();
break;
case 3:
add_interpreterlanguage_diaglogue_window();
break;
case 4:
show_all_languages_of_interpreter_diaglogue_window();
break;
case 5:
show_info_about_language_interpreters_diaglogue_window();
break;
default:
cout << "Wrong command" << endl;
return false;
}
}
#pragma endregion