From b89f6695bf2d3db3360f6e4a2fc77f919444ce96 Mon Sep 17 00:00:00 2001 From: Patrik Seman Date: Sat, 25 Mar 2023 11:57:05 +0000 Subject: [PATCH] Update 'du5/program.c' --- du5/program.c | 173 +++++++++++++++++++++++++------------------------- 1 file changed, 87 insertions(+), 86 deletions(-) diff --git a/du5/program.c b/du5/program.c index eab5343..2fc7162 100644 --- a/du5/program.c +++ b/du5/program.c @@ -1,87 +1,88 @@ -#include -#include -#include - -#define SIZE 100 - -struct student{ - int votes; - char name[SIZE]; -}; - -int search_index(int size, const struct student list[SIZE], const char student[SIZE]){ - for(int x = 0; x < size; x++){ - if(strcmp(list[x].name, student) == 0){ - return x; - } - } - - return -1; -} - -int sort_by_votes(const void* p1, const void* p2){ - struct student* student1 = (struct student*) p1; - struct student* student2 = (struct student*) p2; - - if(student1->votes != student2->votes){ - if(student1->votes < student2->votes){ - return 1; - }else{ - return -1; - } - }else{ - return strcmp(student1->name, student2->name); - } -} - -int main(){ - int votes; - char name[SIZE]; - char line[SIZE]; - char *end; - int counter = 0; - - struct student list[SIZE]; - - while(fgets(line, SIZE, stdin) != NULL){ - if(line[0] == '\n' || !isdigit(line[0])){ - break; - } - - votes = strtol(line, &end, 10); - - if(end[0] != ' ' || !isalpha(end[1])){ - break; - } - - if(end[strlen(end) - 1] == '\n'){ - end[strlen(end) - 1] = '\0'; - } - - strcpy(name, end + 1); - - int stud_index = search_index(counter, list, name); - - if(stud_index == -1){ - counter++; - - list[counter - 1].votes = votes; - strcpy(list[counter - 1].name, name); - }else{ - list[stud_index].votes += votes; - } - } - - if(counter == 0){ - printf("Nepodarilo sa nacitat ziaden vstup\n"); - return 0; - } - - qsort(list, counter, sizeof(list[0]), sort_by_votes); - - printf("Vysledky:\n"); - - for(int x = 0; x < counter; x++){ - printf("%d %s\n", list[x].votes, list[x].name); - } +#include +#include +#include +#include + +#define SIZE 100 + +struct student{ + int votes; + char name[SIZE]; +}; + +int search_index(int size, const struct student list[SIZE], const char student[SIZE]){ + for(int x = 0; x < size; x++){ + if(strcmp(list[x].name, student) == 0){ + return x; + } + } + + return -1; +} + +int sort_by_votes(const void* p1, const void* p2){ + struct student* student1 = (struct student*) p1; + struct student* student2 = (struct student*) p2; + + if(student1->votes != student2->votes){ + if(student1->votes < student2->votes){ + return 1; + }else{ + return -1; + } + }else{ + return strcmp(student1->name, student2->name); + } +} + +int main(){ + int votes; + char name[SIZE]; + char line[SIZE]; + char *end; + int counter = 0; + + struct student list[SIZE]; + + while(fgets(line, SIZE, stdin) != NULL){ + if(line[0] == '\n' || !isdigit(line[0])){ + break; + } + + votes = strtol(line, &end, 10); + + if(end[0] != ' ' || !isalpha(end[1])){ + break; + } + + if(end[strlen(end) - 1] == '\n'){ + end[strlen(end) - 1] = '\0'; + } + + strcpy(name, end + 1); + + int stud_index = search_index(counter, list, name); + + if(stud_index == -1){ + counter++; + + list[counter - 1].votes = votes; + strcpy(list[counter - 1].name, name); + }else{ + list[stud_index].votes += votes; + } + } + + if(counter == 0){ + printf("Nepodarilo sa nacitat ziaden vstup\n"); + return 0; + } + + qsort(list, counter, sizeof(list[0]), sort_by_votes); + + printf("Vysledky:\n"); + + for(int x = 0; x < counter; x++){ + printf("%d %s\n", list[x].votes, list[x].name); + } } \ No newline at end of file