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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
| #include <iostream> // user IO
#include <fstream> // files IO
#include <time.h>
#include <string> // getline()
#include <sstream> // istringstream class
#include <windows.h> // cmd colors
#include <filesystem> // checking for conflicting files
#include <Shellapi.h> // open file in windows
/* CMD COLORS */
#define WHITE 15
#define RED 12
#define BLUE 3
#define GREEN 10
/* CMD COLORS */
#define entrySize 15 // max items in Entry
using namespace std;
ifstream g_inFile; // open a file
ofstream g_outFile; // create a new
ofstream g_outResultsFile; // create a new
fstream g_editFile; // edit a file
HANDLE g_hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // CMD colors
struct Entry {
int id[50]{};
int numPK[50]{};
string MB[50]{};
string CPU[50]{};
string VG[50]{};
string HDD[50]{};
string OptMedia[50]{};
bool Drive[50]{};
string Monitor[50]{};
int Aud[50]{};
}; Entry g_item;
void openFile(string runningFile);
void createFile();
void manageFile(string runningFile);
void outputFile(string fileName);
void writeResults();
void editFile(LPCSTR fileName);
void calcInfo();
void menu(string currentFile);
void userManual(string fileName);
int main() {
setlocale(LC_ALL, "Rus");
menu("N/A");
}
void menu(string currentFile) {
system("CLS");
short int caseInput = 0;
SetConsoleTextAttribute(g_hConsole, RED);
cout << "\nThe Red Book by Boris Elgin v0.1 (13.05.2021)" << endl;
SetConsoleTextAttribute(g_hConsole, WHITE);
cout << "Current File: " << currentFile << endl;
SetConsoleTextAttribute(g_hConsole, GREEN);
cout << "\n0. Task!"
"\n1. Open or Create a file"
"\n2. Display info from current file"
"\n3. User Manual"
"\n4. Quit from program";
SetConsoleTextAttribute(g_hConsole, 3);
cout << "\nChoose action: ";
while (!(cin >> caseInput) || (cin.peek() != '\n')) {
cin.clear();
while (cin.get() != '\n');
system("CLS"); // Windows
system("clear"); // Unix
cout << "Wrong input\n";
menu("N/A");
}
switch (caseInput) {
case 0:
SetConsoleTextAttribute(g_hConsole, WHITE);
cout << "\n37. Краткие сведения об исчезающих животных заносятся в файл: название (вид), семейство, среда обитания, численность, возможность размножения в не-воле."
"Написать программу, формирующую список вымирающих животных в заданной среде обитания, отсортированный по численности."
"Вывести живот-ных, способных размножаться в неволе."
"Определить самое многочисленное и малочисленное семейство по количеству исчезающих видов." << endl;
system("pause");
menu("N/A");
break;
case 1: manageFile(currentFile);
break;
case 2: outputFile(currentFile);
break;
case 3: userManual(currentFile);
break;
case 4:
exit(3);
// save and free memory
g_editFile.close();
g_inFile.close();
g_outFile.close();
break;
default:
cout << "Wrong choice, try again.";
system("CLS"); // Windows
system("clear"); // Unix
menu("N/A"); //
}
}
void manageFile(string runningFile) {
system("CLS");
short int input = 0;
SetConsoleTextAttribute(g_hConsole, GREEN);
cout << "\n1. Create new file"
"\n2. Open an existing file"
"\n3. Go back" << endl;
SetConsoleTextAttribute(g_hConsole, BLUE);
cout << "\nChoose action: ";
cin >> input;
switch (input) {
case 1: createFile();
break;
case 2: openFile(runningFile);
break;
case 3: menu("N/A");
break;
default:
cout << "Wrong action" << endl;
menu("N/A");
}
}
void createFile() {
system("CLS");
string fileName, temp, aMB, aCPU, aVG, aHDD, aOptMedia, aMonitor;
int anumPK, aAud;
bool aDrive,
aChoice = false;
SetConsoleTextAttribute(g_hConsole, BLUE);
cout << "Create a name for file: "; cin >> fileName;
// it's experimental in C++14 and stable in C++17
// https://en.cppreference.com/w/cpp/filesystem
// https://en.cppreference.com/w/cpp/filesystem/remove
// I can also delete an existing file and create a new one. but is it worth it?
bool fileExist = std::filesystem::exists(fileName);
if (fileExist) {
SetConsoleTextAttribute(g_hConsole, RED);
cout << "\nThere is an already named file like this!"
"\nConsider making a new one!" << endl;
system("Pause");
createFile();
}
g_outFile.open(fileName);
do {
SetConsoleTextAttribute(g_hConsole, WHITE); // output
cout << "Now write information about animal: \n" << endl;
// make an unique id here (this method is ok cause I don't need to worry too much about ids)
// https://stackoverflow.com/questions/65524/generating-a-unique-id-in-c
srand(time(NULL));
DWORD id = rand() % 1000;
g_outFile << "ID файла:" << id << endl;
// Ввод информации.
SetConsoleTextAttribute(g_hConsole, BLUE); //
cout << "Название (номер) ПК:"; cin >> anumPK;
g_outFile << "НомерПК:" << anumPK << endl;
cout << "Материнская плата:"; cin >> aMB;
g_outFile << "Мат.плата:" << aMB << endl;
cout << "Процессор ПК:"; cin >> aCPU;
g_outFile << "Процессор:" << aCPU << endl;
cout << "Видеокарта: "; cin >> aVG;
g_outFile << "Видеокарта: " << aVG << endl;
cout << "Жёсткий диск: "; cin >> aHDD;
g_outFile << "ЖД:" << aHDD << endl;
cout << "Оптические носители:"; cin >> aOptMedia;
g_outFile << "Опт.носители:" << aOptMedia << endl;
cout << "Монитор ПК:"; cin >> aMonitor;
g_outFile << "Монитор:" << aMonitor << endl;
cout << "Аудитория:"; cin >> aAud;
g_outFile << "Аудитория:" << aAud << endl;
cout << "Наличие дисковода(Есть/Отсутствует): "; cin >> temp;
aDrive = (temp == "есть" || temp == "Есть") ? true : false;
g_outFile << "Дисковод:" << temp << endl << endl;
cout << "Do you want to add another animal?(True or False): "; cin >> temp;
aChoice = (temp == "true" || temp == "True") ? true : false;
} while (aChoice);
g_inFile.close(); // close&save file
menu(fileName); // just to show filename in menu
}
void openFile(string runningFile) {
if (runningFile != "") {
// if there is a running file close everything
g_editFile.close();
g_inFile.close();
g_outFile.close();
}
system("CLS");
string fileName;
SetConsoleTextAttribute(g_hConsole, WHITE);
cout << "\nEnter name of the file" "\nIt must be in the same directory as the program";
SetConsoleTextAttribute(g_hConsole, BLUE);
cout << "\nFilename(example.txt): ";
cin >> fileName;
g_inFile.open(fileName);
if (!g_inFile) {
SetConsoleTextAttribute(g_hConsole, RED);
cout << "Unable to open file... " "Try again..." << endl;
system("pause");
openFile("");
}
SetConsoleTextAttribute(g_hConsole, WHITE);
cout << fileName << " loaded successfully!" << endl;
system("pause");
menu(fileName);
}
/*int editFile(LPCSTR fileName) {
// LPCSTR: pointer to null terminated const string of char
if (ShellExecuteA(NULL, NULL, fileName, NULL, NULL, SW_RESTORE) <= (HINSTANCE)32) {
puts("An error occured when trying to open file.");
}
}*/
void calcInfo() {
system("CLS");
int lastIndex = 0,
input = 0;
// sort
// bug fix for comparing latest item id with dummy item with id = 0
// make check for breed
/* SetConsoleTextAttribute(g_hConsole, RED);
cout << "\nThe most populated animal: " << endl;
SetConsoleTextAttribute(g_hConsole, WHITE);
cout << "ID: " << g_item.id[0] << endl;
cout << "NAME: " << g_item.name[0] << endl;
cout << "FAMILY: " << g_item.family[0] << endl;
cout << "HABITAT: " << g_item.habitat[0] << endl;
cout << "POPULATION: " << g_item.population[0] << endl;
cout << "CAN BREED: " << g_item.breedAbility[0] << endl << endl;
SetConsoleTextAttribute(g_hConsole, RED);
cout << "\nThe most inpopulated animal: " << endl;
SetConsoleTextAttribute(g_hConsole, WHITE);
cout << "ID: " << g_item.id[lastIndex] << endl;
cout << "NAME: " << g_item.name[lastIndex] << endl;
cout << "FAMILY: " << g_item.family[lastIndex] << endl;
cout << "HABITAT: " << g_item.habitat[lastIndex] << endl;
cout << "POPULATION: " << g_item.population[lastIndex] << endl;
cout << "CAN BREED: " << g_item.breedAbility[lastIndex] << endl << endl;
// make option to save results in new file
SetConsoleTextAttribute(g_hConsole, WHITE);
cout << "What to do next?" << endl;
SetConsoleTextAttribute(g_hConsole, GREEN);
cout << "1. Write results to file"
"\n2. Go back to menu" << endl;
SetConsoleTextAttribute(g_hConsole, BLUE);*/
cout << "Choose action(1-2): "; cin >> input;
switch (input) {
case 1:
break;
case 2:
break;
default:
SetConsoleTextAttribute(g_hConsole, RED);
cout << "Incorrect action." << endl;
system("pause");
menu("N/A");
}
}
void outputFile(string fileName) {
system("CLS");
int previousMax = 0, input = 0, counter = -1;
string line;
LPCSTR fileNameLPCSTR;
if (fileName != "N/A") {
cout << "\nYou have an active file open: " << fileName << endl << endl;
g_editFile.open(fileName);
// check everythin in one line
while (getline(g_editFile, line)) {
istringstream iss(line);
string result;
if (getline(iss, result, ':')) {
// change this to switch case
if (result == "id") {
string id;
while (getline(iss, id, ',')) {
counter++;
cout << "ID: " << id << endl;
//istringstream(id) >> item.id;
istringstream(id) >> g_item.id[counter];
}
}
if (result == "НомерПК:") {
string numPK;
while (getline(iss, numPK, ',')) {
counter++;
cout << "Номер ПК: " << numPK << endl;
//istringstream(id) >> item.id;
istringstream(numPK) >> g_item.numPK[counter];
}
}
if (result == "Мат.плата:") {
string MB;
while (getline(iss, MB, ',')) {
cout << "Материнская плата: " << MB << endl;
//item.name = name;
g_item.MB[counter] = MB;
}
}
if (result == "Процессор:") {
string CPU;
while (getline(iss, CPU, ',')) {
cout << "Процессор ПК: " << CPU << endl;
//item.family = family;
g_item.CPU[counter] = CPU;
}
}
if (result == "Видеокарта:") {
string VG;
while (getline(iss, VG, ',')) {
cout << "Видеокарта: " << VG << endl;
//item.habitat = habitat;
g_item.VG[counter] = VG;
}
}
if (result == "ЖД") {
string HDD;
while (getline(iss, HDD, ',')) {
cout << "Жёсткий диск: " << HDD << endl;
//istringstream(population) >> item.population;
istringstream(HDD) >> g_item.HDD[counter];
}
}
if (result == "Опт.носители") {
string OptMedia;
while (getline(iss, OptMedia, ',')) {
cout << "Оптические носители: " << OptMedia << endl;
//istringstream(population) >> item.population;
istringstream(OptMedia) >> g_item.OptMedia[counter];
}
}
if (result == "Дисковод:") {
string Drive;
while (getline(iss, Drive, ',')) {
cout << "Дисковод: " << Drive << endl << endl;
//istringstream(breedAbility) >> item.breedAbility;
istringstream(Drive) >> g_item.Drive[counter];
}
}
if (result == "Монитор:") {
string Monitor;
while (getline(iss, Monitor, ',')) {
cout << "Мониторы ПК: " << Monitor << endl;
//istringstream(population) >> item.population;
istringstream(Monitor) >> g_item.Monitor[counter];
}
}
if (result == "Аудитория:") {
string aud;
while (getline(iss, aud, ',')) {
cout << "Аудитория: " << aud << endl;
//istringstream(population) >> item.population;
istringstream(aud) >> g_item.Aud[counter];
}
SetConsoleTextAttribute(g_hConsole, WHITE);
cout << "\nWhat you want to do now?:";
SetConsoleTextAttribute(g_hConsole, GREEN);
cout << "\n1. Edit document"
"\n2. Do math and save it to the new document"
"\n3. Go back to menu" << endl;
SetConsoleTextAttribute(g_hConsole, BLUE);
cout << "Choose action: ";
cin >> input;
switch (input) {
case 1:
// it somehow broke visual studio's intellisense
fileNameLPCSTR = fileName.c_str();
editFile(fileNameLPCSTR);
cout << "Book has opened! " "Don't forget to save your changes inside your text editor" << endl;
break;
case 2: calcInfo();
break;
case 3: menu(fileName);
break;
default:
SetConsoleTextAttribute(g_hConsole, RED);
cout << "Wrong action. Saving changes and quit to menu" << endl;
menu(fileName);
}
system("pause");
menu(fileName);
}
else {
SetConsoleTextAttribute(g_hConsole, RED);
cout << "\n!!!You haven't opened/created a new file!!! \n!!!Please make or open it from menu!!!!" << endl << endl;
system("pause");
menu("N/A");
}
}
void writeResults(); {
string fileName;
system("CLS");
cout << "Choose the name for the file:"; cin >> fileName;
bool fileExist = std::filesystem::exists(fileName);
if (fileExist) {
SetConsoleTextAttribute(g_hConsole, RED);
cout << "\nThere is an already named file like this!"
"\nConsider making a new one!" << endl;
system("Pause");
writeResults();
} g_outResultsFile.open(fileName);
for (int i = 0; i <= entrySize; i++) {
g_outResultsFile << "\nID: " << g_item.id[i] << endl;
g_outResultsFile << "Номер ПК:" << g_item.numPK[i] << endl;
g_outResultsFile << "Мат. плата:" << g_item.MB[i] << endl;
g_outResultsFile << "Процессор:" << g_item.CPU[i] << endl;
g_outResultsFile << "Видеокарта:" << g_item.VG[i] << endl;
g_outResultsFile << "Жёсткий диск:" << g_item.HDD[i] << endl;
g_outResultsFile << "Оптические носители:" << g_item.OptMedia[i] << endl;
g_outResultsFile << "Дисковод:" << g_item.Drive[i] << endl;
g_outResultsFile << "Монитор:" << g_item.Monitor[i] << endl;
g_outResultsFile << "Аудитория:" << g_item.Aud[i] << endl;
}
g_outResultsFile.close();
}
}
}
}
void editFile(LPCSTR fileName) {
// LPCSTR: pointer to null terminated const string of char
if (ShellExecuteA(NULL, NULL, fileName, NULL, NULL, SW_RESTORE) <= (HINSTANCE)32) {
puts("An error occured when trying to open file.");
}
}
void userManual(string fileName) {
ifstream userManual("README");
string line;
SetConsoleTextAttribute(g_hConsole, WHITE);
while (getline(userManual, line)) {
cout << line << endl;
}
system("pause");
userManual.close();
// maybe do i need to add a condition to prevent overwriting a running file?
menu(fileName);
} |