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
| #include <iostream>
#include <string>
#include <fstream>
#include <limits>
#include <vector>
#include <algorithm>
#include <windows.h>
#undef max
using namespace std;
struct STUDENT {
int stud_id;
string name;
string group;
int grades[5];
int kolvo_ocenok;
STUDENT() : stud_id(0), name(""), group(""), kolvo_ocenok(0) {}
};
STUDENT kb_input() {
STUDENT student;
ofstream zapis_file("kbinput.txt", ios::app);
if (!zapis_file.is_open()) {
cerr << "Не удалось открыть файл kbinput.txt для записи.\n";
return student;
}
cout << "Введите ID студента: ";
cin >> student.stud_id;
zapis_file << "ID студента: " << student.stud_id << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Введите фамилию и инициалы студента: ";
getline(cin, student.name);
zapis_file << "ФИО: " << student.name << endl;
cout << "Введите название группы студента: ";
getline(cin, student.group);
zapis_file << "Название группы: " << student.group << endl;
cout << "Введите количество оценок в списке (не более 5): ";
cin >> student.kolvo_ocenok;
while (student.kolvo_ocenok < 0 || student.kolvo_ocenok > 5) {
cout << "Ошибка. Введите количество оценок от 0 до 5: ";
cin >> student.kolvo_ocenok;
}
cout << "Введите оценки (через пробел): ";
zapis_file << "Успеваемость: ";
for (int i = 0; i < student.kolvo_ocenok; ++i) {
cin >> student.grades[i];
while (student.grades[i] < 2 || student.grades[i] > 5) {
cout << "Ошибка. Введите оценку от '2' до '5': ";
cin >> student.grades[i];
}
zapis_file << student.grades[i] << " ";
}
zapis_file << endl;
zapis_file.close();
return student;
}
STUDENT file_input(ifstream& inputFile) {
STUDENT student;
inputFile >> student.stud_id;
inputFile.ignore(numeric_limits<streamsize>::max(), '\n');
getline(inputFile, student.name, ',');
getline(inputFile, student.group, ',');
inputFile >> student.kolvo_ocenok;
inputFile.ignore(numeric_limits<streamsize>::max(), '\n');
for (int i = 0; i < student.kolvo_ocenok; ++i) {
inputFile >> student.grades[i];
if (i < student.kolvo_ocenok - 1) {
inputFile.ignore(numeric_limits<streamsize>::max(), ',');
}
}
inputFile.ignore(numeric_limits<streamsize>::max(), '\n');
return student;
}
void save_to_file(const vector<STUDENT>& students, const string& filename) {
ofstream outputFile(filename);
if (outputFile.is_open()) {
for (const auto& student : students) {
outputFile << student.stud_id << endl;
outputFile << student.name << ",";
outputFile << student.group << ",";
outputFile << student.kolvo_ocenok << endl; // Save the number of grades
for (int i = 0; i < student.kolvo_ocenok; ++i) {
outputFile << student.grades[i];
if (i < student.kolvo_ocenok - 1) {
outputFile << ",";
}
}
outputFile << endl;
}
outputFile.close();
cout << "Данные сохранены успешно." << endl;
}
else {
cerr << "Не удалось открыть файл." << endl;
}
}
void print_stud(const STUDENT& student) {
cout << "ID студента: " << student.stud_id << endl;
cout << "ФИО студента: " << student.name << endl;
cout << "Название группы: " << student.group << endl;
cout << "Успеваемость: ";
for (int i = 0; i < student.kolvo_ocenok; ++i) { // Use numGrades
cout << student.grades[i] << " ";
}
cout << endl;
}
bool sravn_names(const STUDENT& a, const STUDENT& b) {
if (a.name == b.name) {
return a.name < b.name;
}
return a.name < b.name;
}
void add_stud(vector<STUDENT>& students) {
STUDENT newStudent = kb_input();
students.push_back(newStudent);
save_to_file(students, "stud.txt");
cout << "Студент добавлен.\n";
}
void edit_grades(STUDENT& student) {
cout << "Введите количество оценок в списке (не более 5): ";
cin >> student.kolvo_ocenok;
while (student.kolvo_ocenok < 0 || student.kolvo_ocenok > 5) {
cout << "Ошибка. Введите количество оценок от 0 до 5: ";
cin >> student.kolvo_ocenok;
}
cout << "Введите новые оценки: "; //через пробел
for (int i = 0; i < 5; ++i) {
cin >> student.grades[i];
while (student.grades[i] < 2 || student.grades[i] > 5) {
cout << "Ошибка. Введите оценку от '2' до '5': ";
cin >> student.grades[i];
}
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
void editstudent(vector<STUDENT>& students, int numberedit) {
bool found = false;
for (auto& student : students) {
if (student.stud_id == numberedit) {
cout << "Вы хотите изменить: \n";
cout << "1. ФИО\n";
cout << "2. Группа\n";
cout << "3. Успеваемость\n";
int choice;
cin >> choice;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
switch (choice) {
case 1:
cout << "Введите новую фамилию и инициалы студента: ";
getline(cin, student.name);
break;
case 2:
cout << "Введите название новой группы: ";
getline(cin, student.group);
break;
case 3:
edit_grades(student);
break;
default:
cout << "Некорректный выбор.\n";
}
cout << "Данные студента изменены.\n";
found = true;
save_to_file(students, "stud.txt");
break;
}
}
if (!found) {
cout << "Студент под номером " << numberedit << " не найден.\n";
}
}
void delete_stud(vector<STUDENT>& students, int deletenumber) {
bool found = false;
for (size_t i = 0; i < students.size(); ++i) {
if (students[i].stud_id == deletenumber) {
students.erase(students.begin() + i);
cout << "Студент под номером " << deletenumber << " удален.\n";
found = true;
save_to_file(students, "stud.txt");
break;
}
}
if (!found) {
cout << "Студент под номером " << deletenumber << " не найден.\n";
}
}
bool goodgrades_only(const STUDENT& student) {
for (int i = 0; i < student.kolvo_ocenok; ++i) {
if (student.grades[i] < 4) {
return false;
}
}
return true;
}
void goodstuds_only(const vector<STUDENT>& students) {
cout << "\nСтуденты с оценками '4' и '5':\n";
bool found = false;
for (const auto& student : students) {
if (goodgrades_only(student)) {
cout << "ID студента: " << student.stud_id << endl;
cout << "ФИО: " << student.name << ", группа: " << student.group << endl;
cout << "Список оценок: ";
for (int i = 0; i < 5; ++i) {
cout << student.grades[i] << " ";
}
cout << endl;
found = true;
}
}
if (!found) {
cout << "Студенты с оценками только '4' и '5' не найдены.\n";
}
}
void print_specialty(const vector<STUDENT>& students, const string& specialty) {
cout << "\nСтуденты по специальности " << specialty << ":\n";
bool found = false;
for (const auto& student : students) {
if (student.group.find(specialty) != string::npos) {
cout << "ID студента: " << student.stud_id << ". ФИО: " << student.name << ", группа: " << student.group << ". Список оценок: ";
for (int i = 0; i < 5; ++i) {
cout << student.grades[i] << " ";
}
cout << endl;
found = true;
}
}
if (!found) {
cout << "Студенты по специальности '" << specialty << "' не найдены.\n";
}
}
int main() {
#ifdef _WIN32
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
#endif
vector<STUDENT> students;
setlocale(LC_ALL, "RU");
string filename = "stud.txt";
cout << "Выберите способ работы с записями.\n";
cout << "1. Ввод с клавиатуры\n";
cout << "2. Взять данные из файла\n";
cout << "Введите номер операции: ";
int choice;
cin >> choice;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (choice == 1) {
int kolvo_students;
cout << "Введите количество студентов: ";
cin >> kolvo_students;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
for (int i = 0; i < kolvo_students; ++i) {
cout << "\nВвод данных для студента номер " << (i + 1) << ":\n";
students.push_back(kb_input());
}
}
else if (choice == 2) {
string filename = "stud.txt";
ifstream inputFile(filename);
if (inputFile.is_open()) {
while (inputFile.peek() != EOF) {
students.push_back(file_input(inputFile));
}
inputFile.close();
cout << "Данные из файла '" << filename << "' успешно загружены." << endl;
}
else {
cerr << "Не удалось открыть файл '" << filename << "'.";
return 1;
}
}
else {
cout << "Некорректный выбор.\n";
return 1;
}
int menu;
do {
cout << endl;
cout << "--- Меню ---" << endl;
cout << "1. Вывести список всех студентов\n";
cout << "2. Отсортировать список студентов по фамилии и инициалам\n";
cout << "3. Отредактировать данные студента\n";
cout << "4. Удалить студента из списка\n";
cout << "5. Добавить студента в список\n";
cout << "6. Вывести список студентов с оценками '4' и '5'\n";
cout << "7. Вывести список студентов по специальности\n" << endl;
cout << "Введите номер операции: ";
cin >> menu;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
switch (menu) {
case 1:
cout << "\nСписок студентов:\n";
for (const auto& student : students) {
print_stud(student);
}
break;
case 2:
sort(students.begin(), students.end(), sravn_names);
cout << "Список отсортирован.\n";
save_to_file(students, filename);
break;
case 3:
{
int numberedit;
cout << "Введите ID студента для редактирования: ";
cin >> numberedit;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
editstudent(students, numberedit);
save_to_file(students, filename);
break;
}
case 4:
{
int deletenumber;
cout << "Введите ID студента для удаления: ";
cin >> deletenumber;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
delete_stud(students, deletenumber);
break;
}
case 5:
cout << "Добавление нового студента:\n";
add_stud(students);
break;
case 6:
goodstuds_only(students);
break;
case 7: {
string specialty;
cout << "Введите название специальности: ";
getline(cin, specialty);
print_specialty(students, specialty);
break;
}
default:
cout << "Некорректный ввод.\n";
}
} while (menu != 0);
ofstream chistim("kbinput.txt");
chistim.close();
return 0;
} |