From e3610ccddfaf14529cbd088be3d7d5a8b4a09c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Frankovi=C4=8D?= Date: Thu, 5 Mar 2026 13:42:08 +0100 Subject: [PATCH] du2 --- du2/program.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 du2/program.c diff --git a/du2/program.c b/du2/program.c new file mode 100644 index 0000000..cdf52f3 --- /dev/null +++ b/du2/program.c @@ -0,0 +1,69 @@ +#include +#include +#include + +#define MAX_STUDENTS 1024 +#define MAX_NAME 256 + +typedef struct { + char name[MAX_NAME]; + int votes; +} Student; + +Student students[MAX_STUDENTS]; +int count = 0; + +int find(const char *name) { + for (int i = 0; i < count; i++) { + if (strcmp(students[i].name, name) == 0) return i; + } + return -1; +} + +int cmp(const void *a, const void *b) { + const Student *sa = (const Student *)a; + const Student *sb = (const Student *)b; + if (sb->votes != sa->votes) return sb->votes - sa->votes; + return strcmp(sa->name, sb->name); +} + +int main() { + char line[MAX_NAME + 32]; + int loaded = 0; + + while (fgets(line, sizeof(line), stdin) != NULL) { + int votes; + char name[MAX_NAME]; + + line[strcspn(line, "\n")] = '\0'; + + if (sscanf(line, "%d %255[^\n]", &votes, name) != 2 || votes <= 0) { + break; + } + + loaded++; + int idx = find(name); + if (idx >= 0) { + students[idx].votes += votes; + } else { + if (count >= MAX_STUDENTS) break; + strncpy(students[count].name, name, MAX_NAME - 1); + students[count].votes = votes; + count++; + } + } + + if (loaded == 0) { + fprintf(stderr, "Chyba: nepodarilo sa nacitat ziadny zaznam.\n"); + return 1; + } + + qsort(students, count, sizeof(Student), cmp); + + printf("Vysledky:\n"); + for (int i = 0; i < count; i++) { + printf("%d %s\n", students[i].votes, students[i].name); + } + + return 0; +}