From 2ee3f9b48120aea5fbb2e5eb0c5c06fb5a2e37e3 Mon Sep 17 00:00:00 2001 From: Mykyta Sadchenko Date: Wed, 20 Mar 2024 12:55:44 +0100 Subject: [PATCH] g --- cv5/program.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/cv5/program.c b/cv5/program.c index bba61b1..fce909c 100644 --- a/cv5/program.c +++ b/cv5/program.c @@ -21,13 +21,24 @@ struct student { }*/ -char* read_line(char* buffer, int size) { +/*char* read_line(char* buffer, int size) { char* result = fgets(buffer, size, stdin); if (result) { buffer[strcspn(buffer, "\n")] = '\0'; } return result; +}*/ + +int read_input(int* votes, char* name, int size) { + char line[SIZE]; + if (fgets(line, size, stdin) != NULL) { + + if (sscanf(line, "%d %s", votes, name) == 2) { + return 1; + } + } + return 0; } @@ -99,3 +110,24 @@ int compare_students(const void* p1, const void* p2) { 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; +}