oooodchod
This commit is contained in:
parent
48b462db74
commit
7b4ae4dafd
@ -16,17 +16,34 @@ void initialize_database(struct student* database, int size, char* names[], int*
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pomocná funkcia na porovnanie dvoch študentov
|
||||||
|
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;
|
||||||
|
|
||||||
|
// Ak majú študenti rôzny počet hlasov, porovnáme ich podľa počtu hlasov
|
||||||
|
if (studentA->votes != studentB->votes) {
|
||||||
|
return studentB->votes - studentA->votes; // Zoradenie zostupne podľa počtu hlasov
|
||||||
|
} else {
|
||||||
|
// Ak majú rovnaký počet hlasov, porovnáme ich abecedne podľa mena
|
||||||
|
return strcmp(studentA->name, studentB->name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
struct student database[SIZE];
|
struct student database[SIZE];
|
||||||
memset(database, 0, SIZE * sizeof(struct student));
|
memset(database, 0, SIZE * sizeof(struct student));
|
||||||
int size = 9; // Veľkosť databázy mien študentov
|
int size = 10; // Veľkosť databázy mien študentov
|
||||||
char* names[9] = {"Bardos Mrtakrys", "Rita Umhi", "Prylenn Alak", "Lak'hi Elavorg", "Prylenn Alak", "Prylenn Alak", "Prylenn Alak", "Rita Umhi", "Terian Dis"};
|
char* names[10] = {"Bardos Mrtakrys", "Rita Umhi", "Prylenn Alak", "Lak'hi Elavorg", "Prylenn Alak", "Prylenn Alak", "Prylenn Alak", "Rita Umhi", "Terian Dis"};
|
||||||
int votes[9] = {2, 1, 1, 10, 3, 3, 3, 1, 10}; // Počet hlasov pre každého študenta
|
int votes[10] = {2, 1, 1, 10, 3, 3, 3, 1, 10}; // Počet hlasov pre každého študenta
|
||||||
|
|
||||||
initialize_database(database, size, names, votes); // Inicializácia databázy mien študentov
|
initialize_database(database, size, names, votes); // Inicializácia databázy mien študentov
|
||||||
|
|
||||||
// Výpis databázy
|
// Zoradenie databázy študentov podľa počtu hlasov a abecedne
|
||||||
printf("Databaza mien studentov s poctom hlasov:\n");
|
qsort(database, size, sizeof(struct student), compare_students);
|
||||||
|
|
||||||
|
// Výpis zoradenej databázy
|
||||||
|
printf("Zoradeny zoznam studentov podla poctu hlasov a abecedne:\n");
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
printf("%d %s\n", database[i].votes, database[i].name);
|
printf("%d %s\n", database[i].votes, database[i].name);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user