96 lines
2.0 KiB
C
96 lines
2.0 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define SIZE 100
|
|
|
|
struct student {
|
|
char name[SIZE];
|
|
int votes;
|
|
};
|
|
|
|
int najdi_studenta(struct student databaza[], int pocet, char meno[]) {
|
|
int i;
|
|
for (i = 0; i < pocet; i++) {
|
|
if (strcmp(databaza[i].name, meno) == 0) {
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int porovnaj(const void *a, const void *b) {
|
|
struct student *s1 = (struct student *)a;
|
|
struct student *s2 = (struct student *)b;
|
|
|
|
if (s1->votes != s2->votes) {
|
|
return s2->votes - s1->votes;
|
|
}
|
|
|
|
return strcmp(s1->name, s2->name);
|
|
}
|
|
|
|
int main() {
|
|
struct student databaza[SIZE];
|
|
int pocet_studentov = 0;
|
|
int valid_input = 0;
|
|
|
|
memset(databaza, 0, SIZE * sizeof(struct student));
|
|
|
|
char riadok[SIZE];
|
|
char meno[SIZE];
|
|
int hlasy;
|
|
|
|
while (fgets(riadok, SIZE, stdin) != NULL) {
|
|
int dlzka = strlen(riadok);
|
|
if (dlzka > 0 && riadok[dlzka-1] == '\n') {
|
|
riadok[dlzka-1] = '\0';
|
|
}
|
|
|
|
char *koniec;
|
|
hlasy = strtol(riadok, &koniec, 10);
|
|
|
|
if (hlasy <= 0) {
|
|
break;
|
|
}
|
|
|
|
if (*koniec != ' ') {
|
|
break;
|
|
}
|
|
|
|
char *meno_start = koniec + 1;
|
|
|
|
if (strlen(meno_start) == 0) {
|
|
break;
|
|
}
|
|
|
|
strcpy(meno, meno_start);
|
|
|
|
int pozicia = najdi_studenta(databaza, pocet_studentov, meno);
|
|
|
|
if (pozicia == -1) {
|
|
strcpy(databaza[pocet_studentov].name, meno);
|
|
databaza[pocet_studentov].votes = hlasy;
|
|
pocet_studentov++;
|
|
} else {
|
|
databaza[pozicia].votes += hlasy;
|
|
}
|
|
|
|
valid_input = 1;
|
|
}
|
|
|
|
if (!valid_input) {
|
|
printf("Chyba: Nepodarilo sa načítať žiadny platný záznam.\n");
|
|
return 1;
|
|
}
|
|
qsort(databaza, pocet_studentov, sizeof(struct student), porovnaj);
|
|
|
|
printf("Vysledky:\n");
|
|
int i;
|
|
for (i = 0; i < pocet_studentov; i++) {
|
|
printf("%d %s\n", databaza[i].votes, databaza[i].name);
|
|
}
|
|
|
|
return 0;
|
|
}
|