diff --git a/du4/program.c b/du4/program.c new file mode 100644 index 0000000..30a0d48 --- /dev/null +++ b/du4/program.c @@ -0,0 +1,82 @@ +#include +#include +#include + +#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; +} +