54 lines
1.5 KiB
C
54 lines
1.5 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] = {
|
|
{"Bardos Mrtakrys", 2},
|
|
{"Rita Umhi", 1},
|
|
{"Prylenn Alak", 1},
|
|
{"Lak'hi Elavorg", 10},
|
|
{"Prylenn Alak", 3},
|
|
{"Prylenn Alak", 3},
|
|
{"Prylenn Alak", 3},
|
|
{"Rita Umhi", 1}
|
|
};
|
|
int num_students = 8; // 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;
|
|
}
|
|
|