From 4bfd81f296367a26e381375f338153fac6309177 Mon Sep 17 00:00:00 2001 From: Matus Tokarcik Date: Thu, 21 Mar 2024 23:31:04 +0100 Subject: [PATCH] cv5 --- cv5/program.c | 61 ++++++++++++++++++++++++------------------------ cv5/program.exe | Bin 68836 -> 69470 bytes 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/cv5/program.c b/cv5/program.c index bb3d416..5d80a46 100644 --- a/cv5/program.c +++ b/cv5/program.c @@ -3,64 +3,64 @@ #include #include #define SIZE 100 +#define MY_ERROR_OUTPUT stderr // chybovy vystup struct student { char name[SIZE]; int votes; }; -// Porovnávacia funkcia pre qsort +// porovnavacia funkcia pre qsort int compare_students(const void *a, const void *b) { const struct student *student_a = (const struct student *)a; const struct student *student_b = (const struct student *)b; - if (student_a->votes == student_b->votes) { - return strcmp(student_a->name, student_b->name); + // ak maju rovnaky pocet hlasov, porovnam podla abecedy + if (student_a->votes != student_b->votes) { + return (student_b->votes - student_a->votes); } - return (student_b->votes - student_a->votes); + // inak porovnam podla poctu hlasov + return strcmp(student_a->name, student_b->name); } -// Definícia ukazovateľa na vlastný chybový výstup -#define MY_ERROR_OUTPUT stderr - -// Funkcia na kontrolu formátu riadku a načítanie údajov int check_and_parse_line(char *line, int *votes, char *name) { - // Nájdenie prvej medzery v reťazci + + // najdenie prvej medzery v retazci char *space_pos = strchr(line, ' '); - // Kontrola, či sa našla medzera + // kontrola, ci sa nasla medzera if (space_pos == NULL) { - return 0; // Neplatný formát riadku + return 0; // neplatny riadok } - // Konverzia počtu hlasov zo substringu reťazca do celého čísla + //string na cislo premena char *end; *votes = strtol(line, &end, 10); if (*votes == 0) { - return 0; // Konverzia sa nepodarila + return 0; // premena sa nepodarila } - // Nastavenie ukazovateľa name na miesto za prvou medzerou + // nastavenie ukazovatela name na miesto za prvou medzerou char *name_start = space_pos + 1; - // Načítanie mena - memset(name, 0, SIZE); // Vynulovanie pomocného poľa pre meno - int name_size = strlen(name_start) - 1; // Veľkosť mena (bez konca riadka) + // nacitanie mena + memset(name, 0, SIZE); // vynulovanie pomocneho pola pre meno + int name_size = strlen(name_start) - 1; // velkost mena ale uz bez konca riadka if (name_size > 0) { memcpy(name, name_start, name_size); } else { - return 0; // Nepodarilo sa načítať meno + return 0; // nepodarilo sa nacítat meno } - return 1; // Platný formát riadku + return 1; } -// Funkcia na vyhľadanie položky v databáze +// vyhladanie polozky v databaze int find_student(struct student *students, int size, const char *name) { for (int i = 0; i < size; i++) { if (strcmp(students[i].name, name) == 0) { - return i; // Ak nájde študenta + return i; // ak najde studenta } } return -1; @@ -70,11 +70,11 @@ int main() { struct student databaza[SIZE]; int size = 0; char line[SIZE]; - int votes; // Premenná pre počet hlasov - char name[SIZE]; // Pole pre meno + int votes; + char name[SIZE]; bool chyba = false; - // Načítanie a kontrola formátu vstupu + // nacítanie a kontrola formatu vstupu while (1) { char* r = fgets(line,SIZE,stdin); @@ -90,32 +90,31 @@ int main() { if (check_and_parse_line(line, &votes, name) == 0) { printf("Neplatny format"); - continue; // Preskoč tento riadok a načítaj ďalší + continue; // preskoci riadok a nacita dalsi } - // Kontrola, či sa študent už nachádza v databáze + // ci sa student nachadza v databaze int id = find_student(databaza, size, name); if (id < 0) { - // Ak študent nie je v databáze, pridaj ho + // Ak student nie je v databaze ta ho pridaj if (size < SIZE) { strcpy(databaza[size].name, name); databaza[size].votes = votes; size++; } else { printf("Databaza je plna\n"); - break; // Ukonči načítavanie, keď je databáza plná + break; // ked je databaza plna break } } else { - // Ak študent už existuje v databáze, aktualizuj počet hlasov + // ak je v databaze aktualizuj pocet hlasov databaza[id].votes += votes; } } - // Triedenie študentov pomocou qsort + // triedim studentov qsort(databaza, size, sizeof(struct student), compare_students); if (chyba == false){ - // Výpis výsledkov printf("Vysledky:\n"); for (int i = 0; i < size; i++) { printf("%d %s\n", databaza[i].votes, databaza[i].name); diff --git a/cv5/program.exe b/cv5/program.exe index cd048ef3920688862e266ed500f8b1fe7a63d21f..13371e3f88de61ab758c6e55077ad58dc04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