From da94129580ac70fe32dc35828359b54dd0aa6bad Mon Sep 17 00:00:00 2001 From: Denis Landa Date: Fri, 21 Mar 2025 15:22:30 +0100 Subject: [PATCH] 1254 --- du4/program.c | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/du4/program.c b/du4/program.c index 7d45190..51b57cd 100644 --- a/du4/program.c +++ b/du4/program.c @@ -1,10 +1,11 @@ #include #include #include +#include #define SIZE 100 -struct student{ +struct student { char name[SIZE]; int votes; }; @@ -12,9 +13,9 @@ struct student{ struct student databaza[SIZE]; int size = 0; -int find_student(const char* name){ - for (int i = 0; i < size; i++){ - if (strcmp(databaza[i].name, name) == 0){ +int find_student(const char* name) { + for (int i = 0; i < size; i++) { + if (strcmp(databaza[i].name, name) == 0) { return i; } } @@ -25,13 +26,9 @@ int compare(const void* a, const void* b) { struct student* s1 = (struct student*)a; struct student* s2 = (struct student*)b; - if (s1->votes > s2->votes) { - return -1; - } else if (s1->votes < s2->votes) { - return 1; - } else { - return strcmp(s1->name, s2->name); - } + if (s1->votes > s2->votes) return -1; + if (s1->votes < s2->votes) return 1; + return strcmp(s1->name, s2->name); } int main() { @@ -39,18 +36,17 @@ int main() { memset(databaza, 0, sizeof(databaza)); while (fgets(line, SIZE, stdin)) { - char* end = NULL; + char* end; int votes = strtol(line, &end, 10); - if (votes == 0 && end == line) { + if (*end != ' ') { printf("Chyba: Neplatny format vstupu!\n"); - continue; - } - while (*end == ' ') { - end++; + return 1; } + + end++; if (*end == '\0' || *end == '\n') { - printf("Chyba: Neplatny format - chyba meno!\n"); - continue; + printf("Chyba: Neplatny format vstupu!\n"); + return 1; } char name[SIZE]; @@ -62,9 +58,8 @@ int main() { if (id < 0) { if (size >= SIZE) { printf("Chyba: Databaza je plna!\n"); - continue; + return 1; } - strncpy(databaza[size].name, name, SIZE - 1); databaza[size].votes = votes; size++;