From b6c18a05cefd95d910275f7613a3d9dcdfbad415 Mon Sep 17 00:00:00 2001 From: Mykyta Sadchenko Date: Wed, 20 Mar 2024 13:03:50 +0100 Subject: [PATCH] k --- cv5/program.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/cv5/program.c b/cv5/program.c index fce909c..7446a12 100644 --- a/cv5/program.c +++ b/cv5/program.c @@ -30,7 +30,7 @@ struct student { return result; }*/ -int read_input(int* votes, char* name, int size) { +/*int read_input(int* votes, char* name, int size) { char line[SIZE]; if (fgets(line, size, stdin) != NULL) { @@ -39,6 +39,24 @@ int read_input(int* votes, char* name, int size) { } } return 0; +}*/ + +int read_input(int* votes, char* name, int size) { + char line[SIZE]; + if (fgets(line, size, stdin) != NULL) { + + char* space = strchr(line, ' '); + if (space != NULL) { + *votes = atoi(line); + strcpy(name, space + 1); + + char* newline = strchr(name, '\n'); + if (newline) + *newline = '\0'; + return 1; + } + } + return 0; } @@ -110,7 +128,7 @@ int compare_students(const void* p1, const void* p2) { return 0; }*/ -int main() { +/*int main() { struct student database[SIZE]; int size = 0; @@ -129,5 +147,28 @@ int main() { } } + return 0; +}*/ +int main() { + struct student database[SIZE]; + int size = 0; + + int votes; + char name[SIZE]; + + + while (read_input(&votes, name, SIZE)) { + add_student(database, &size, name, votes); + } + + + qsort(database, size, sizeof(struct student), compare_students); + + + printf("Vysledky hlasovania:\n"); + for (int i = 0; i < size; ++i) { + printf("%d %s\n", database[i].votes, database[i].name); + } + return 0; }