From 82c883e82a1c75b6b37e3cad7f2406024986e969 Mon Sep 17 00:00:00 2001 From: Weber Date: Fri, 22 Mar 2024 00:22:30 +0000 Subject: [PATCH] skuska --- cv5/program.c | 102 +++++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/cv5/program.c b/cv5/program.c index b16ad16..fd55e66 100644 --- a/cv5/program.c +++ b/cv5/program.c @@ -3,70 +3,70 @@ #include typedef struct student { - char *name; - int vote_count; + char *name; + int vote_count; } student_t; int compare_students(const void *a, const void *b) { - const student_t *student_a = *(const student_t **)a; - const student_t *student_b = *(const student_t **)b; + const student_t *student_a = (const student_t *)a; + const student_t *student_b = (const student_t *)b; - if (student_a->vote_count > student_b->vote_count) { - return -1; - } else if (student_a->vote_count < student_b->vote_count) { - return 1; - } + if (student_a->vote_count > student_b->vote_count) { + return -1; + } else if (student_a->vote_count < student_b->vote_count) { + return 1; + } - return strcmp(student_a->name, student_b->name); + return strcmp(student_a->name, student_b->name); } int main() { - student_t **students = NULL; - int students_capacity = 0; - int student_count = 0; + student_t *students = NULL; + int students_capacity = 0; + int student_count = 0; - char line[1024]; - while (fgets(line, sizeof(line), stdin)) { - int vote_count; - char name[1024]; - sscanf(line, "%d %[^\n]", &vote_count, name); + char line[1024]; - int found = 0; - for (int i = 0; i < student_count; i++) { - if (strcmp(students[i]->name, name) == 0) { - students[i]->vote_count += vote_count; - found = 1; - break; - } - } - if (!found) { - 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; - } + while (fgets(line, sizeof(line), stdin) != NULL) { + int vote_count; + char name[1024]; + sscanf(line, "%d %[^\n]", &vote_count, name); - students[student_count] = malloc(sizeof(student_t)); - students[student_count]->name = strdup(name); - students[student_count]->vote_count = vote_count; - student_count++; - } + char *prev_name = NULL; + int total_vote_count = 0; + + if (student_count > 0 && strcmp(students[student_count - 1].name, name) == 0) { + vote_count += students[student_count - 1].vote_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); - free(students[i]->name); - free(students[i]); + 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, "Chyba"); + return 1; + } + students = new_students; } - free(students); - return 0; -} + 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); + } + + for (int i = 0; i < student_count; i++) { + free(student_t.name); + } + free(students); + + return 0; +} \ No newline at end of file