diff --git a/du4/program.c b/du4/program.c index 30a0d48..fe07f7c 100644 --- a/du4/program.c +++ b/du4/program.c @@ -1,13 +1,15 @@ + #include #include #include +#include -#define MAX_MENO 100 -#define MAX_STUDENTI 100 +#define MAX_M 100 +#define MAX_S 100 typedef struct { - char meno[MAX_MENO]; + char meno[MAX_M]; int hlasy; } Student; @@ -26,40 +28,56 @@ 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 je_cislo(const char *retazec) { + while (*retazec) { + if (!isdigit(*retazec)) { + return 0; + } + retazec++; + } + return 1; +} + int main() { - Student studenti[MAX_STUDENTI]; + Student studenti[MAX_S]; int pocet_studentov = 0; - char meno[MAX_MENO]; + char meno[MAX_M]; + char vstup[MAX_M + 10]; int hlasy; + int nacitane = 0; - while (scanf("%d ", &hlasy) == 1) { - if (fgets(meno, MAX_MENO, stdin) == NULL) { - printf("CHYBA\n"); - return 1; - } - - - meno[strcspn(meno, "\n")] = 0; + while (fgets(vstup, sizeof(vstup), stdin) != NULL) + vstup[strcspn(vstup, "\n")] = 0; - if (hlasy <= 0 || strlen(meno) == 0) { - printf("CHYBA\n"); + + if (strlen(vstup) == 0) { + break; + } + + + if (sscanf(vstup, "%d %[^\n]", &hlasy, meno) != 2 || hlasy <= 0) { + printf("Nepodarilo nacitat nic\n"); return 1; } + nacitane = 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"); + if (pocet_studentov >= MAX_S) { + printf("Nepodarilo nacitat nic\n"); return 1; } strcpy(studenti[pocet_studentov].meno, meno); @@ -68,10 +86,16 @@ int main() { } } - + + if (!nacitane) { + printf("Nepodarilo nacitat nic\n"); + return 1; + } + + 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);