112 lines
2.3 KiB
C
112 lines
2.3 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
#define SIZE 100
|
|
|
|
struct student {
|
|
|
|
char meno[SIZE];
|
|
int hlasy;
|
|
|
|
};
|
|
|
|
int najdi_studenta(struct student* studenti, int pocet, const char* meno) {
|
|
|
|
for (int i = 0; i < pocet; i++) {
|
|
|
|
if (strcmp(studenti[i].meno, meno) == 0) {
|
|
return i;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int porovnaj(const void* p1, const void* p2) {
|
|
|
|
struct student* s1 = (struct student*)p1;
|
|
struct student* s2 = (struct student*)p2;
|
|
|
|
if (s2->hlasy != s1->hlasy) return s2->hlasy - s1->hlasy;
|
|
return strcmp(s1->meno, s2->meno);
|
|
}
|
|
|
|
int main() {
|
|
|
|
struct student databaza[SIZE];
|
|
memset(databaza, 0, SIZE * sizeof(struct student));
|
|
int pocet = 0;
|
|
char riadok[SIZE];
|
|
|
|
while (1) {
|
|
|
|
memset(riadok, 0, SIZE);
|
|
char* vysledok = fgets(riadok, SIZE, stdin);
|
|
|
|
if (vysledok == NULL) {
|
|
break;
|
|
}
|
|
|
|
char* koniec = NULL;
|
|
int hodnota = (int)strtol(riadok, &koniec, 10);
|
|
|
|
if (koniec == riadok || hodnota == 0) {
|
|
break;
|
|
}
|
|
|
|
while (*koniec == ' ') {
|
|
koniec++;
|
|
}
|
|
|
|
char* zaciatok_mena = koniec;
|
|
|
|
if (*zaciatok_mena == '\n' || *zaciatok_mena == '\0') {
|
|
continue;
|
|
}
|
|
|
|
char meno[SIZE];
|
|
memset(meno, 0, SIZE);
|
|
int velkost_mena = (int)strlen(zaciatok_mena);
|
|
|
|
if (zaciatok_mena[velkost_mena - 1] == '\n') {
|
|
velkost_mena--;
|
|
}
|
|
if (velkost_mena <= 0) {
|
|
continue;
|
|
}
|
|
|
|
memcpy(meno, zaciatok_mena, velkost_mena);
|
|
meno[velkost_mena] = '\0';
|
|
|
|
int index = najdi_studenta(databaza, pocet, meno);
|
|
|
|
if (index < 0) {
|
|
if (pocet >= SIZE) {
|
|
break;
|
|
}
|
|
|
|
memcpy(databaza[pocet].meno, meno, velkost_mena);
|
|
databaza[pocet].meno[velkost_mena] = '\0';
|
|
databaza[pocet].hlasy = hodnota;
|
|
pocet++;
|
|
|
|
} else {
|
|
databaza[index].hlasy += hodnota;
|
|
}
|
|
}
|
|
|
|
qsort(databaza, pocet, sizeof(struct student), porovnaj);
|
|
|
|
if (pocet == 0) {
|
|
printf("Nepodarilo nacitat nic\n");
|
|
} else {
|
|
printf("Vysledky:\n");
|
|
|
|
for (int i = 0; i < pocet; i++) {
|
|
printf("%d %s\n", databaza[i].hlasy, databaza[i].meno);
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|