From 1b0f27ad83495e9824379f7c8c9db29cee27edad Mon Sep 17 00:00:00 2001 From: ov075wu Date: Thu, 20 Mar 2025 12:37:16 +0100 Subject: [PATCH] refresh --- du4/program.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/du4/program.c b/du4/program.c index a20528d..7efe6ac 100644 --- a/du4/program.c +++ b/du4/program.c @@ -14,9 +14,9 @@ int compare_students(const void *a, const void *b) { const Student *studentB = (const Student *)b; if (studentA->votes != studentB->votes) { - return studentB->votes - studentA->votes; // Сортировка по убыванию голосов + return studentB->votes - studentA->votes; } else { - return strcmp(studentA->name, studentB->name); // Лексикографическая сортировка по имени + return strcmp(studentA->name, studentB->name); } } @@ -30,10 +30,9 @@ int main() { char name[MAX_NAME_LENGTH]; if (sscanf(line, "%d %[^\n]", &votes, name) != 2) { - break; // Прерываем чтение при неверном формате - } + break; - // Поиск студента в массиве + int found = 0; for (int i = 0; i < student_count; i++) { if (strcmp(students[i].name, name) == 0) { @@ -43,12 +42,8 @@ int main() { } } - // Если студент не найден, добавляем нового - if (!found) { - strcpy(students[student_count].name, name); - students[student_count].votes = votes; - student_count++; - } + + } if (student_count == 0) { @@ -56,10 +51,10 @@ int main() { return 1; } - // Сортировка студентов + qsort(students, student_count, sizeof(Student), compare_students); - // Вывод результатов + printf("Vysledky:\n"); for (int i = 0; i < student_count; i++) { printf("%d %s\n", students[i].votes, students[i].name);