From 0b02744f3c61543479ae99b8ce8b77ec4b4b2702 Mon Sep 17 00:00:00 2001 From: Vasylenko Date: Tue, 23 Apr 2024 09:41:48 +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, 48 insertions(+), 68 deletions(-) diff --git a/cv10/program.c b/cv10/program.c index a3c994a..4a0d918 100644 --- a/cv10/program.c +++ b/cv10/program.c @@ -1,94 +1,74 @@ -~~#include +#include #include #include #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); + return strcmp(*(const char **)a, *(const char **)b); } int main() { int max_students; - char name[MAX_NAME_LENGTH]; - char **names = NULL; - int size = 0, capacity = 10; - - names = (char **)malloc(capacity * sizeof(char *)); - if (names == NULL) { - puts("Memory allocation error"); - return 0; - } - - if (scanf("%d\n", &max_students) != 1 || max_students <= 0) { + char temp[MAX_NAME_LENGTH]; + char **students; + int count = 0, unique_count = 0; + + if (scanf("%d", &max_students) != 1 || max_students <= 0) { puts("Nespravny vstup"); - free(names); - return 0; + return 1; } - - 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; + + students = malloc(max_students * sizeof(char *)); + if (students == NULL) { + puts("Memory allocation failed"); + return 1; + } + + while (scanf("%99s", temp) == 1) { + int is_unique = 1; + for (int i = 0; i < unique_count; i++) { + if (strcmp(students[i], temp) == 0) { + is_unique = 0; break; } } - if (duplicate) continue; - - if (size == capacity) { - capacity *= 2; - char **new_names = (char **)realloc(names, capacity * sizeof(char *)); - if (new_names == NULL) { - puts("Memory allocation error"); - for (int j = 0; j < size; j++) free(names[j]); - free(names); - return 0; + + if (is_unique) { + students[unique_count] = strdup(temp); + if (students[unique_count] == NULL) { + puts("Memory allocation failed"); + for (int i = 0; i < unique_count; i++) { + free(students[i]); + } + free(students); + return 1; + } + unique_count++; + if (unique_count >= max_students) { + break; } - names = new_names; } - - names[size] = strdup(name); - if (names[size] == NULL) { - puts("Memory allocation error"); - for (int j = 0; j < size; j++) free(names[j]); - free(names); - return 0; - } - size++; } - - if (size == 0) { + + if (unique_count == 0) { puts("Ziadne prihlasky"); - free(names); - return 0; + free(students); + return 1; } - - qsort(names, size, sizeof(char *), compare); - + + qsort(students, unique_count, sizeof(char *), compare); + puts("Prijati studenti:"); - int accepted = size < max_students ? size : max_students; - for (int i = 0; i < accepted; i++) { - puts(names[i]); + for (int i = 0; i < unique_count; i++) { + puts(students[i]); } - - if (size > max_students) { - puts("Neprijati studenti:"); - for (int i = max_students; i < size; i++) { - puts(names[i]); - } + + for (int i = 0; i < unique_count; i++) { + free(students[i]); } - - for (int i = 0; i < size; i++) { - free(names[i]); - } - free(names); + free(students); + return 0; } -