60 lines
1.4 KiB
C
60 lines
1.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define MAX_STUDENTS 100
|
|
#define MAX_NAME_LENGTH 50
|
|
|
|
struct Student {
|
|
char name[MAX_NAME_LENGTH];
|
|
int votes;
|
|
};
|
|
|
|
int read_students(struct Student students[], int max_students) {
|
|
int count = 0;
|
|
while (count < max_students) {
|
|
int votes;
|
|
char name[MAX_NAME_LENGTH];
|
|
if (scanf("%d %[^\n]", &votes, name) != 2) {
|
|
break; // Koniec vstupu alebo chyba
|
|
}
|
|
strcpy(students[count].name, name);
|
|
students[count].votes = votes;
|
|
count++;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
int compare(const void *p1, const void *p2) {
|
|
struct Student *s1 = (struct Student *)p1;
|
|
struct Student *s2 = (struct Student *)p2;
|
|
// Porovnanie podľa počtu hlasov, v prípade rovnakého počtu podľa mena
|
|
if (s1->votes != s2->votes) {
|
|
return s2->votes - s1->votes;
|
|
} else {
|
|
return strcmp(s1->name, s2->name);
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
struct Student students[MAX_STUDENTS];
|
|
int count = read_students(students, MAX_STUDENTS);
|
|
|
|
if (count == 0) {
|
|
printf("Chyba: Nepodarilo sa načítať žiadne dáta.\n");
|
|
return 1;
|
|
}
|
|
|
|
// Triedenie študentov
|
|
qsort(students, count, sizeof(struct Student), compare);
|
|
|
|
// Výpis výsledkov
|
|
printf("Výsledky:\n");
|
|
for (int i = 0; i < count; i++) {
|
|
printf("%d %s\n", students[i].votes, students[i].name);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|