This commit is contained in:
Matej Hajduk 2025-03-11 12:32:55 +01:00
parent 5e4709d75b
commit e0eab6984f

82
du4/program.c Normal file
View File

@ -0,0 +1,82 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_MENO 100
#define MAX_STUDENTI 100
typedef struct {
char meno[MAX_MENO];
int hlasy;
} Student;
int najdi_studenta(Student studenti[], int pocet, char *meno) {
for (int i = 0; i < pocet; i++) {
if (strcmp(studenti[i].meno, meno) == 0) {
return i;
}
}
return -1;
}
int porovnaj(const void *a, const void *b) {
Student *s1 = (Student *)a;
Student *s2 = (Student *)b;
if (s2->hlasy != s1->hlasy) {
return s2->hlasy - s1->hlasy;
}
return strcmp(s1->meno, s2->meno);
}
int main() {
Student studenti[MAX_STUDENTI];
int pocet_studentov = 0;
char meno[MAX_MENO];
int hlasy;
while (scanf("%d ", &hlasy) == 1) {
if (fgets(meno, MAX_MENO, stdin) == NULL) {
printf("CHYBA\n");
return 1;
}
meno[strcspn(meno, "\n")] = 0;
if (hlasy <= 0 || strlen(meno) == 0) {
printf("CHYBA\n");
return 1;
}
int index = najdi_studenta(studenti, pocet_studentov, meno);
if (index != -1) {
studenti[index].hlasy += hlasy;
} else {
if (pocet_studentov >= MAX_STUDENTI) {
printf("CHYBA\n");
return 1;
}
strcpy(studenti[pocet_studentov].meno, meno);
studenti[pocet_studentov].hlasy = hlasy;
pocet_studentov++;
}
}
qsort(studenti, pocet_studentov, sizeof(Student), porovnaj);
printf("Vysledky:\n");
for (int i = 0; i < pocet_studentov; i++) {
printf("%d %s\n", studenti[i].hlasy, studenti[i].meno);
}
return 0;
}