From eeb15f099de4c940722e4c087d980cc0d5137ad5 Mon Sep 17 00:00:00 2001 From: Bohdana Marchenko Date: Fri, 21 Mar 2025 11:33:36 +0000 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20du4/program.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- du4/program.c | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/du4/program.c b/du4/program.c index 8e3d014..8e65da9 100644 --- a/du4/program.c +++ b/du4/program.c @@ -2,34 +2,32 @@ #include #include -#define SIZE 100 // Maximálny počet študentov a dĺžka mena +#define SIZE 100 struct student { - char name[SIZE]; // Meno študenta - int votes; // Počet hlasov pre študenta + char name[SIZE]; + int votes; }; -struct student database[SIZE]; // Databáza študentov +struct student database[SIZE]; int size = 0; -// Funkcia na nájdenie študenta podľa mena v databáze 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) { // Porovnanie mien, či sú rovnaké + if (strcmp(students[i].name, name) == 0) { return i; } } return -1; } -// Porovnávacia funkcia pre qsort na zoradenie študentov int compare(const void *p1, const void *p2) { struct student *s1 = (struct student *)p1; struct student *s2 = (struct student *)p2; if (s2->votes != s1->votes) { - return s2->votes - s1->votes; // Triedenie zostupne podľa hlasov + return s2->votes - s1->votes; } - return strcmp(s1->name, s2->name); // Triedenie lexikograficky, ak sú hlasy rovnaké + return strcmp(s1->name, s2->name); } int main() { @@ -39,10 +37,9 @@ int main() { char *end = NULL; int votes = strtol(line, &end, 10); if (end == line || *end == '\0' || votes <= 0) { - break; // Neplatný vstup, ukončiť načítanie + break; } - // Preskočiť medzeru po čísle hlasov while (*end == ' ') { end++; } @@ -50,33 +47,33 @@ int main() { char name[SIZE]; memset(name, 0, SIZE); int name_len = strlen(end); - if (name_len > 0 && end[name_len - 1] == '\n') { // Odstrániť nový riadok na konci + if (name_len > 0 && end[name_len - 1] == '\n') { end[name_len - 1] = '\0'; } strcpy(name, end); - int id = find_student(database, size, name); // Hľadanie študenta v databáze - if (id < 0) { // Nový študent - if (size < SIZE) { // Kontrola, či je miesto v databáze - strcpy(database[size].name, name); // Uloženie mena nového študenta - database[size].votes = votes; // Nastavenie hlasov pre nového študenta + int id = find_student(database, size, name); + if (id < 0) { + if (size < SIZE) { + strcpy(database[size].name, name); + database[size].votes = votes; size++; } } else { - database[id].votes += votes; // Pridanie hlasov existujúcemu študentovi + database[id].votes += votes; } } - qsort(database, size, sizeof(struct student), compare); // Zoradenie databázy podľa hlasov a mien + qsort(database, size, sizeof(struct student), compare); if (size > 0) { printf("Vysledky:\n"); for (int i = 0; i < size; i++) { - printf("%d %s\n", database[i].votes, database[i].name); // Výpis výsledkov + printf("%d %s\n", database[i].votes, database[i].name); } } else { - printf("Nepodarilo nacitat nic\n"); // Správa, ak neboli načítané žiadne údaje + printf("Nepodarilo nacitat nic\n"); } return 0;