-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbigexam1.cpp
More file actions
569 lines (508 loc) · 16.9 KB
/
bigexam1.cpp
File metadata and controls
569 lines (508 loc) · 16.9 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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
/*------------------------------MANAGE STUDENT-------------------------------*/
// fuction to turn ',' and ' ' into ' '
string processLine(const string &line)
{
string processedLine = line;
for (char &ch : processedLine)
{
if (ch == ',' || ch == ' ')
{
ch = ' ';
}
}
return processedLine;
}
// Input student from file
void loadStudentFromFile(const string &filename, vector<string> &listname, vector<vector<float>> &listScores)
{
ifstream file(filename);
string line;
cout << "Student list: " << endl;
while (getline(file, line))
{
string processedLine = processLine(line);
stringstream splitData(processedLine);
string name;
float math, english, literature, average;
// read
splitData >> name >> math >> literature >> english >> average;
listname.push_back(name);
listScores.push_back({math, literature, english, average});
}
file.close();
}
// Show student
void showStudentList(const vector<string> &listname, vector<vector<float>> &listScores)
{
if (listname.empty())
{
cout << "List is empty. Please add student" << endl;
return;
}
for (int i = 0; i < listname.size(); ++i)
{
cout << "student name: " << listname[i] << ", ";
cout << "Math: " << listScores[i][0] << ", ";
cout << "Literature: " << listScores[i][1] << ", ";
cout << "English: " << listScores[i][2] << " , ";
// Average point
double average = (listScores[i][0] + listScores[i][1] + listScores[i][2]) / 3.0;
cout << "Average: " << average << endl;
}
}
void ouputStudentList(const vector<string> &listname, vector<vector<float>> &listScores)
{
ofstream outputFile("studentDB.txt");
for (int i = 0; i < listname.size(); ++i)
{
outputFile << listname[i] << ", ";
outputFile << listScores[i][0] << ", ";
outputFile << listScores[i][1] << ", ";
outputFile << listScores[i][2] << " , ";
// Average point
double average = (listScores[i][0] + listScores[i][1] + listScores[i][2]) / 3.0;
outputFile << average << endl;
}
outputFile.close();
}
// Add student
void addStudent(vector<string> &names, vector<vector<float>> &scores)
{
string name;
vector<float> studentScores(3);
cout << "Enter student name: ";
cin.ignore();
getline(cin, name);
names.push_back(name);
// enter point
cout << "Enter Math score: ";
cin >> studentScores[0];
cout << "Enter Literature score: ";
cin >> studentScores[1];
cout << "Enter English score: ";
cin >> studentScores[2];
scores.push_back(studentScores);
cout << "Add new student completed...." << endl;
}
// remove student
void removeStudent(vector<string> &listname, vector<vector<float>> &listScores)
{
string name;
cout << "Enter student name you want to erase: ";
cin.ignore();
getline(cin, name);
for (int i = 0; i < listname.size(); ++i)
{
if (listname[i] == name)
{
listname.erase(listname.begin() + i);
listScores.erase(listScores.begin() + i);
cout << "Student" << name << "is erased success" << endl;
}
else
{
cout << "student not exist in the list";
}
}
}
// Search student
void searchStudent(vector<string> &listname, vector<vector<float>> &listScores)
{
string name;
cout << "Enter student name you want to find: ";
cin.ignore();
getline(cin, name);
for (int i = 0; i < listname.size(); ++i)
{
int sum = 0;
if (listname[i] == name)
{
cout << "---Student information---" << endl;
cout << "Student name: " << listname[i] << ", ";
cout << "Math: " << listScores[i][0] << ", ";
cout << "Literature: " << listScores[i][1] << ", ";
cout << "English: " << listScores[i][2] << endl;
// Average point
double average = (listScores[i][0] + listScores[i][1] + listScores[i][2]) / 3.0;
cout << "Average: " << average << endl;
return;
}
}
cout << "student not exist in the list" << endl;
}
// UpdateStudent
void updateStudent(vector<string> &listname, vector<vector<float>> &listScores)
{
string name;
cout << "Enter student name you want to update: ";
cin.ignore();
getline(cin, name);
for (int i = 0; i < listname.size(); ++i)
{
if (listname[i] == name)
{
string newName;
int newScore;
vector<float> newStudentScores(3);
cout << "Enter new student name: ";
getline(cin, newName);
if (!newName.empty())
{
listname[i] = newName;
}
cout << "Enter new Math score: ";
cin >> newScore;
newStudentScores[0] = newScore;
cout << "Enter new Literature score: ";
cin >> newScore;
newStudentScores[1] = newScore;
cout << "Enter new English score: ";
cin >> newScore;
newStudentScores[2] = newScore;
listScores[i] = newStudentScores;
cout << "Student information updated successfully.\n";
return;
}
}
cout << "Student not found in the list.\n";
}
// show top student
void showTopStudent(vector<string> &listName, vector<vector<float>> &listScore, int enterTop, int subjectIndex)
{
if (subjectIndex == 0)
{
// create vector pair
vector<pair<float, string>> topStudentMathList;
for (int i = 0; i < listName.size(); i++)
{
topStudentMathList.push_back({listScore[i][0], listName[i]});
}
// sort
sort(topStudentMathList.begin(), topStudentMathList.end(), greater<pair<float, string>>());
// show
int count = min(enterTop, static_cast<int>(topStudentMathList.size()));
for (int i = 0; i < count; i++)
{
cout << "Name: " << topStudentMathList[i].second << ", math: " << topStudentMathList[i].first << endl;
}
}
if (subjectIndex == 1)
{
// create vector pair
vector<pair<float, string>> topStudentLiteratureList;
for (int i = 0; i < listName.size(); i++)
{
topStudentLiteratureList.push_back({listScore[i][1], listName[i]});
}
// sort
sort(topStudentLiteratureList.begin(), topStudentLiteratureList.end(), greater<pair<float, string>>());
// show
int count2 = min(enterTop, static_cast<int>(topStudentLiteratureList.size()));
for (int i = 0; i < count2; i++)
{
cout << "Name: " << topStudentLiteratureList[i].second << ", literature: " << topStudentLiteratureList[i].first << endl;
}
}
if (subjectIndex == 2)
{
// create vector pair
vector<pair<float, string>> topStudentEnglishList;
for (int i = 0; i < listName.size(); i++)
{
topStudentEnglishList.push_back({listScore[i][2], listName[i]});
}
// sort
sort(topStudentEnglishList.begin(), topStudentEnglishList.end(), greater<pair<float, string>>());
// show
int count3 = min(enterTop, static_cast<int>(topStudentEnglishList.size()));
for (int i = 0; i < count3; i++)
{
cout << "Name: " << topStudentEnglishList[i].second << ", english: " << topStudentEnglishList[i].first << endl;
}
}
else
{
// create vector pair
vector<pair<float, string>> topStudentAverageList;
for (int i = 0; i < listName.size(); i++) {
float score = (listScore[i][0] + listScore[i][1] + listScore[i][2]) / 3.0;
topStudentAverageList.push_back({score, listName[i]});
}
// sort
sort(topStudentAverageList.begin(), topStudentAverageList.end(), greater<pair<float, string>>());
// show
int count3 = min(enterTop, static_cast<int>(topStudentAverageList.size()));
for (int i = 0; i < count3; i++) {
cout << "Name: " << topStudentAverageList[i].second << ", english: " << topStudentAverageList[i].first << endl;
}
}
}
// Show student had average < 5
void showStudentsBelowAverage(vector<string> &listname, vector<vector<float>> &listScores)
{
cout << "---List student had score Below 5";
for (int i = 0; i < listname.size(); ++i)
{
double average = (listScores[i][0] + listScores[i][1] + listScores[i][2]) / 3.0;
if (average < 5)
{
cout << "Name: " << listname[i] << endl;
cout << "Math " << listScores[i][0] << ", ";
cout << "Literature: " << listScores[i][1] << ", ";
cout << "English: " << listScores[i][2] << ", ";
cout << "Average point: " << average << endl;
}
}
}
/*------------------------------MANAGE CLASS-------------------------------*/
// Show class
void showClassList(vector<string> &listClass)
{
if (listClass.empty())
{
cout << "List Class is empty. Please add class" << endl;
return;
}
cout << "Class list: " << endl;
for (int i = 0; i < listClass.size(); ++i)
{
cout << "Class name: " << listClass[i] << endl;
}
}
// Add class
void addClass(vector<string> &classes)
{
string nameClass;
cout << "Enter class name ";
cin.ignore();
getline(cin, nameClass);
classes.push_back(nameClass);
cout << "Add class completed...." << endl;
}
//Update class
void updateClass(vector<string> &classes)
{
string name;
cout << "Enter class name you want to update: ";
cin.ignore();
getline(cin, name);
for (int i = 0; i < classes.size(); ++i)
{
if (classes[i] == name)
{
string newName;
cout << "Enter new class name: ";
getline(cin, newName);
if (!newName.empty())
{
classes[i] = newName;
}
cout << "Class information updated successfully.\n";
return;
}
}
cout << "Class not found in the list.\n";
}
// remove class
void removeClass(vector<string> &classes)
{
string nameClass;
cout << "Enter class name you want to erase: ";
cin.ignore();
getline(cin, nameClass);
for (int i = 0; i < classes.size(); ++i)
{
if (classes[i] == nameClass)
{
classes.erase(classes.begin() + i);
cout << "Class" << nameClass << "is erased success" << endl;
}
else
{
cout << "Class not exist in the list" << endl;
}
}
}
// Search class
void searchClass(vector<string> &listClass)
{
string nameClass;
cout << "Enter class name you want to find: ";
cin.ignore();
getline(cin, nameClass);
for (int i = 0; i < listClass.size(); ++i)
{
int sum = 0;
if (listClass[i] == nameClass)
{
cout << "---Student information---" << endl;
cout << "Class: " << nameClass;
return;
}
}
cout << "class not exist in the list" << endl;
}
int main()
{
vector<string> className;
vector<string> studentName;
vector<vector<float>> studentScores;
// Load data from file
loadStudentFromFile("studentDB.txt", studentName, studentScores);
// Menu program
char option;
cout << "------School Program------" << endl;
cout << "1. Manage student" << endl;
cout << "2. Manage classes" << endl;
cout << "q. Quit" << endl;
while (true)
{
cout << "Enter your option: ";
cin >> option;
option = tolower(option);
bool isQuitProgram = false;
switch (option)
{
case '1':
char optionManageStudent;
cout << "Manage student" << endl;
cout << "1. Show student" << endl;
cout << "2. Add student" << endl;
cout << "3. Remove student" << endl;
cout << "4. Update student" << endl;
cout << "5. Search student by name" << endl;
cout << "6. View top students" << endl;
cout << "7. View failed students" << endl;
cout << "q. Quit" << endl;
while (true)
{
cout << "Enter you option: ";
cin >> optionManageStudent;
optionManageStudent = tolower(optionManageStudent);
bool isQuitManageStudentProgram = false;
switch (optionManageStudent)
{
case '1':
showStudentList(studentName, studentScores);
break;
case '2':
addStudent(studentName, studentScores);
break;
case '3':
removeStudent(studentName, studentScores);
break;
case '4':
updateStudent(studentName, studentScores);
break;
case '5':
searchStudent(studentName, studentScores);
break;
case '6':
int enterTopStudent;
cout << "Enter number of top you want to show: ";
cin >> enterTopStudent;
int enterSubject;
cout << "Enter which subject you want to show (0: Math, 1: Literature, 2: English, 3: Average): ";
cin >> enterSubject;
showTopStudent(studentName, studentScores, enterTopStudent, enterSubject);
break;
case '7':
showStudentsBelowAverage(studentName, studentScores);
break;
case 'q':
cout << "Existing manage student program. Turn back to homepage" << endl;
isQuitManageStudentProgram = true;
break;
default:
cout << "Invalid option" << endl;
break;
}
if (isQuitManageStudentProgram)
{
cout << "------School Program------" << endl;
cout << "1. Manage student" << endl;
cout << "2. Manage classes" << endl;
cout << "q. Quit" << endl;
break;
}
}
break;
case '2':
char optionManageClass;
cout << "Manage Class" << endl;
cout << "1. Show Class" << endl;
cout << "2. Add Class" << endl;
cout << "3. Remove Class" << endl;
cout << "4. Update Class" << endl;
cout << "5. Search Class by name" << endl;
cout << "5. View student list of a class" << endl;
cout << "5. View Top Class" << endl;
cout << "q. Quit" << endl;
while (true)
{
cout << "Enter you option: ";
cin >> optionManageClass;
optionManageClass = tolower(optionManageClass);
bool isQuitManageClassProgram = false;
switch (optionManageClass)
{
case '1':
showClassList(className);
break;
case '2':
addClass(className);
break;
case '3':
removeClass(className);
break;
case '4':
updateClass(className);
break;
case '5':
searchClass(className);
break;
case '6':
// show top student
break;
case 'q':
cout << "Existing manage class program. Turn back to homepage" << endl;
isQuitManageClassProgram = true;
break;
default:
cout << "Invalid option" << endl;
break;
}
if (isQuitManageClassProgram)
{
cout << "------School Program------" << endl;
cout << "1. Manage student" << endl;
cout << "2. Manage classes" << endl;
cout << "q. Quit" << endl;
break;
}
}
break;
case 'q':
cout << "Existing...";
isQuitProgram = true;
// write in DB
ouputStudentList(studentName, studentScores);
break;
default:
cout << "Invalid option" << endl;
break;
}
if (isQuitProgram)
{
break;
}
}
return 0;
}