pvjc24/cv10/program.c

73 lines
1.7 KiB
C
Raw Normal View History

2024-04-22 12:01:17 +00:00
#include <stdio.h>
2024-04-25 16:32:35 +00:00
#include <stdlib.h>
2024-04-25 15:29:10 +00:00
#include <string.h>
2024-04-22 12:01:17 +00:00
2024-04-25 18:28:58 +00:00
int compare_names(const void *a, const void *b) {
2024-04-25 17:04:21 +00:00
return strcmp(*(const char **)a, *(const char **)b);
}
2024-04-22 12:01:17 +00:00
int main() {
2024-04-25 18:28:58 +00:00
int count;
2024-04-25 16:42:45 +00:00
char buffer[100];
2024-04-25 18:28:58 +00:00
char **applications;
2024-04-25 19:03:39 +00:00
int i, j, accepted_count = 0;
2024-04-22 12:06:40 +00:00
2024-04-25 18:42:45 +00:00
if (scanf("%d", &count) != 1 || count <= 0) {
2024-04-25 18:28:58 +00:00
printf("Nespravny vstup\n");
2024-04-22 12:06:40 +00:00
return 1;
}
2024-04-22 12:01:17 +00:00
2024-04-25 18:28:58 +00:00
applications = (char **)malloc(count * sizeof(char *));
if (!applications) {
printf("Chyba pri alokacii pamate\n");
2024-04-25 16:42:45 +00:00
return 1;
2024-04-25 16:32:35 +00:00
}
2024-04-25 15:41:03 +00:00
2024-04-25 18:28:58 +00:00
i = 0;
while (i < count && fgets(buffer, sizeof(buffer), stdin)) {
int len = strlen(buffer);
if (len > 0 && buffer[len - 1] == '\n') { // Remove newline character if present
buffer[len - 1] = '\0';
2024-04-25 18:28:01 +00:00
}
2024-04-25 18:34:12 +00:00
int found = 0;
for (j = 0; j < i; j++) {
if (strcmp(applications[j], buffer) == 0) {
found = 1;
break;
}
}
if (!found) {
2024-04-25 18:52:22 +00:00
applications[i] = strdup(buffer);
2024-04-25 18:55:28 +00:00
i++;
2024-04-25 19:03:39 +00:00
accepted_count++;
2024-04-25 18:34:12 +00:00
}
2024-04-25 16:42:45 +00:00
}
2024-04-25 19:03:39 +00:00
if (accepted_count == 0) {
2024-04-25 18:28:58 +00:00
printf("Ziadne prihlasky\n");
2024-04-25 16:32:35 +00:00
return 1;
}
2024-04-25 15:56:47 +00:00
2024-04-25 18:28:01 +00:00
// Sort the applications alphabetically
2024-04-25 19:03:39 +00:00
qsort(applications, accepted_count, sizeof(char *), compare_names);
2024-04-22 12:01:17 +00:00
2024-04-25 18:51:35 +00:00
printf("Prijati studenti:");
2024-04-25 19:03:39 +00:00
for (j = 0; j < accepted_count; j++) {
2024-04-25 18:39:49 +00:00
printf("%s\n", applications[j]);
2024-04-25 18:28:58 +00:00
}
2024-04-25 19:03:39 +00:00
if (count < accepted_count) {
2024-04-25 18:42:45 +00:00
printf("Neprijati studenti:");
for (; j < i; j++) {
printf("%s\n", applications[j]);
}
}
2024-04-25 18:28:58 +00:00
// Free allocated memory
for (j = 0; j < i; j++) {
free(applications[j]);
}
free(applications);
2024-04-25 16:42:45 +00:00
2024-04-22 12:01:17 +00:00
return 0;
2024-04-25 19:03:39 +00:00
}