-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
145 lines (129 loc) · 4.73 KB
/
Copy pathmain.cpp
File metadata and controls
145 lines (129 loc) · 4.73 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
#include <iostream>
#include "InputUtils.h"
#include "FileManager.h"
#include "StudentRepository.h"
int main() {
Node* head = nullptr;
loadFromFile(head, "students.csv");
int choice = -1;
while (true) {
std::cout << "\n==== Student Management ====\n";
std::cout << "1. Add student\n";
std::cout << "2. Display all students\n";
std::cout << "3. Search student by ID\n";
std::cout << "4. Search student by name\n";
std::cout << "5. Update student by ID\n";
std::cout << "6. Delete student by ID\n";
std::cout << "7. Save to file\n";
std::cout << "8. Statistics (total, avg, min, max)\n";
std::cout << "9. Sort (ID/Name/Average)\n";
std::cout << "0. Exit\n";
std::cout << "Choice: ";
if (!(std::cin >> choice)) {
std::cin.clear();
std::cin.ignore(10000, '\n');
std::cout << "Invalid input\n";
continue;
}
// exit code is 0
if (choice == 0) {
saveToFile(head, "students.csv");
clearList(head);
std::cout << "Goodbye\n";
break;
}
std::cin.ignore(10000, '\n');
// Choices from 1 to 9
switch (choice)
{
case 1: {
addStudent(head);
saveToFile(head, "students.csv");
break;
}
case 2: {
displayStudents(head);
break;
}
case 3: {
int id = getIntInput("Enter ID: ");
Student* s = findStudentByID(head, id);
if (s) s->displayInfo(); else std::cout << "Not found\n";
break;
}
case 4: {
std::string name = getStringInput("Enter name (exact match): ");
// linear search by name
Node* cur = head;
bool found = false;
while (cur) {
if (cur->student && cur->student->getName() == name) {
cur->student->displayInfo(); found = true;
}
cur = cur->next;
}
if (!found) std::cout << "No students found with that name.\n";
break;
}
case 5: {
int id = getIntInput("Enter ID to update: ");
updateStudent(head, id);
saveToFile(head, "students.csv");
break;
}
case 6: {
int id = getIntInput("Enter ID to delete: ");
deleteStudent(head, id);
saveToFile(head, "students.csv");
break;
}
case 7: {
saveToFile(head, "students.csv");
break;
}
case 8: {
// statistics
int total = 0;
float sumAll = 0.0f;
float maxGrade = -1e9f, minGrade = 1e9f;
Node* cur = head;
while (cur) {
if (cur->student) {
++total;
float avg = cur->student->calculateAverage();
sumAll += avg;
// per-student min/max over their grades
for (int i = 0; i < cur->student->getGradesCount(); ++i) {
float g = cur->student->getGrade(i);
if (g > maxGrade) maxGrade = g;
if (g < minGrade) minGrade = g;
}
}
cur = cur->next;
}
std::cout << "Total students: " << total << "\n";
if (total > 0) std::cout << "Average of student averages: " << (sumAll / total) << "\n";
if (maxGrade > -1e8f) std::cout << "Highest grade: " << maxGrade << "\n";
if (minGrade < 1e8f) std::cout << "Lowest grade: " << minGrade << "\n";
break;
}
case 9: {
std::cout << "Sort by: 1) ID 2) Name 3) Average\nChoice: ";
int c;
if (!(std::cin >> c)) { std::cin.clear(); std::cin.ignore(10000,'\n'); continue; }
std::cin.ignore(10000,'\n');
if (c == 1) sortByID(head);
else if (c == 2) sortByName(head);
else if (c == 3) sortByAverage(head);
else std::cout << "Unknown sort choice\n";
saveToFile(head, "students.csv");
break;
}
default: {
std::cout << "Invalid Input!";
break;
}
}
}
return 0;
}