From 0d6e6aadd29062305813921061ec9e0770f350ac Mon Sep 17 00:00:00 2001 From: VIliam Date: Fri, 22 Mar 2024 00:13:18 +0100 Subject: [PATCH] cv5 --- cv5/program.c | 60 ++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/cv5/program.c b/cv5/program.c index 6bbca35..0b4b67c 100644 --- a/cv5/program.c +++ b/cv5/program.c @@ -15,32 +15,26 @@ int compare(const void* p1, const void* p2) { const struct student* s2 = (const struct student*)p2; // Porovnanie podľa počtu hlasov - return s2->votes - s1->votes; -} - -// Funkcia na vyhľadanie študenta 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) { - return i; // Nájdený študent - } + int result = s2->votes - s1->votes; + if (result == 0) { + // Ak majú rovnaký počet hlasov, zoradíme lexikograficky podľa mena + return strcmp(s1->name, s2->name); } - return -1; // Študent nenájdený + return result; } int main() { - struct student database[SIZE]; - memset(database, 0, SIZE * sizeof(struct student)); + struct student students[SIZE]; + memset(students, 0, SIZE * sizeof(struct student)); int size = 0; char line[SIZE]; char name[SIZE]; int votes; - while (1) { - printf("Enter votes and student name (or 'q' to quit): "); - fgets(line, SIZE, stdin); - + printf("Enter votes for 'Student of the Year' contest (or 'q' to quit):\n"); + + while (fgets(line, SIZE, stdin) != NULL) { // Kontrola ukončenia vstupu if (strcmp(line, "q\n") == 0 || strcmp(line, "Q\n") == 0) { break; @@ -48,34 +42,42 @@ int main() { // Načítanie počtu hlasov a mena zo vstupu if (sscanf(line, "%d %[^\n]", &votes, name) != 2) { - printf("Invalid input. Please try again.\n"); - continue; + printf("Invalid input format. Exiting.\n"); + return 1; } // Vyhľadanie študenta v databáze - int index = find_student(database, size, name); + int index = -1; + for (int i = 0; i < size; i++) { + if (strcmp(students[i].name, name) == 0) { + index = i; + break; + } + } + + // Ak študent nie je v databáze, pridáme ho if (index == -1) { - // Študent nenájdený, pridáme ho do databázy if (size < SIZE) { - strcpy(database[size].name, name); - database[size].votes = votes; + strcpy(students[size].name, name); + students[size].votes = votes; size++; } else { - printf("Database is full.\n"); + printf("Database is full. Exiting.\n"); + return 1; } } else { - // Študent nájdený, zvýšime počet hlasov - database[index].votes += votes; + // Ak študent už existuje, zvýšime počet hlasov + students[index].votes += votes; } } - // Triedenie databázy podľa počtu hlasov - qsort(database, size, sizeof(struct student), compare); + // Triedenie databázy podľa počtu hlasov a mena + qsort(students, size, sizeof(struct student), compare); - // Výpis triedenej databázy + // Výpis výsledkov printf("\nResults:\n"); for (int i = 0; i < size; i++) { - printf("%d %s\n", database[i].votes, database[i].name); + printf("%d %s\n", students[i].votes, students[i].name); } return 0;