This commit is contained in:
Filip Chochol 2026-03-10 13:48:31 +01:00
parent f7d21c44c1
commit 4e49e6ad1c

View File

@ -5,12 +5,16 @@
#define SIZE 100 #define SIZE 100
struct student { struct student {
char meno[SIZE]; char meno[SIZE];
int hlasy; int hlasy;
}; };
int najdi_studenta(struct student* studenti, int pocet, const char* meno) { int najdi_studenta(struct student* studenti, int pocet, const char* meno) {
for (int i = 0; i < pocet; i++) { for (int i = 0; i < pocet; i++) {
if (strcmp(studenti[i].meno, meno) == 0) { if (strcmp(studenti[i].meno, meno) == 0) {
return i; return i;
} }
@ -19,27 +23,33 @@ int najdi_studenta(struct student* studenti, int pocet, const char* meno) {
} }
int porovnaj(const void* p1, const void* p2) { int porovnaj(const void* p1, const void* p2) {
struct student* s1 = (struct student*)p1; struct student* s1 = (struct student*)p1;
struct student* s2 = (struct student*)p2; struct student* s2 = (struct student*)p2;
if (s2->hlasy != s1->hlasy) return s2->hlasy - s1->hlasy; if (s2->hlasy != s1->hlasy) return s2->hlasy - s1->hlasy;
return strcmp(s1->meno, s2->meno); return strcmp(s1->meno, s2->meno);
} }
int main() { int main() {
struct student databaza[SIZE]; struct student databaza[SIZE];
memset(databaza, 0, SIZE * sizeof(struct student)); memset(databaza, 0, SIZE * sizeof(struct student));
int pocet = 0; int pocet = 0;
char riadok[SIZE]; char riadok[SIZE];
while (1) { while (1) {
memset(riadok, 0, SIZE); memset(riadok, 0, SIZE);
char* vysledok = fgets(riadok, SIZE, stdin); char* vysledok = fgets(riadok, SIZE, stdin);
if (vysledok == NULL) { if (vysledok == NULL) {
break; break;
} }
char* koniec = NULL; char* koniec = NULL;
int hodnota = (int)strtol(riadok, &koniec, 10); int hodnota = (int)strtol(riadok, &koniec, 10);
if (koniec == riadok || hodnota == 0) { if (koniec == riadok || hodnota == 0) {
break; break;
} }
@ -49,6 +59,7 @@ int main() {
} }
char* zaciatok_mena = koniec; char* zaciatok_mena = koniec;
if (*zaciatok_mena == '\n' || *zaciatok_mena == '\0') { if (*zaciatok_mena == '\n' || *zaciatok_mena == '\0') {
continue; continue;
} }
@ -56,6 +67,7 @@ int main() {
char meno[SIZE]; char meno[SIZE];
memset(meno, 0, SIZE); memset(meno, 0, SIZE);
int velkost_mena = (int)strlen(zaciatok_mena); int velkost_mena = (int)strlen(zaciatok_mena);
if (zaciatok_mena[velkost_mena - 1] == '\n') { if (zaciatok_mena[velkost_mena - 1] == '\n') {
velkost_mena--; velkost_mena--;
} }
@ -67,14 +79,17 @@ int main() {
meno[velkost_mena] = '\0'; meno[velkost_mena] = '\0';
int index = najdi_studenta(databaza, pocet, meno); int index = najdi_studenta(databaza, pocet, meno);
if (index < 0) { if (index < 0) {
if (pocet >= SIZE) { if (pocet >= SIZE) {
break; break;
} }
memcpy(databaza[pocet].meno, meno, velkost_mena); memcpy(databaza[pocet].meno, meno, velkost_mena);
databaza[pocet].meno[velkost_mena] = '\0'; databaza[pocet].meno[velkost_mena] = '\0';
databaza[pocet].hlasy = hodnota; databaza[pocet].hlasy = hodnota;
pocet++; pocet++;
} else { } else {
databaza[index].hlasy += hodnota; databaza[index].hlasy += hodnota;
} }
@ -86,6 +101,7 @@ int main() {
printf("Nepodarilo nacitat nic\n"); printf("Nepodarilo nacitat nic\n");
} else { } else {
printf("Vysledky:\n"); printf("Vysledky:\n");
for (int i = 0; i < pocet; i++) { for (int i = 0; i < pocet; i++) {
printf("%d %s\n", databaza[i].hlasy, databaza[i].meno); printf("%d %s\n", databaza[i].hlasy, databaza[i].meno);
} }