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:46:13 +00:00
|
|
|
// Pevne definovaný zoznam študentov s počtom hlasov
|
|
|
|
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]);
|
|
|
|
|
|
|
|
// Spracovanie hlasov
|
|
|
|
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:46:13 +00:00
|
|
|
sscanf(input[i], "%d %[^\n]", &votes, name);
|
2024-03-20 15:29:47 +00:00
|
|
|
|
2024-03-20 15:42:19 +00:00
|
|
|
// Hľadanie študenta v databáze
|
|
|
|
int index = -1;
|
2024-03-20 15:46:13 +00:00
|
|
|
for (int j = 0; j < size; j++) {
|
|
|
|
if (strcmp(database[j].name, name) == 0) {
|
|
|
|
index = j;
|
2024-03-20 15:42:19 +00:00
|
|
|
break;
|
2024-03-20 15:22:08 +00:00
|
|
|
}
|
2024-03-20 15:42:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Aktualizácia databázy
|
|
|
|
if (index == -1) { // Študent ešte nie je v databáze
|
2024-03-20 15:22:08 +00:00
|
|
|
strcpy(database[size].name, name);
|
|
|
|
database[size].votes = votes;
|
|
|
|
size++;
|
2024-03-20 15:42:19 +00:00
|
|
|
} else { // Študent je už v databáze
|
|
|
|
database[index].votes += votes;
|
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
|
|
|
}
|
|
|
|
|