55 lines
1.6 KiB
C
55 lines
1.6 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#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;
|
||
|
}
|
||
|
|