“program”

This commit is contained in:
Vasylenko 2024-04-19 15:39:33 +02:00
parent 7ac6c75676
commit 7c4684e788

View File

@ -2,96 +2,86 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
int compare_strings(const void *a, const void *b) { #define MAX_NAME_LENGTH 100
const char *str1 = *(const char **)a;
const char *str2 = *(const char **)b; int compare(const void *a, const void *b) {
return strcmp(str1, str2); const char *name1 = *(const char **)a;
const char *name2 = *(const char **)b;
return strcmp(name1, name2);
} }
int main() { int main() {
int max_students, read; int max_students;
char buffer[101]; char name[MAX_NAME_LENGTH];
char **names = NULL; char **names = NULL;
size_t count = 0; int size = 0, capacity = 10;
size_t capacity = 0;
names = (char **)malloc(capacity * sizeof(char *));
if (names == NULL) {
puts("Memory allocation error");
return 1;
}
if (scanf("%d", &max_students) != 1 || max_students <= 0) { if (scanf("%d\n", &max_students) != 1 || max_students <= 0) {
puts("Nespravny vstup"); puts("Nespravny vstup");
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); free(names);
return 1; return 1;
} }
names = new_names;
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) {
names[count] = strdup(buffer); capacity *= 2;
if (!names[count]) { names = (char **)realloc(names, capacity * sizeof(char *));
puts("Memory allocation failed"); if (names == NULL) {
while (count > 0) free(names[--count]); puts("Memory allocation error");
free(names);
return 1; return 1;
} }
count++;
} }
if (count == 0) { names[size] = strdup(name);
if (names[size] == NULL) {
puts("Memory allocation error");
return 1;
}
size++;
}
if (size == 0) {
puts("Ziadne prihlasky"); puts("Ziadne prihlasky");
free(names); free(names);
return 1; return 1;
} }
qsort(names, size, sizeof(char *), compare);
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]);
}
}
puts("Prijati studenti:"); puts("Prijati studenti:");
size_t limit = max_students < unique_count ? max_students : unique_count; int accepted = size < max_students ? size : max_students;
for (size_t i = 0; i < limit; i++) { for (int i = 0; i < accepted; i++) {
puts(names[i]); puts(names[i]);
} }
if (size > max_students) {
if (limit < unique_count) {
puts("Neprijati studenti:"); puts("Neprijati studenti:");
for (size_t i = limit; i < unique_count; i++) { for (int i = max_students; i < size; i++) {
puts(names[i]); puts(names[i]);
}
}
for (int i = 0; i < size; i++) {
free(names[i]); free(names[i]);
} }
}
for (size_t i = 0; i < limit; i++) {
free(names[i]);
}
free(names); free(names);
return 0; return 0;
} }