From 71acba71136b0263373dc74d3289c364d63662ed Mon Sep 17 00:00:00 2001 From: Mykola Syniavskyi Date: Thu, 20 Mar 2025 23:48:26 +0000 Subject: [PATCH] =?UTF-8?q?P=C5=99idat=20du4/program.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- du4/program.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 du4/program.c diff --git a/du4/program.c b/du4/program.c new file mode 100644 index 0000000..eb7acd9 --- /dev/null +++ b/du4/program.c @@ -0,0 +1,63 @@ +#include +#include + +#define MAX_NAMELEN 50 +#define MAX_STUDENTS 100 + +typedef struct { + char name[MAX_NAMELEN]; + int votes; +} Student; + +int main() { + Student studenty[MAX_STUDENTS]; + char name[MAX_NAMELEN]; + char line[255]; + int znayd = 0; + int golosa=0; + int kilkstudentiv = 0; + + while (fgets(line, sizeof(line), stdin)) { + if (sscanf(line, "%d %[^\n]", &golosa, name) != 2 || golosa <= 0) { + if (kilkstudentiv == 0) { + printf("Chyba: Neplatný vstup.\n"); + return 1; + } + break; + } + + for (int i = 0; i < kilkstudentiv; i++) { + if (strcmp(studenty[i].name, name) == 0) { + studenty[i].votes += golosa; + znayd = 1; + break; + } + } + + if (znayd == 0) { + strcpy(studenty[kilkstudentiv].name, name); + studenty[kilkstudentiv].votes = golosa; + kilkstudentiv++; + } + } + + if (kilkstudentiv == 0) { + printf("Chyba: Neplatný vstup.\n"); + return 1; } + + for (int i = 0; i < kilkstudentiv - 1; i++) { + for (int j = i + 1; j < kilkstudentiv; j++) { + if (studenty[i].votes < studenty[j].votes || + (studenty[i].votes == studenty[j].votes && strcmp(studenty[i].name, studenty[j].name) > 0)) { + Student change = studenty[i]; + studenty[i] = studenty[j]; + studenty[j] = change; + } + } + } + printf("Vysledky:\n"); + for (int i = 0; i < kilkstudentiv; i++) { + printf("%d %s\n", studenty[i].votes, studenty[i].name); + } + return 0; +} \ No newline at end of file