Initialization

This commit is contained in:
Kozar 2024-04-22 14:06:40 +02:00
parent 9df41d91f7
commit 33387e35b1

View File

@ -1,51 +1,50 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#define SIZE 100 #define MAX_STUDENTS 100
#define NAME_SIZE 50
int compare(const void* arg1, const void* arg2) {
char* s1 = *((char**)arg1);
char* s2 = *((char**)arg2);
return strcmp(s1, s2);
}
int main() { int main() {
char* pole_smernikov[SIZE]; int max_students, num_accepted = 0;
memset(pole_smernikov, 0, SIZE * sizeof(char*)); char names[MAX_STUDENTS][NAME_SIZE];
char temp_name[NAME_SIZE];
char line[SIZE]; scanf("%d", &max_students);
memset(line, 0, SIZE); if (max_students <= 0 || max_students > MAX_STUDENTS) {
printf("Nespravny vstup\n");
return 1;
}
int pocet_mien_v_poli = 0; while (scanf("%s", temp_name) != EOF && num_accepted < max_students) {
int i, found = 0;
while (fgets(line, SIZE, stdin) != NULL && strlen(line) > 0) { for (i = 0; i < num_accepted; i++) {
int found = 0; if (strcmp(names[i], temp_name) == 0) {
for (int i = 0; i < pocet_mien_v_poli; i++) {
if (memcmp(pole_smernikov[i], line, strlen(line)) == 0) {
found = 1; found = 1;
break; break;
} }
} }
if (!found) { if (!found) {
pole_smernikov[pocet_mien_v_poli] = malloc(strlen(line) + 1); strcpy(names[num_accepted], temp_name);
memcpy(pole_smernikov[pocet_mien_v_poli], line, strlen(line) + 1); num_accepted++;
pocet_mien_v_poli++;
} }
memset(line, 0, SIZE);
} }
qsort(pole_smernikov, pocet_mien_v_poli, sizeof(char*), compare); if (num_accepted == 0) {
printf("Ziadne prihlasky\n");
printf("Prijati studenti:\n"); } else {
for (int i = 0; i < pocet_mien_v_poli; i++) { printf("Prijati studenti:\n");
printf("%s", pole_smernikov[i]); for (int i = 0; i < num_accepted; i++) {
printf("%s\n", names[i]);
}
} }
for (int i = 0; i < pocet_mien_v_poli; i++) { if (num_accepted < max_students) {
free(pole_smernikov[i]); printf("Neprijati studenti:\n");
for (int i = num_accepted; i < max_students; i++) {
printf("%s\n", names[i]);
}
} }
return 0; return 0;