pvjc24/cv10/program.c

75 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;
int i, j;
2024-04-22 12:06:40 +00:00
2024-04-25 18:38:17 +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) {
applications[i] = strdup(buffer);
i++;
}
2024-04-25 16:42:45 +00:00
}
2024-04-25 18:38:17 +00:00
if (i!= count) {
printf("Nespravny vstup\n");
for (j = 0; j < i; j++) {
free(applications[j]);
}
free(applications);
return 1;
}
2024-04-25 18:28:58 +00:00
if (i == 0) {
printf("Ziadne prihlasky\n");
2024-04-25 18:38:17 +00:00
free(applications);
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 18:28:58 +00:00
qsort(applications, i, sizeof(char *), compare_names);
2024-04-22 12:01:17 +00:00
2024-04-25 18:31:47 +00:00
printf("Prijati studenti:");
2024-04-25 18:28:58 +00:00
for (j = 0; j < i; j++) {
2024-04-25 18:38:17 +00:00
printf("\n%s", 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 18:38:17 +00:00
}