pvjc24/cv10/program.c

73 lines
1.6 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
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;
while (scanf("%s", buffer) == 1 && strcmp(buffer, "") != 0) {
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
for (int m = 0; m < i - 1; m++) {
for (int n = m + 1; n < i; n++) {
if (strcmp(applications[m], applications[n]) > 0) {
char *temp = applications[m];
applications[m] = applications[n];
applications[n] = temp;
}
}
}
2024-04-22 12:01:17 +00:00
2024-04-25 16:32:35 +00:00
printf("Prijati studenti:\n");
2024-04-25 16:42:45 +00:00
for (j = 0; j < (count < i ? count : i); j++) {
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 16:32:35 +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
}