-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentRepository.cpp
More file actions
276 lines (236 loc) · 7.75 KB
/
Copy pathStudentRepository.cpp
File metadata and controls
276 lines (236 loc) · 7.75 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
// Project files
#include "StudentRepository.h"
#include "InputUtils.h"
// Defauld models
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <limits>
// utitlities
static void ignoreLine() {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
Node* findNodeById(Node* head, int id) {
Node* cur = head;
while (cur) {
if (cur->student && cur->student->getID() == id) {
return cur;
}
cur = cur->next;
}
return nullptr;
}
Node* findNodePrevByID(Node* head, int id) {
Node* prev = nullptr;
Node* cur = head;
while (cur)
{
if (cur->student && cur->student->getID() == id) { return prev; }
prev = cur;
cur = cur->next;
}
return nullptr;
}
Student* findStudentByID(Node* head, int id) {
Node* n = findNodeById(head, id);
if (!n) { return nullptr;}
return n->student;
}
// CRUD (Create, Read, Update, Delete)
void addStudent(Node*& head) {
std::string name = getStringInput("Enter name: ");
int id = getIntInput("Enter ID (Integer): ");
if (findStudentByID(head, id)) {
std::cout << "A student with this ID already exists.\n";
return;
}
int age = getIntInput("Enter age: ");
int gradesCount = getIntInput("Number of grades: ");
if (gradesCount < 0) gradesCount = 0;
Student* s = new Student(name, id, age, gradesCount);
for (int i = 0; i < gradesCount; ++i) {
float g = getFloatInput(("Grade #" + std::to_string(i+1) + ":").c_str());
s->setGrade(i, g);
}
Node* node = new Node(s);
if (!head) {
head = node;
} else {
Node* cur = head;
while (cur->next)
{
cur = cur->next;
}
cur->next = node;
}
std::cout << "Student added.\n";
}
// Information students
void displayStudents(Node* head) {
if (!head) {
std::cout << "List is empty\n";
return;
}
std::cout << "Students:\n";
Node* cur = head;
while (cur) {
if (cur->student) cur->student->displayInfo();
cur = cur->next;
}
}
// Get student by id and update fields
void updateStudent(Node* head, int id) {
Student* s = findStudentByID(head, id);
if (!s) {
std::cout << "Student not found.\n";
return;
}
std::cout << "Updating student (current values):\n";
s->displayInfo();
// цикл выбора поля для обновления (без раннего return)
while (true) {
std::cout << "\nChoose field to update:\n";
std::cout << "1. Name\n2. Age\n3. Grades\n0. Finish\nChoice: ";
int ch;
if (!(std::cin >> ch)) {
std::cin.clear();
ignoreLine();
ch = -1;
} else ignoreLine();
if (ch == 0) break;
// получаем указатель на узел, чтобы заменить student* при необходимости
Node* n = findNodeById(head, id);
if (!n) {
std::cout << "Unexpected: node not found.\n";
return;
}
switch (ch)
{
case 1: {
std::string newName = getStringInput("New name: ");
if (newName.empty()) { std::cout << "Name unchanged.\n"; continue; }
int age = s->getAge();
int gcount = s->getGradesCount();
Student* newS = new Student(newName, s->getID(), age, gcount);
for (int i = 0; i < gcount; ++i) newS->setGrade(i, s->getGrade(i));
delete n->student; // удаляем старый объект
n->student = newS; // ставим новый
s = newS; // продолжаем работать с новым объектом
std::cout << "Name updated.\n";
}
case 2: {
int newAge = getIntInput("New age: ");
std::string name = s->getName();
int gcount = s->getGradesCount();
Student* newS = new Student(name, s->getID(), newAge, gcount);
for (int i = 0; i < gcount; ++i) newS->setGrade(i, s->getGrade(i));
delete n->student;
n->student = newS;
s = newS;
std::cout << "Age updated.\n";
}
case 3: {
// Baholar qoshiladi lekin ozgartirilmaydi
int newCount = getIntInput("New grades count: ");
if (newCount < 0) newCount = 0;
std::string name = s->getName();
int age = s->getAge();
Student* newS = new Student(name, s->getID(), age, newCount);
int copyCount = std::min(s->getGradesCount(), newCount);
for (int i = 0; i < copyCount; ++i) newS->setGrade(i, s->getGrade(i));
for (int i = copyCount; i < newCount; ++i) {
float g = getFloatInput(("Grade #" + std::to_string(i+1) + ": ").c_str());
newS->setGrade(i, g);
}
delete n->student;
n->student = newS;
s = newS;
std::cout << "Grades updated.\n";
}
default: {
std::cout << "Unknown choice\n";
}
}
}
std::cout << "Update finished.\n";
}
// Delete student by id
void deleteStudent(Node*& head, int id) {
if (!head) {std::cout << "List empty.\n"; return; }
Node* prev = nullptr;
Node* cur = head;
while (cur)
{
if (cur->student && cur->student->getID() == id) break;
prev = cur;
cur = cur->next;
}
if (!cur) { std:: cout << "Student not found.\n"; return; }
if (prev == nullptr) {
head = cur->next;
} else {
prev->next = cur->next;
}
delete cur->student;
delete cur;
std::cout << "Student deleted.\n";
}
// Clear list of students
void clearList(Node*& head) {
Node* cur = head;
while (cur) {
Node* tmp = cur;
cur = cur->next;
delete tmp->student;
delete tmp;
}
head = nullptr;
}
// convrt list -> vector, sort, rebuild
static std::vector<Student*> listToVector(Node* head) {
std::vector<Student*> v;
Node* cur = head;
while (cur) {
if (cur->student) v.push_back(cur->student);
cur = cur->next;
}
return v;
}
static Node* vectorToList(const std::vector<Student*>& v) {
Node* newHead = nullptr;
Node* tail = nullptr;
for (Student* s : v) {
Node* node = new Node(s);
if (!newHead) newHead = node;
else tail->next = node;
tail = node;
}
return newHead;
}
void sortByID(Node*& head) {
auto v = listToVector(head);
std::sort(v.begin(), v.end(), [](Student* a, Student* b){ return a->getID() < b->getID(); });
// free old node wrappers but keep Student* pointers
Node* cur = head;
while (cur) { Node* tmp = cur; cur = cur->next; delete tmp; }
head = vectorToList(v);
std::cout << "Sorted by ID ascending.\n";
}
void sortByName(Node*& head) {
auto v = listToVector(head);
std::sort(v.begin(), v.end(), [](Student* a, Student* b){ return a->getName() < b->getName(); });
Node* cur = head;
while (cur) { Node* tmp = cur; cur = cur->next; delete tmp; }
head = vectorToList(v);
std::cout << "Sorted by name (A-Z).\n";
}
void sortByAverage(Node*& head) {
auto v = listToVector(head);
std::sort(v.begin(), v.end(), [](Student* a, Student* b){ return a->calculateAverage() > b->calculateAverage(); });
Node* cur = head;
while (cur) { Node* tmp = cur; cur = cur->next; delete tmp; }
head = vectorToList(v);
std::cout << "Sorted by average (descending).\n";
}