2024-03-13 09:55:11 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-03-20 12:59:55 +00:00
|
|
|
#define MAX_NAME_LENGTH 100
|
2024-03-20 14:08:46 +00:00
|
|
|
#define MAX_BUFFER_LENGTH 256
|
2024-03-20 12:59:55 +00:00
|
|
|
|
2024-03-20 14:08:46 +00:00
|
|
|
// Štruktúra pre uchovanie informácií o študentovi
|
2024-03-13 09:55:11 +00:00
|
|
|
typedef struct {
|
2024-03-20 14:08:46 +00:00
|
|
|
char name[MAX_NAME_LENGTH];
|
2024-03-13 09:55:11 +00:00
|
|
|
int votes;
|
2024-03-20 12:33:29 +00:00
|
|
|
} Student;
|
2024-03-13 09:55:11 +00:00
|
|
|
|
2024-03-20 14:08:46 +00:00
|
|
|
// Porovnávacia funkcia pre zoradenie študentov podľa počtu hlasov a mena
|
2024-03-20 12:33:29 +00:00
|
|
|
int compare_students(const void *a, const void *b) {
|
2024-03-20 14:08:46 +00:00
|
|
|
const Student *student_a = (const Student *)a;
|
|
|
|
const Student *student_b = (const Student *)b;
|
|
|
|
|
|
|
|
// Najprv porovnajte počet hlasov
|
|
|
|
if (student_a->votes != student_b->votes) {
|
|
|
|
return student_b->votes - student_a->votes; // Zoradiť zostupne
|
2024-03-13 09:55:11 +00:00
|
|
|
}
|
2024-03-20 14:08:46 +00:00
|
|
|
|
|
|
|
// Ak majú rovnaký počet hlasov, porovnajte podľa mena
|
|
|
|
return strcmp(student_a->name, student_b->name);
|
2024-03-13 09:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2024-03-20 14:08:46 +00:00
|
|
|
// Inicializácia poľa študentov
|
|
|
|
Student students[MAX_BUFFER_LENGTH];
|
|
|
|
int num_students = 0;
|
2024-03-13 09:55:11 +00:00
|
|
|
|
2024-03-20 14:08:46 +00:00
|
|
|
// Načítanie hlasov zo štandardného vstupu
|
|
|
|
char buffer[MAX_BUFFER_LENGTH];
|
|
|
|
while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
|
|
|
|
// Načítajte počet hlasov a meno študenta
|
2024-03-20 14:34:37 +00:00
|
|
|
char *token = strtok(buffer, "\x20");
|
2024-03-20 14:28:37 +00:00
|
|
|
if (token == NULL) {
|
|
|
|
fprintf(stderr, "Chyba: Neplatný formát vstupu.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int votes = atoi(token);
|
|
|
|
|
|
|
|
token = strtok(NULL, "\n");
|
|
|
|
if (token == NULL) {
|
2024-03-20 14:08:46 +00:00
|
|
|
fprintf(stderr, "Chyba: Neplatný formát vstupu.\n");
|
2024-03-13 10:10:55 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2024-03-20 14:28:37 +00:00
|
|
|
char name[MAX_NAME_LENGTH];
|
|
|
|
strncpy(name, token, MAX_NAME_LENGTH);
|
2024-03-20 14:08:46 +00:00
|
|
|
|
|
|
|
// Uložte študenta do poľa
|
|
|
|
strncpy(students[num_students].name, name, MAX_NAME_LENGTH);
|
|
|
|
students[num_students].votes = votes;
|
|
|
|
num_students++;
|
|
|
|
|
|
|
|
// Kontrola prekročenia maximálnej veľkosti poľa študentov
|
|
|
|
if (num_students >= MAX_BUFFER_LENGTH) {
|
|
|
|
fprintf(stderr, "Chyba: Príliš veľa študentov.\n");
|
2024-03-20 13:52:38 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2024-03-13 09:55:11 +00:00
|
|
|
}
|
|
|
|
|
2024-03-20 14:08:46 +00:00
|
|
|
// Ak neboli načítané žiadne dáta, vypíšte chybovú správu
|
|
|
|
if (num_students == 0) {
|
|
|
|
fprintf(stderr, "Chyba: Žiadne záznamy načítané.\n");
|
|
|
|
return 1;
|
2024-03-13 09:55:11 +00:00
|
|
|
}
|
|
|
|
|
2024-03-20 14:08:46 +00:00
|
|
|
// Zoradenie študentov podľa počtu hlasov a mena
|
|
|
|
qsort(students, num_students, sizeof(Student), compare_students);
|
|
|
|
|
|
|
|
// Výpis výsledkov
|
|
|
|
printf("Vysledky:\n");
|
|
|
|
for (int i = 0; i < num_students; i++) {
|
|
|
|
printf("%d %s\n", students[i].votes, students[i].name);
|
|
|
|
}
|
2024-03-20 13:52:38 +00:00
|
|
|
|
2024-03-13 09:55:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|