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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
| #include <iostream> // Управление потоком ввода/вывода
#include <fstream> // Работа с файлами
#include <time.h>
#include <string> // Работа со строками
#include <sstream> // istringstream class
#include <windows.h> // Создание "интерфейса", для работы с ОС Windows
#include <filesystem> // Работа с файлами
#include <Shellapi.h> // Открытие файлов в ОС Windows
/* CMD палитра*/
#define WHITE 15
#define LIGHTBLUE 9
#define RED 12
#define BLUE 3
#define GREEN 10
#define YELLOW 6
#define PURPLE 5
/* CMD палитра*/
#define entrySize 30
using namespace std;
ifstream g_inFile; // Открыть файл.
ofstream g_outFile; //Закрыть файл
ofstream g_outResultsFile; //Создать новый файл
fstream g_editFile; //Редактировать файл
HANDLE g_hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // CMD палитра
struct Entry {
int id[50];
string numPK[50];
string MB[50];
string CPU[50];
string VG[50];
string HDD[50];
int Memory[50];
string OptMedia[50];
int Drive[50];
string Monitor[50];
int Aud[30];
}; 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");
int _actions,
amountOfData = 0;
string fileName;
Entry* d = new Entry[amountOfData];
menu("N/A");
}
void menu(string currentFile) {
system("CLS");
short int caseInput = 0;
SetConsoleTextAttribute(g_hConsole, PURPLE);
cout << "\nОБЕСПЕЧЕНИЕ ПЕРСОНАЛЬНЫМИ КОМПЬЮТЕРАМИ КАФЕДРЫ АС (1.0)" << endl;
SetConsoleTextAttribute(g_hConsole, YELLOW);
cout << "\nТекущий файл: " << currentFile << endl;
SetConsoleTextAttribute(g_hConsole, GREEN);
cout << "\n1. Задание"
"\n2. Открыть или создать файл"
"\n3. Редактировать файл"
"\n4. Руководство пользователя (Справка)"
"\n0. Выход";
SetConsoleTextAttribute(g_hConsole, 6);
cout << "\nЧто Вы хотите сделать? ";
while (!(cin >> caseInput) || (cin.peek() != '\n')) {
cin.clear();
while (cin.get() != '\n');
system("CLS"); //Очистка пространства консоли
system("clear");
cout << "Некорректный ввод!\n";
menu("N/A");
}
switch (caseInput) {
case 1:
SetConsoleTextAttribute(g_hConsole, 9);
cout << "\n36. Обеспечение персональными компьютерами кафедры АС фиксируется в файле со следующими сведениями:"
"\nназвание (номер) ПК, материнская плата, процессор,видеокарта, жёсткий диск, память, оптические носители, "
"\nдисковод, монитор, аудитория. Написать программу позволяющую: ";
SetConsoleTextAttribute(g_hConsole, 3);
cout << "\n-Cформировать список ПК, находящихся в одной аудитории."
"\n-Выдать список ПК, память которых меньше 128 Мб."
"\n-Подсчитать количество компьютеров в каждой аудитории."
"\n-Определить самого распространённого производителя материнских плат." << endl;
system("pause");
menu("N/A");
break;
case 2: manageFile(currentFile);
break;
case 3: outputFile(currentFile);
break;
case 4: userManual(currentFile);
break;
case 5: writeResults();
break;
case 0:
exit(3);
//сохранение данных и освобождение памяти
g_editFile.close();
g_inFile.close();
g_outFile.close();
break;
default:
cout << "Некорректный ввод. Повторите попытку.";
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. Создать новый файл"
"\n2. Oткрыть существующий файл"
"\n3. Назад в главное меню" << endl;
SetConsoleTextAttribute(g_hConsole, 6);
cout << "\nЧто Вы хотите сделать? ";
cin >> input;
switch (input) {
case 1: createFile();
break;
case 2: openFile(runningFile);
break;
case 3: menu("N/A");
break;
default:
cout << "Некорректный выбор." << endl;
menu("N/A");
}
}
void createFile() {
system("CLS");
string fileName, temp, aMB, aCPU, aVG, aHDD, aOptMedia, aMonitor, anumPK;
int aMemory, aAud;
bool aDrive, aChoice = true;
SetConsoleTextAttribute(g_hConsole, 5);
cout << "Введите имя файла: "; cin >> fileName;
ifstream f(fileName.c_str());
bool fileExist = f.good();
if (fileExist) {
SetConsoleTextAttribute(g_hConsole, RED);
cout << "\nФайл с таким именем уже существует!"
"\nПопробуйте снова!" << endl;
system("Pause");
createFile();
}
g_outFile.open(fileName);
do {
SetConsoleTextAttribute(g_hConsole, GREEN); // output
cout << "\nВведите данные о ПК: \n" << endl;
srand(time(NULL));
DWORD id = rand() % 1000;
//g_outFile << "ID:"
g_outFile << id << endl;
// Ввод информации.
SetConsoleTextAttribute(g_hConsole, 6); //
cout << "Название (номер) ПК: "; cin >> anumPK;
//g_outFile << "НомерПК:" <<
g_outFile << anumPK << endl;
cout << "Материнская плата: "; cin >> aMB;
//g_outFile << "Мат.плата:" << aMB << endl;
cout << "Процессор ПК: "; cin >> aCPU;
//g_outFile << "Процессор:" <<
g_outFile << aCPU << endl;
cout << "Видеокарта: "; cin >> aVG;
//g_outFile << "Видеокарта: " <<
g_outFile << aVG << endl;
cout << "Жёсткий диск : "; cin >> aHDD;
//g_outFile << "ЖД:" <<
g_outFile << aHDD << endl;
cout << "Память(Mб): "; cin >> aMemory;
//g_outFile << "Память:" <<
g_outFile << aMemory << endl;
cout << "Оптические носители:"; cin >> aOptMedia;
//g_outFile << "Опт.носители:" <<
g_outFile << aOptMedia << endl;
cout << "Монитор ПК: "; cin >> aMonitor;
//g_outFile << "Монитор:" <<
g_outFile << aMonitor << endl;
do{
cout << "Аудитория (1-10): ";
cin >> aAud;
} while (aAud<1 || aAud>10);
//g_outFile << "Аудитория:" <<
g_outFile << aAud << endl;
cout << "Наличие дисковода(Есть/Отсутствует): "; cin >> temp;
aDrive = (temp == "есть" || temp == "Есть") ? 1 : 0;
//g_outFile << "Дисковод:" <<
g_outFile << temp << endl;
cout << "Добавить данные о других ПК?(True/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 << "\nВведите имя файла" "\nОн должен находиться в том же каталоге, что и программа";
SetConsoleTextAttribute(g_hConsole, 6);
cout << "\nFilename(example.txt): ";
cin >> fileName;
g_inFile.open(fileName);
if (!g_inFile) {
SetConsoleTextAttribute(g_hConsole, RED);
cout << "Невозможно открыть файл... " "Попытайтесь снова..." << endl;
system("pause");
openFile("");
}
SetConsoleTextAttribute(g_hConsole, WHITE);
cout << fileName << " успешно открыт!" << endl;
system("pause");
menu(fileName);
}
void calcInfo() {
system("CLS");
int input = 0;
SetConsoleTextAttribute(g_hConsole, WHITE);
cout << "Что Вы хотите сделать далее?" << endl;
SetConsoleTextAttribute(g_hConsole, GREEN);
cout << "1. Записать результат в файл"
"\n2. Вернуться в меню" << endl;
SetConsoleTextAttribute(g_hConsole, BLUE);
cout << "Ваш выбор(1-2): "; cin >> input;
switch (input) {
case 1:
break;
case 2:
break;
default:
SetConsoleTextAttribute(g_hConsole, RED);
cout << "Некорректный выбор." << 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 << "\nОткрыт файл: " << fileName << endl << endl;
g_editFile.open(fileName);
while (!g_editFile.eof()){
counter++;
g_editFile >> g_item.id[counter];
g_editFile.ignore();
getline(g_editFile, line);
g_item.numPK[counter] = line;
getline(g_editFile, line);
g_item.MB[counter] = line;
getline(g_editFile, line);
g_item.CPU[counter] = line;
getline(g_editFile, line);
g_item.VG[counter] = line;
getline(g_editFile, line);
g_item.HDD[counter] = line;
g_editFile >> g_item.Memory[counter];
g_editFile.ignore();
getline(g_editFile, line);
g_item.OptMedia[counter] = line;
g_editFile >> g_item.Drive[counter];
g_editFile.ignore();
getline(g_editFile, line);
g_item.Monitor[counter] = line;
g_editFile >> g_item.Aud[counter];
}
g_outResultsFile.open("result.txt");
do{
system("CLS");
cout << endl;
for (int i = 0; i <= counter; i++)
{
cout << "ID: " << g_item.id[i] << ", №: " << g_item.numPK[i] << ", Материнка: " << g_item.MB[i] << ", Процессор: "
<< g_item.CPU[i] << ", Видео: " << g_item.VG[i] << ", ЖД: " << g_item.HDD[i] << ", Память: " << g_item.Memory[i]
<< ", Оптика: " << g_item.OptMedia[i] << ", Привод: " << g_item.Drive[i] << ", Монитор: " << g_item.Monitor[i]
<< ", Аудитория: " << g_item.Aud[i] << endl;
}
SetConsoleTextAttribute(g_hConsole, 6);
cout << "\nЧто Вы хотите сделать теперь?: ";
SetConsoleTextAttribute(g_hConsole, GREEN);
cout << "\n1. Редактировать документ"
"\n2. Сохранить"
"\n3. Cформировать список ПК, находящихся в одной аудитории"
"\n4. Выдать список ПК, память которых меньше 128 Мб"
"\n5. Подсчитать количество компьютеров в каждой аудитории"
"\n6. Определить самого распространённого производителя материнских плат"
"\n0. Назад в главное меню" << endl;
SetConsoleTextAttribute(g_hConsole, 6);
cout << "Что Вы хотите сделать?: ";
cin >> input;
int kk = 0,max,m;
string tmp,tmps;
int tmpk;
switch (input) {
case 1:
// it somehow broke visual studio's intellisense
fileNameLPCSTR = fileName.c_str();
editFile(fileNameLPCSTR);
cout << "Файл открыт для редактирования! " "Не забудьте сохранить изменения в текстовом редакторе." << endl;
break;
case 2: calcInfo();
break;
case 3:
cout << "Какая аудитория интересует: ";
cin >> tmpk;
g_outResultsFile << "\nПоиск компьютеров из аудитории №" << tmpk << endl;
for (int i = 0; i <= counter; i++)
if (tmpk == g_item.Aud[i]) {
kk++;
cout << "ID: " << g_item.id[i] << ", №: " << g_item.numPK[i] << ", Материнка: " << g_item.MB[i] << ", Процессор: "
<< g_item.CPU[i] << ", Видео: " << g_item.VG[i] << ", ЖД: " << g_item.HDD[i] << ", Память: " << g_item.Memory[i]
<< ", Оптика: " << g_item.OptMedia[i] << ", Привод: " << g_item.Drive[i] << ", Монитор: " << g_item.Monitor[i]
<< ", Аудитория: " << g_item.Aud[i] << endl;
g_outResultsFile << "ID: " << g_item.id[i] << ", №: " << g_item.numPK[i] << ", Материнка: " << g_item.MB[i] << ", Процессор: "
<< g_item.CPU[i] << ", Видео: " << g_item.VG[i] << ", ЖД: " << g_item.HDD[i] << ", Память: " << g_item.Memory[i]
<< ", Оптика: " << g_item.OptMedia[i] << ", Привод: " << g_item.Drive[i] << ", Монитор: " << g_item.Monitor[i]
<< ", Аудитория: " << g_item.Aud[i] << endl;
}
if (kk == 0){
g_outResultsFile << "Не найдено такой аудитории" << endl;
cout << "Не найдено такой аудитории" << endl;
}
break;
case 4:
g_outResultsFile << "\nПоиск ПК с обьемом памяти не более 128Мб" << endl;
for (int i = 0; i <= counter; i++)
if (g_item.Memory[i] <= 128) {
kk++;
cout << "ID: " << g_item.id[i] << ", №: " << g_item.numPK[i] << ", Материнка: " << g_item.MB[i] << ", Процессор: "
<< g_item.CPU[i] << ", Видео: " << g_item.VG[i] << ", ЖД: " << g_item.HDD[i] << ", Память: " << g_item.Memory[i]
<< ", Оптика: " << g_item.OptMedia[i] << ", Привод: " << g_item.Drive[i] << ", Монитор: " << g_item.Monitor[i]
<< ", Аудитория: " << g_item.Aud[i] << endl;
g_outResultsFile << "ID: " << g_item.id[i] << ", №: " << g_item.numPK[i] << ", Материнка: " << g_item.MB[i] << ", Процессор: "
<< g_item.CPU[i] << ", Видео: " << g_item.VG[i] << ", ЖД: " << g_item.HDD[i] << ", Память: " << g_item.Memory[i]
<< ", Оптика: " << g_item.OptMedia[i] << ", Привод: " << g_item.Drive[i] << ", Монитор: " << g_item.Monitor[i]
<< ", Аудитория: " << g_item.Aud[i] << endl;
}
if (kk == 0) {
g_outResultsFile << "Не найдено таких ЖД" << endl;
cout << "Не найдено таких ЖД" << endl;
}
break;
case 5:
g_outResultsFile << "\nПодсчет количества ПК в аудиториях" << endl;
for (int j = 1; j <= 10; j++){
kk = 0;
for (int i = 0; i <= counter; i++)
if (g_item.Aud[i] == j) {
kk++;
}
g_outResultsFile << "Аудитория №" << j << " = " << kk << endl;
cout << "Аудитория №" << j << " = " << kk << endl;
}
break;
case 6:
max = 0;
tmps="";
for (int i = 0; i <= counter; i++)
{
m = 0;
tmp = g_item.MB[i].substr(0, g_item.MB[i].find(' '));
for (int j = i + 1; j <= counter; j++)
if (tmp == g_item.MB[j].substr(0, g_item.MB[j].find(' '))) m++;
if (m > max) {
max = m;
tmps = tmp;
}
}
g_outResultsFile << "\nСамый популярный производител материнок: " << tmps << endl;
break;
case 0: menu(fileName);
break;
default:
SetConsoleTextAttribute(g_hConsole, RED);
cout << "Некорректный ввод!" << endl;
menu(fileName);
}
system("pause");
} while (input != 0);
menu(fileName);
}
else {
SetConsoleTextAttribute(g_hConsole, RED);
cout << "\n!!!Отсутствует текущий файл!!! \n!!!Создайте файл или вернитесь в главное меню!!!!" << endl << endl;
system("pause");
menu("N/A");
}
}
void writeResults() {
for (int i = 0; i <= entrySize; i++) {
cout << "\nID: " << g_item.id[i] << endl;
cout << "Номер ПК:" << g_item.numPK[i] << endl;
cout << "Мат. плата:" << g_item.MB[i] << endl;
cout << "Процессор:" << g_item.CPU[i] << endl;
cout << "Видеокарта:" << g_item.VG[i] << endl;
cout << "Жёсткий диск:" << g_item.HDD[i] << endl;
cout << "Оптические носители:" << g_item.OptMedia[i] << endl;
cout << "Дисковод:" << g_item.Drive[i] << endl;
cout << "Монитор:" << g_item.Monitor[i] << endl;
cout << "Аудитория:" << g_item.Aud[i] << endl;
}
}
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("Ошибка при попытке открыть файл.");
}
}
void userManual(string fileName) {
cout << "\n";
SetConsoleTextAttribute(g_hConsole, RED);
ifstream userManual("reference");
string line;
SetConsoleTextAttribute(g_hConsole, RED);
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);
} |