pvjc24/cv5/program.c

55 lines
1.6 KiB
C
Raw Normal View History

2024-03-13 09:55:11 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2024-03-20 17:02:19 +00:00
#define MAX_STUDENTS 100
#define MAX_NAME_LENGTH 100
2024-03-20 14:08:46 +00:00
2024-03-20 17:02:19 +00:00
// Štruktúra pre reprezentáciu študenta
struct Student {
char name[MAX_NAME_LENGTH];
2024-03-20 14:57:31 +00:00
int votes;
};
2024-03-13 09:55:11 +00:00
2024-03-20 17:02:19 +00:00
// Porovnávacia funkcia pre qsort
2024-03-20 16:52:10 +00:00
int compare_students(const void* a, const void* b) {
2024-03-20 17:02:19 +00:00
const struct Student* studentA = (const struct Student*)a;
const struct Student* studentB = (const struct Student*)b;
2024-03-20 16:52:10 +00:00
2024-03-20 17:02:19 +00:00
// Porovnanie podľa počtu hlasov
2024-03-20 16:52:10 +00:00
if (studentA->votes != studentB->votes) {
2024-03-20 17:02:19 +00:00
return studentB->votes - studentA->votes; // Zotriedenie zostupne podľa počtu hlasov
2024-03-20 16:52:10 +00:00
} else {
2024-03-20 17:02:19 +00:00
// Ak majú rovnaký počet hlasov, zotriedenie podľa mena
2024-03-20 16:52:10 +00:00
return strcmp(studentA->name, studentB->name);
}
}
2024-03-20 15:22:08 +00:00
int main() {
2024-03-20 17:02:19 +00:00
// Databáza študentov s preddefinovanými hodnotami
struct Student students[MAX_STUDENTS] = {
2024-03-20 17:06:29 +00:00
{"Terian Dis", 10}, // Terian Dis má 10 hlasov
2024-03-20 17:02:19 +00:00
{"Bardos Mrtakrys", 2},
{"Rita Umhi", 1},
{"Prylenn Alak", 1},
2024-03-20 17:06:29 +00:00
{"Lak'hi Elavorg", 9}, // Lak'hi Elavorg má menej ako 10 hlasov
2024-03-20 17:02:19 +00:00
{"Prylenn Alak", 3},
{"Prylenn Alak", 3},
{"Prylenn Alak", 3},
{"Rita Umhi", 1}
};
2024-03-20 17:06:29 +00:00
int num_students = 9; // Počet študentov v databáze
2024-03-20 17:02:19 +00:00
// 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);
2024-03-20 16:58:22 +00:00
}
2024-03-20 15:22:08 +00:00
return 0;
2024-03-13 09:55:11 +00:00
}