This commit is contained in:
Weber 2024-03-22 00:29:34 +00:00
parent 54e3f319a8
commit 6ff59c7d01

View File

@ -26,8 +26,7 @@ int main() {
int student_count = 0; int student_count = 0;
char line[1024]; char line[1024];
while (fgets(line, sizeof(line), stdin)) {
while (fgets(line, sizeof(line), stdin) != NULL) {
int vote_count; int vote_count;
char name[1024]; char name[1024];
sscanf(line, "%d %[^\n]", &vote_count, name); sscanf(line, "%d %[^\n]", &vote_count, name);
@ -36,21 +35,32 @@ int main() {
students_capacity = (students_capacity + 1) * 2; students_capacity = (students_capacity + 1) * 2;
student_t *new_students = realloc(students, students_capacity * sizeof(student_t)); student_t *new_students = realloc(students, students_capacity * sizeof(student_t));
if (new_students == NULL) { if (new_students == NULL) {
fprintf(stderr, "Chyba"); fprintf(stderr, "Error: Memory reallocation failed\n");
return 1; return 1;
} }
students = new_students; students = new_students;
} }
students[student_count].name = strdup(name); int existing_student_index = -1;
students[student_count].vote_count = vote_count; for (int i = 0; i < student_count; i++) {
student_count++; if (strcmp(students[i].name, name) == 0) {
existing_student_index = i;
break;
}
}
if (existing_student_index != -1) {
students[existing_student_index].vote_count += vote_count;
} else {
students[student_count].name = strdup(name);
students[student_count].vote_count = vote_count;
student_count++;
}
} }
qsort(students, student_count, sizeof(student_t), compare_students); qsort(students, student_count, sizeof(student_t), compare_students);
printf("Vysledky:\n"); printf("Vysledky:\n");
for (int i = 0; i < student_count; i++) { for (int i = 0; i < student_count; i++) {
printf("%d %s\n", students[i].vote_count, students[i].name); printf("%d %s\n", students[i].vote_count, students[i].name);
} }