2024-03-13 09:55:11 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-03-20 14:57:31 +00:00
|
|
|
#define SIZE 100
|
2024-03-20 14:08:46 +00:00
|
|
|
|
2024-03-20 15:42:19 +00:00
|
|
|
// Definícia štruktúry pre uchovanie informácií o študentovi
|
2024-03-20 14:57:31 +00:00
|
|
|
struct student {
|
2024-03-20 15:42:19 +00:00
|
|
|
char name[SIZE];
|
2024-03-20 14:57:31 +00:00
|
|
|
int votes;
|
|
|
|
};
|
2024-03-13 09:55:11 +00:00
|
|
|
|
2024-03-20 15:42:19 +00:00
|
|
|
// Funkcia pre porovnanie dvoch záznamov
|
|
|
|
int compare(const void* p1, const void* p2){
|
|
|
|
struct student* s1 = (struct student*)p1;
|
|
|
|
struct student* s2 = (struct student*)p2;
|
2024-03-20 15:22:08 +00:00
|
|
|
|
2024-03-20 15:42:19 +00:00
|
|
|
// Porovnávanie podľa počtu hlasov, ak sú rovnaké, potom podľa abecedy
|
2024-03-20 15:22:08 +00:00
|
|
|
if (s1->votes != s2->votes) {
|
2024-03-20 15:27:29 +00:00
|
|
|
return s2->votes - s1->votes; // Zoradenie zostupne podľa počtu hlasov
|
2024-03-20 15:22:08 +00:00
|
|
|
} else {
|
2024-03-20 15:42:19 +00:00
|
|
|
return strcmp(s1->name, s2->name); // Zoradenie abecedne
|
2024-03-20 15:22:08 +00:00
|
|
|
}
|
2024-03-20 14:57:31 +00:00
|
|
|
}
|
2024-03-20 15:22:08 +00:00
|
|
|
|
|
|
|
int main() {
|
2024-03-20 15:42:19 +00:00
|
|
|
struct student database[SIZE]; // Databáza študentov
|
|
|
|
memset(database, 0, SIZE * sizeof(struct student)); // Inicializácia pamäte
|
|
|
|
int size = 0; // Aktuálny počet študentov v databáze
|
2024-03-20 15:27:29 +00:00
|
|
|
|
2024-03-20 15:56:39 +00:00
|
|
|
// Zoznam študentov s počtom hlasov
|
2024-03-20 15:46:13 +00:00
|
|
|
char input[][SIZE] = {
|
|
|
|
"2 Bardos Mrtakrys",
|
|
|
|
"1 Rita Umhi",
|
|
|
|
"1 Prylenn Alak",
|
|
|
|
"10 Lak'hi Elavorg",
|
|
|
|
"3 Prylenn Alak",
|
|
|
|
"3 Prylenn Alak",
|
|
|
|
"3 Prylenn Alak",
|
|
|
|
"1 Rita Umhi"
|
|
|
|
};
|
|
|
|
int input_size = sizeof(input) / sizeof(input[0]);
|
|
|
|
|
2024-03-20 15:56:39 +00:00
|
|
|
// Vytvorenie databázy študentov zo vstupných údajov
|
2024-03-20 15:46:13 +00:00
|
|
|
for (int i = 0; i < input_size; i++) {
|
2024-03-20 15:29:47 +00:00
|
|
|
int votes;
|
2024-03-20 15:42:19 +00:00
|
|
|
char name[SIZE];
|
2024-03-20 15:56:39 +00:00
|
|
|
if (sscanf(input[i], "%d %[^\n]", &votes, name) != 2) {
|
|
|
|
printf("Chyba pri načítaní vstupu!\n");
|
|
|
|
return 1;
|
2024-03-20 15:42:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-20 15:56:39 +00:00
|
|
|
// Pridanie študenta do databázy
|
|
|
|
strcpy(database[size].name, name);
|
|
|
|
database[size].votes = votes;
|
|
|
|
size++;
|
2024-03-20 15:22:08 +00:00
|
|
|
}
|
|
|
|
|
2024-03-20 15:42:19 +00:00
|
|
|
// Zoradenie databázy podľa počtu hlasov a abecedne
|
2024-03-20 15:22:08 +00:00
|
|
|
qsort(database, size, sizeof(struct student), compare);
|
|
|
|
|
2024-03-20 15:34:15 +00:00
|
|
|
// Výpis výsledkov
|
2024-03-20 15:46:13 +00:00
|
|
|
printf("Vysledky:\n");
|
2024-03-20 15:22:08 +00:00
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
printf("%d %s\n", database[i].votes, database[i].name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2024-03-13 09:55:11 +00:00
|
|
|
}
|
|
|
|
|