From 7c4684e78862c8bb9b02f05c15679b91997f5ffe Mon Sep 17 00:00:00 2001 From: Vasylenko Date: Fri, 19 Apr 2024 15:39:33 +0200 Subject: [PATCH] =?UTF-8?q?=E2=80=9Cprogram=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cv10/program.c | 116 ++++++++++++++++++++++--------------------------- 1 file changed, 53 insertions(+), 63 deletions(-) diff --git a/cv10/program.c b/cv10/program.c index a9d0295..f81a7a1 100644 --- a/cv10/program.c +++ b/cv10/program.c @@ -2,96 +2,86 @@ #include #include -int compare_strings(const void *a, const void *b) { - const char *str1 = *(const char **)a; - const char *str2 = *(const char **)b; - return strcmp(str1, str2); +#define MAX_NAME_LENGTH 100 + +int compare(const void *a, const void *b) { + const char *name1 = *(const char **)a; + const char *name2 = *(const char **)b; + return strcmp(name1, name2); } int main() { - int max_students, read; - char buffer[101]; + int max_students; + char name[MAX_NAME_LENGTH]; char **names = NULL; - size_t count = 0; - size_t capacity = 0; + int size = 0, capacity = 10; - - if (scanf("%d", &max_students) != 1 || max_students <= 0) { - puts("Nespravny vstup"); + names = (char **)malloc(capacity * sizeof(char *)); + if (names == NULL) { + puts("Memory allocation error"); return 1; } - - while (fgets(buffer, sizeof(buffer), stdin)) { - - buffer[strcspn(buffer, "\n")] = 0; - if (strlen(buffer) == 0) break; - - - if (count == capacity) { - capacity = capacity == 0 ? 16 : capacity * 2; - char **new_names = realloc(names, capacity * sizeof(char *)); - if (!new_names) { - puts("Memory allocation failed"); - free(names); - return 1; - } - names = new_names; - } - - - names[count] = strdup(buffer); - if (!names[count]) { - puts("Memory allocation failed"); - while (count > 0) free(names[--count]); - free(names); - return 1; - } - count++; + if (scanf("%d\n", &max_students) != 1 || max_students <= 0) { + puts("Nespravny vstup"); + free(names); + return 1; } - if (count == 0) { + while (fgets(name, MAX_NAME_LENGTH, stdin) != NULL) { + if (name[0] == '\n') break; + name[strcspn(name, "\n")] = 0; + + int duplicate = 0; + for (int i = 0; i < size; i++) { + if (strcmp(names[i], name) == 0) { + duplicate = 1; + break; + } + } + if (duplicate) continue; + + if (size == capacity) { + capacity *= 2; + names = (char **)realloc(names, capacity * sizeof(char *)); + if (names == NULL) { + puts("Memory allocation error"); + return 1; + } + } + + names[size] = strdup(name); + if (names[size] == NULL) { + puts("Memory allocation error"); + return 1; + } + size++; + } + + if (size == 0) { puts("Ziadne prihlasky"); free(names); return 1; } - - qsort(names, count, sizeof(char *), compare_strings); - - - size_t unique_count = 0; - for (size_t i = 0; i < count; i++) { - if (unique_count == 0 || strcmp(names[unique_count - 1], names[i]) != 0) { - names[unique_count++] = names[i]; - } else { - free(names[i]); - } - } - + qsort(names, size, sizeof(char *), compare); puts("Prijati studenti:"); - size_t limit = max_students < unique_count ? max_students : unique_count; - for (size_t i = 0; i < limit; i++) { + int accepted = size < max_students ? size : max_students; + for (int i = 0; i < accepted; i++) { puts(names[i]); } - - if (limit < unique_count) { + if (size > max_students) { puts("Neprijati studenti:"); - for (size_t i = limit; i < unique_count; i++) { + for (int i = max_students; i < size; i++) { puts(names[i]); - free(names[i]); } } - - for (size_t i = 0; i < limit; i++) { + for (int i = 0; i < size; i++) { free(names[i]); } - - free(names); return 0; } -