Aktualizovat du4/program.c

This commit is contained in:
Denys Sanchuk 2025-03-11 13:51:40 +00:00
parent afea69cade
commit 59a36a80be

View File

@ -5,9 +5,10 @@
#define MAX_LEN 256 #define MAX_LEN 256
#define MAX_NAME_LEN 100 #define MAX_NAME_LEN 100
// Структура для хранения информации о студенте
typedef struct { typedef struct {
char name[MAX_NAME_LEN]; char name[MAX_NAME_LEN]; // имя студента
int votes; int votes; // количество голосов
} Student; } Student;
int compare_students(const void *a, const void *b) { int compare_students(const void *a, const void *b) {
@ -23,8 +24,7 @@ int compare_students(const void *a, const void *b) {
int main() { int main() {
Student students[MAX_LEN]; Student students[MAX_LEN];
int student_count = 0; int student_count = 0;
char line[MAX_LEN]; char line[MAX_LEN];
while (fgets(line, MAX_LEN, stdin)) { while (fgets(line, MAX_LEN, stdin)) {
@ -60,7 +60,9 @@ int main() {
} }
} }
printf("Vysledky:\n"); qsort(students, student_count, sizeof(Student), compare_students);
printf("Результаты:\n");
for (int i = 0; i < student_count; i++) { for (int i = 0; i < student_count; i++) {
printf("%d %s\n", students[i].votes, students[i].name); printf("%d %s\n", students[i].votes, students[i].name);
} }