From 86a5ea47e0b6bbdb9affcaefcd15f56279f8a23f Mon Sep 17 00:00:00 2001 From: Weber Date: Fri, 22 Mar 2024 00:32:24 +0000 Subject: [PATCH] test --- cv5/program.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/cv5/program.c b/cv5/program.c index 0d53740..67acae3 100644 --- a/cv5/program.c +++ b/cv5/program.c @@ -31,16 +31,6 @@ int main() { char name[1024]; 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; for (int i = 0; i < student_count; i++) { if (strcmp(students[i].name, name) == 0) { @@ -52,9 +42,28 @@ int main() { if (existing_student_index != -1) { students[existing_student_index].vote_count += vote_count; } 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].vote_count = vote_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; + } } }