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