pvjc25/du4/program.c
2025-03-20 12:37:16 +01:00

65 lines
1.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 100
typedef struct {
char name[MAX_NAME_LENGTH];
int votes;
} Student;
int compare_students(const void *a, const void *b) {
const Student *studentA = (const Student *)a;
const Student *studentB = (const Student *)b;
if (studentA->votes != studentB->votes) {
return studentB->votes - studentA->votes;
} else {
return strcmp(studentA->name, studentB->name);
}
}
int main() {
Student students[100];
int student_count = 0;
char line[256];
while (fgets(line, sizeof(line), stdin)) {
int votes;
char name[MAX_NAME_LENGTH];
if (sscanf(line, "%d %[^\n]", &votes, name) != 2) {
break;
int found = 0;
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].name, name) == 0) {
students[i].votes += votes;
found = 1;
break;
}
}
}
if (student_count == 0) {
printf("Chyba: nepodarilo sa nacitat ziadny zaznam.\n");
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);
}
return 0;
}