pvjc24/cv10/program.c

76 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 17:04:21 +00:00
int compare_names(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}
2024-04-22 12:01:17 +00:00
int main() {
2024-04-25 16:42:45 +00:00
int count;
char buffer[100];
char **applications;
int i, j;
2024-04-22 12:06:40 +00:00
2024-04-25 16:42:45 +00:00
if (scanf("%d", &count) != 1 || count <= 0) {
2024-04-25 15:32:41 +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 16:42:45 +00:00
applications = (char **)malloc(count * sizeof(char *));
if (!applications) {
printf("Chyba pri alokacii pamate\n");
return 1;
2024-04-25 16:32:35 +00:00
}
2024-04-25 15:41:03 +00:00
2024-04-25 16:42:45 +00:00
i = 0;
2024-04-25 17:30:56 +00:00
while (fgets(buffer, sizeof(buffer), stdin)) {
if (buffer[0] == '\n' || i >= count) { // End of input or reached maximum count
break;
}
2024-04-25 16:49:15 +00:00
int len = strlen(buffer);
2024-04-25 16:56:41 +00:00
if (len > 0 && buffer[len - 1] == '\n') { // Remove newline character if present
2024-04-25 16:49:15 +00:00
buffer[len - 1] = '\0';
}
2024-04-25 16:42:45 +00:00
int found = 0;
for (j = 0; j < i; j++) {
if (strcmp(applications[j], buffer) == 0) {
found = 1;
break;
}
}
if (!found) {
applications[i] = strdup(buffer);
i++;
}
}
if (i == 0) {
2024-04-25 16:32:35 +00:00
printf("Ziadne prihlasky\n");
return 1;
}
2024-04-25 15:56:47 +00:00
2024-04-25 16:42:45 +00:00
// Sort the applications alphabetically
2024-04-25 17:04:21 +00:00
qsort(applications, i, sizeof(char *), compare_names);
2024-04-22 12:01:17 +00:00
2024-04-25 17:30:56 +00:00
printf("Prijati studenti:\n");
2024-04-25 17:21:37 +00:00
for (j = 0; j < i; j++) {
2024-04-25 16:42:45 +00:00
printf("%s\n", applications[j]);
2024-04-22 12:01:17 +00:00
}
2024-04-25 16:42:45 +00:00
if (count < i) {
2024-04-25 17:30:56 +00:00
printf("Neprijati studenti:\n");
2024-04-25 16:42:45 +00:00
for (; j < i; j++) {
printf("%s\n", applications[j]);
2024-04-22 12:06:40 +00:00
}
2024-04-22 12:01:17 +00:00
}
2024-04-22 16:28:15 +00:00
2024-04-25 16:42:45 +00:00
// Free allocated memory
for (j = 0; j < i; j++) {
free(applications[j]);
}
free(applications);
2024-04-22 12:01:17 +00:00
return 0;
2024-04-25 16:42:45 +00:00
}