This commit is contained in:
Filip Chochol 2026-03-09 17:40:52 +01:00
parent 76516ba3d8
commit 8dce09feb0

View File

@ -5,8 +5,10 @@
#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) {
@ -23,18 +25,16 @@ 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 -1; if (s2->hlasy > s1->hlasy) return -1;
if (s2->hlasy < s1->hlasy) return 1; if (s2->hlasy < s1->hlasy) return 1;
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];
@ -66,7 +66,6 @@ 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') {
@ -83,29 +82,28 @@ int main() {
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;
} }
} }
qsort(databaza, pocet, sizeof(struct student), porovnaj); qsort(databaza, pocet, sizeof(struct student), porovnaj);
printf("Vysledky:\n");
for (int i = 0; i < pocet; i++) { for (int i = 0; i < pocet; i++) {
printf("%s %d\n", databaza[i].meno, databaza[i].hlasy); printf("%d %s\n", databaza[i].hlasy, databaza[i].meno);
} }
return 0; return 0;
} }