2024-03-21 12:38:59 +00:00
|
|
|
#include <stdio.h>
|
2024-03-21 15:43:03 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2024-03-21 12:38:59 +00:00
|
|
|
|
2024-03-21 13:09:15 +00:00
|
|
|
#define SIZE 100
|
|
|
|
|
|
|
|
struct student {
|
|
|
|
char name[SIZE];
|
|
|
|
int votes;
|
|
|
|
};
|
2024-03-21 15:43:03 +00:00
|
|
|
|
2024-03-21 15:45:29 +00:00
|
|
|
// porovnávacia funkcia pre zoradenie študentov
|
2024-03-21 15:43:03 +00:00
|
|
|
int compare(const void *a, const void *b) {
|
|
|
|
struct student *student_a = (struct student *) a;
|
|
|
|
struct student *student_b = (struct student *) b;
|
|
|
|
|
|
|
|
if (student_a->votes > student_b->votes) {
|
|
|
|
return -1;
|
|
|
|
} else if (student_a->votes < student_b->votes) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
2024-03-21 15:45:29 +00:00
|
|
|
// ak majú rovnaký počet hlasov, porovnajte mená
|
2024-03-21 15:43:03 +00:00
|
|
|
return strcmp(student_a->name, student_b->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2024-03-21 15:45:29 +00:00
|
|
|
// definícia poľa študentov s veľkosťou SIZE
|
2024-03-21 15:43:03 +00:00
|
|
|
struct student students[SIZE];
|
|
|
|
|
2024-03-21 15:45:29 +00:00
|
|
|
// inicializácia premennej na uchovávanie veľkosti poľa
|
2024-03-21 15:43:03 +00:00
|
|
|
int size = 0;
|
|
|
|
|
2024-03-21 15:45:29 +00:00
|
|
|
// načítanie údajov o študentoch zo štandardného vstupu
|
2024-03-21 15:43:03 +00:00
|
|
|
while (1) {
|
|
|
|
char line[SIZE];
|
|
|
|
memset(line, 0, SIZE);
|
|
|
|
char *r = fgets(line, SIZE, stdin);
|
|
|
|
|
2024-03-21 15:45:29 +00:00
|
|
|
// kontrola, či je riadok prázdny
|
|
|
|
if (r == NULL || (r[0] == '\n' && strlen(r) == 1)) {
|
|
|
|
// koniec vstupu
|
2024-03-21 15:43:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-03-21 15:45:29 +00:00
|
|
|
// rozparsovanie riadku a extrakcia počtu hlasov a mena študenta
|
2024-03-21 15:43:03 +00:00
|
|
|
int votes;
|
|
|
|
if (sscanf(line, "%d %s", &votes, students[size].name) != 2) {
|
2024-03-21 15:45:29 +00:00
|
|
|
// chyba, vstup nebol v očakávanom formáte
|
|
|
|
// vypíšte chybovú správu a ukončite program
|
|
|
|
fprintf(stderr, "Chyba: neplatný formát vstupu\n");
|
2024-03-21 15:43:03 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
students[size].votes = votes;
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
|
2024-03-21 15:45:29 +00:00
|
|
|
// zoradenie študentov podľa počtu hlasov
|
2024-03-21 15:43:03 +00:00
|
|
|
qsort(students, size, sizeof(struct student), compare);
|
|
|
|
|
2024-03-21 15:45:29 +00:00
|
|
|
// výpis výsledkov
|
|
|
|
printf("Výsledky:\n");
|
2024-03-21 15:43:03 +00:00
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
printf("%d %s\n", students[i].votes, students[i].name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|