diff --git a/cv5/program.c b/cv5/program.c index e69de29..7aaaa58 100644 --- a/cv5/program.c +++ b/cv5/program.c @@ -0,0 +1,66 @@ +#include +#include +#include + +#define MAX_STUDENTOV 100 +#define MAX_MENO 100 + +typedef struct { + char meno[MAX_MENO]; + int hlasy; +} Student; + +Student studenti[MAX_STUDENTOV]; +int pocetStudentov = 0; + +int pridajHlas(char* meno, int pocetHlasov) { + for (int i = 0; i < pocetStudentov; i++) { + if (strcmp(studenti[i].meno, meno) == 0) { + studenti[i].hlasy += pocetHlasov; + return 0; + } + } + if (pocetStudentov < MAX_STUDENTOV) { + strcpy(studenti[pocetStudentov].meno, meno); + studenti[pocetStudentov].hlasy = pocetHlasov; + pocetStudentov++; + return 0; + } + return 1; +} + +int porovnaj(const void* a, const void* b) { + Student *studentA = (Student *)a; + Student *studentB = (Student *)b; + + if (studentA->hlasy == studentB->hlasy) { + return strcmp(studentA->meno, studentB->meno); + } + return studentB->hlasy - studentA->hlasy; +} + +int main() { + char riadok[150]; + char meno[MAX_MENO]; + int pocetHlasov; + + while (fgets(riadok, sizeof(riadok), stdin)) { + if (sscanf(riadok, "%d %[^\n]s", &pocetHlasov, meno) == 2) { + if (pridajHlas(meno, pocetHlasov)) { + printf("Nepodarilo sa pridaƄ hlas.\n"); + return 1; + } + } else { + break; + } + } + + qsort(studenti, pocetStudentov, sizeof(Student), porovnaj); + + printf("Vysledky:\n"); + for (int i = 0; i < pocetStudentov; i++) { + printf("%d %s\n", studenti[i].hlasy, studenti[i].meno); + } + + return 0; +}