This commit is contained in:
Weber 2024-03-22 00:32:24 +00:00
parent 6ff59c7d01
commit 86a5ea47e0

View File

@ -31,16 +31,6 @@ int main() {
char name[1024]; char name[1024];
sscanf(line, "%d %[^\n]", &vote_count, name); sscanf(line, "%d %[^\n]", &vote_count, name);
if (student_count >= students_capacity) {
students_capacity = (students_capacity + 1) * 2;
student_t *new_students = realloc(students, students_capacity * sizeof(student_t));
if (new_students == NULL) {
fprintf(stderr, "Error: Memory reallocation failed\n");
return 1;
}
students = new_students;
}
int existing_student_index = -1; int existing_student_index = -1;
for (int i = 0; i < student_count; i++) { for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].name, name) == 0) { if (strcmp(students[i].name, name) == 0) {
@ -52,9 +42,28 @@ int main() {
if (existing_student_index != -1) { if (existing_student_index != -1) {
students[existing_student_index].vote_count += vote_count; students[existing_student_index].vote_count += vote_count;
} else { } else {
if (student_count >= students_capacity) {
students_capacity = (students_capacity + 1) * 2;
student_t *new_students = realloc(students, students_capacity * sizeof(student_t));
if (new_students == NULL) {
fprintf(stderr, "Error: Memory reallocation failed\n");
return 1;
}
students = new_students;
}
students[student_count].name = strdup(name); students[student_count].name = strdup(name);
students[student_count].vote_count = vote_count; students[student_count].vote_count = vote_count;
student_count++; student_count++;
if (student_count == 1) {
printf("Vysledky:\n");
printf("%d %s\n", students[0].vote_count, students[0].name);
free(students[0].name);
free(students);
return 0;
}
} }
} }