2024-03-22 07:38:38 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define MAX_STUDENTS 100
|
2024-03-22 07:49:29 +00:00
|
|
|
#define MAX_NAME_LENGTH 50
|
2024-03-22 07:38:38 +00:00
|
|
|
|
|
|
|
struct Student {
|
2024-03-22 07:49:29 +00:00
|
|
|
char name[MAX_NAME_LENGTH];
|
2024-03-22 07:38:38 +00:00
|
|
|
int votes;
|
|
|
|
};
|
|
|
|
|
2024-03-22 08:05:21 +00:00
|
|
|
// Funkcia načíta študentov zo štandardného vstupu a sčíta ich hlasy
|
|
|
|
int read_and_sum_votes(struct Student students[], int max_students) {
|
2024-03-22 07:49:29 +00:00
|
|
|
int count = 0;
|
|
|
|
while (count < max_students) {
|
2024-03-22 07:38:38 +00:00
|
|
|
int votes;
|
2024-03-22 07:49:29 +00:00
|
|
|
char name[MAX_NAME_LENGTH];
|
|
|
|
if (scanf("%d %[^\n]", &votes, name) != 2) {
|
|
|
|
break; // Koniec vstupu alebo chyba
|
2024-03-22 07:38:38 +00:00
|
|
|
}
|
2024-03-22 08:05:21 +00:00
|
|
|
|
|
|
|
// Hľadáme, či už sme tento študent videli
|
|
|
|
int found = 0;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if (strcmp(students[i].name, name) == 0) {
|
|
|
|
// Ak sme študenta našli, pridáme mu hlasy
|
|
|
|
students[i].votes += votes;
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ak sme študenta ešte nevideli, pridáme ho do zoznamu
|
|
|
|
if (!found) {
|
|
|
|
strcpy(students[count].name, name);
|
|
|
|
students[count].votes = votes;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2024-03-22 07:51:27 +00:00
|
|
|
// Zbavíme sa koncového znaku nového riadku z bufferu vstupu
|
|
|
|
getchar();
|
2024-03-22 07:49:29 +00:00
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2024-03-22 08:05:21 +00:00
|
|
|
// Funkcia na porovnávanie študentov pre qsort
|
2024-03-22 07:49:29 +00:00
|
|
|
int compare(const void *p1, const void *p2) {
|
2024-03-22 08:05:21 +00:00
|
|
|
const struct Student *s1 = (const struct Student *)p1;
|
|
|
|
const struct Student *s2 = (const struct Student *)p2;
|
2024-03-22 07:49:29 +00:00
|
|
|
// 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);
|
2024-03-22 07:38:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
struct Student students[MAX_STUDENTS];
|
2024-03-22 08:05:21 +00:00
|
|
|
int count = read_and_sum_votes(students, MAX_STUDENTS);
|
2024-03-22 07:49:29 +00:00
|
|
|
|
|
|
|
if (count == 0) {
|
|
|
|
printf("Chyba: Nepodarilo sa načítať žiadne dáta.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2024-03-22 07:38:38 +00:00
|
|
|
|
2024-03-22 08:05:21 +00:00
|
|
|
// Triedenie študentov
|
|
|
|
qsort(students, count, sizeof(struct Student), compare);
|
|
|
|
|
|
|
|
// Výpis výsledkov
|
|
|
|
printf("Vysledky:\n");
|
2024-03-22 07:59:47 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
2024-03-22 08:05:21 +00:00
|
|
|
printf("%d %s\n", students[i].votes, students[i].name);
|
2024-03-22 07:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|