From 1ee7eeaa4dcd1007b3c51525d559993de3f1d84d Mon Sep 17 00:00:00 2001 From: st529yr Date: Fri, 22 Mar 2024 09:11:34 +0100 Subject: [PATCH] funguje --- cv5/program.c | 79 --------------------------------------------------- 1 file changed, 79 deletions(-) delete mode 100644 cv5/program.c diff --git a/cv5/program.c b/cv5/program.c deleted file mode 100644 index 558c607..0000000 --- a/cv5/program.c +++ /dev/null @@ -1,79 +0,0 @@ -#include -#include -#include - -#define MAX_STUDENTS 100 -#define MAX_NAME_LENGTH 50 - -struct Student { - char name[MAX_NAME_LENGTH]; - int votes; -}; - -// Funkcia načíta študentov zo štandardného vstupu a sčíta ich hlasy -int read_and_sum_votes(struct Student students[], int max_students) { - int count = 0; - while (count < max_students) { - int votes; - char name[MAX_NAME_LENGTH]; - if (scanf("%d %[^\n]", &votes, name) != 2) { - break; // Koniec vstupu alebo chyba - } - - // Hľadáme, či už sme tento študent videli - int found = 0; - for (int i = 0; i < count; i++) { - if (strcmp(students[i].name, name) == 0) { - // Ak sme študenta našli, pridáme mu hlasy - students[i].votes += votes; - found = 1; - break; - } - } - - // Ak sme študenta ešte nevideli, pridáme ho do zoznamu - if (!found) { - strcpy(students[count].name, name); - students[count].votes = votes; - count++; - } - - // Zbavíme sa koncového znaku nového riadku z bufferu vstupu - getchar(); - } - return count; -} - -// Funkcia na porovnávanie študentov pre qsort -int compare(const void *p1, const void *p2) { - const struct Student *s1 = (const struct Student *)p1; - const struct Student *s2 = (const struct Student *)p2; - // Porovnanie podľa počtu hlasov, v prípade rovnakého počtu podľa mena - if (s1->votes != s2->votes) { - return s2->votes - s1->votes; - } else { - return strcmp(s1->name, s2->name); - } -} - -int main() { - struct Student students[MAX_STUDENTS]; - int count = read_and_sum_votes(students, MAX_STUDENTS); - - if (count == 0) { - printf("Nepodarilo nacitat nic\n"); - return 1; - } - - // Triedenie študentov - qsort(students, count, sizeof(struct Student), compare); - - // Výpis výsledkov - printf("Vysledky:\n"); - for (int i = 0; i < count; i++) { - printf("%d %s\n", students[i].votes, students[i].name); - } - - return 0; -} -