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