Initialization

This commit is contained in:
Kozar 2024-04-25 17:29:10 +02:00
parent 06701bbaee
commit 23dc469d67

View File

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