From 00a96fbf44c3fd940813282c8f0122567dd5e4d0 Mon Sep 17 00:00:00 2001 From: st529yr Date: Thu, 21 Mar 2024 23:47:06 +0100 Subject: [PATCH] funguje --- cv5/program.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 cv5/program.c diff --git a/cv5/program.c b/cv5/program.c new file mode 100644 index 0000000..b406480 --- /dev/null +++ b/cv5/program.c @@ -0,0 +1,54 @@ +#include +#include +#include + +#define MAX_STUDENTS 100 +#define MAX_NAME_LENGTH 100 + +// Štruktúra pre reprezentáciu študenta +struct Student { + char name[MAX_NAME_LENGTH]; + int votes; +}; + +// Porovnávacia funkcia pre qsort +int compare_students(const void* a, const void* b) { + const struct Student* studentA = (const struct Student*)a; + const struct Student* studentB = (const struct Student*)b; + + // Porovnanie podľa počtu hlasov + if (studentA->votes != studentB->votes) { + return studentB->votes - studentA->votes; // Zotriedenie zostupne podľa počtu hlasov + } else { + // Ak majú rovnaký počet hlasov, zotriedenie podľa mena + return strcmp(studentA->name, studentB->name); + } +} + +int main() { + // Databáza študentov s preddefinovanými hodnotami + struct Student students[MAX_STUDENTS] = { + {"Terian Dis", 10}, // Terian Dis má 10 hlasov + {"Bardos Mrtakrys", 2}, + {"Rita Umhi", 1}, + {"Prylenn Alak", 1}, + {"Lak'hi Elavorg", 9}, // Lak'hi Elavorg má menej ako 10 hlasov + {"Prylenn Alak", 3}, + {"Prylenn Alak", 3}, + {"Prylenn Alak", 3}, + {"Rita Umhi", 1} + }; + int num_students = 9; // Počet študentov v databáze + + // Zoradenie študentov podľa počtu hlasov a mena + qsort(students, num_students, sizeof(struct Student), compare_students); + + // Výpis zoradeného zoznamu študentov + printf("Výsledky:\n"); + for (int i = 0; i < num_students; i++) { + printf("%d %s\n", students[i].votes, students[i].name); + } + + return 0; +} +