2024-03-21 22:47:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-03-21 23:18:00 +00:00
|
|
|
#define MAX_SIZE 100
|
|
|
|
#define MAX_STUDENTS 100
|
|
|
|
|
|
|
|
// Štruktúra pre reprezentáciu študenta
|
|
|
|
struct Student {
|
|
|
|
char name[MAX_SIZE];
|
|
|
|
int votes;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Porovnávacia funkcia pre triedenie študentov
|
|
|
|
int compare(const void *p1, const void *p2) {
|
|
|
|
const struct Student *s1 = (const struct Student *)p1;
|
|
|
|
const struct Student *s2 = (const struct Student *)p2;
|
|
|
|
// Porovnanie počtu hlasov
|
|
|
|
if (s1->votes != s2->votes) {
|
|
|
|
return s2->votes - s1->votes; // Zoradenie zostupne podľa hlasov
|
2024-03-21 23:09:53 +00:00
|
|
|
}
|
2024-03-21 23:18:00 +00:00
|
|
|
// Ak majú rovnaký počet hlasov, zoradíme lexikograficky podľa mena
|
|
|
|
return strcmp(s1->name, s2->name);
|
2024-03-21 23:09:53 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 23:18:00 +00:00
|
|
|
int main() {
|
|
|
|
// Otvoríme súbor na čítanie
|
|
|
|
FILE *file = fopen("nazov_suboru.txt", "r");
|
|
|
|
if (file == NULL) {
|
|
|
|
perror("Chyba pri otváraní súboru");
|
|
|
|
return 1;
|
2024-03-21 23:13:59 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 23:18:00 +00:00
|
|
|
// Pole študentov
|
|
|
|
struct Student students[MAX_STUDENTS];
|
2024-03-21 23:04:41 +00:00
|
|
|
int num_students = 0;
|
2024-03-21 23:06:32 +00:00
|
|
|
|
2024-03-21 23:18:00 +00:00
|
|
|
// Načítame každý riadok zo súboru
|
|
|
|
char line[MAX_SIZE];
|
|
|
|
while (fgets(line, sizeof(line), file) != NULL) {
|
|
|
|
// Načítame počet hlasov a meno
|
|
|
|
int votes;
|
|
|
|
char name[MAX_SIZE];
|
|
|
|
if (sscanf(line, "%d %99[^\n]", &votes, name) == 2) {
|
|
|
|
// Uložíme študenta do pola študentov
|
|
|
|
strcpy(students[num_students].name, name);
|
|
|
|
students[num_students].votes = votes;
|
2024-03-21 23:09:53 +00:00
|
|
|
num_students++;
|
|
|
|
} else {
|
2024-03-21 23:18:00 +00:00
|
|
|
fprintf(stderr, "CHYBA: Neplatný zápis na riadku\n");
|
2024-03-21 23:04:41 +00:00
|
|
|
}
|
2024-03-21 22:47:06 +00:00
|
|
|
}
|
2024-03-21 23:01:55 +00:00
|
|
|
|
2024-03-21 23:18:00 +00:00
|
|
|
// Uzavrieme súbor
|
|
|
|
fclose(file);
|
2024-03-21 23:13:59 +00:00
|
|
|
|
2024-03-21 23:18:00 +00:00
|
|
|
// Zoradíme študentov podľa počtu hlasov
|
|
|
|
qsort(students, num_students, sizeof(struct Student), compare);
|
2024-03-21 23:04:41 +00:00
|
|
|
|
2024-03-21 23:18:00 +00:00
|
|
|
// Vypíšeme výsledky
|
|
|
|
printf("Výsledky:\n");
|
2024-03-21 23:04:41 +00:00
|
|
|
for (int i = 0; i < num_students; i++) {
|
2024-03-21 23:18:00 +00:00
|
|
|
printf("%d %s\n", students[i].votes, students[i].name);
|
2024-03-21 22:58:42 +00:00
|
|
|
}
|
2024-03-21 23:01:55 +00:00
|
|
|
|
2024-03-21 22:47:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|