“program”
This commit is contained in:
parent
7ac6c75676
commit
7c4684e788
116
cv10/program.c
116
cv10/program.c
@ -2,96 +2,86 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user