Initialization

This commit is contained in:
Kozar 2024-04-25 17:32:41 +02:00
parent 5b8ab43104
commit 08fef112f0

View File

@ -1,80 +1,56 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
typedef struct Student { #define MAX_STUDENTS 100
char name[100]; #define NAME_SIZE 50
int applied;
} Student;
int cmpfunc(const void *a, const void *b) { int compare_names(const void *a, const void *b) {
Student *student1 = (Student *)a; const char **name_a = a;
Student *student2 = (Student *)b; const char **name_b = b;
return strcmp(student1->name, student2->name); return strcmp(*name_a, *name_b);
} }
int main() { int main() {
int max_students, num_students; int max_students, num_accepted = 0, num_students = 0;
char name[100]; char names[MAX_STUDENTS][NAME_SIZE];
Student students[1000]; char temp_name[NAME_SIZE];
int students_accepted = 0;
int i, j;
if (scanf("%d", &max_students) != 1 || max_students <= 0) { scanf("%d", &max_students);
puts("Nespravny vstup"); if (max_students <= 0 || max_students > MAX_STUDENTS) {
printf("Nespravny vstup\n");
return 1; return 1;
} }
if (getchar() == '\n') { memset(names, 0, sizeof(names));
puts("Nespravny vstup");
return 1;
}
if (scanf("%d", &num_students) != 1 || num_students <= 0) { while (fgets(temp_name, sizeof(temp_name), stdin) != NULL && num_accepted < max_students) {
puts("Nespravny vstup");
return 1;
}
if (num_students > max_students) { int i, found = 0;
num_students = max_students;
}
for (i = 0; i < num_students; i++) { for (i = 0; i < num_accepted; i++) {
if (scanf("%s", name) != 1) { if (strcmp(names[i], temp_name) == 0) {
puts("Ziadne prihlasky"); found = 1;
return 1;
}
for (j = 0; j < i; j++) {
if (strcmp(students[j].name, name) == 0) {
break; break;
} }
} }
if (j == i) { if (!found) {
strcpy(students[i].name, name); strcpy(names[num_accepted], temp_name);
students[i].applied = 1; num_accepted++;
} }
num_students++;
} }
qsort(students, i, sizeof(Student), cmpfunc); // sort the names by alphabet
qsort(names, num_accepted, sizeof(char*) * NAME_SIZE, compare_names);
printf("Prijati students:\n"); if (num_accepted == 0) {
for (i = 0; i < max_students; i++) { printf("Ziadne prihlasky");
if (students[i].applied == 1) {
printf("%s\n", students[i].name);
students_accepted++;
} else { } else {
break; printf("Prijati studenti:\n");
} for (int i = 0; i < num_accepted; i++) {
} printf("%s", names[i]);
if (students_accepted < num_students) {
printf("Neprijati studenti:\n");
for (i = 0; i < num_students; i++) {
if (students[i].applied == 0) {
printf("%s\n", students[i].name);
}
} }
} }