“program”

This commit is contained in:
Vasylenko 2024-04-23 11:13:00 +02:00
parent 7bd8fbba85
commit a5ea74f79c

View File

@ -21,37 +21,36 @@ int main() {
char **students = NULL; char **students = NULL;
int num_students = 0; int num_students = 0;
printf("Enter the maximum number of students that can be accepted: ");
if (scanf("%d\n", &max_students) != 1 || max_students <= 0) { if (scanf("%d\n", &max_students) != 1 || max_students <= 0) {
puts("Nespravny vstup"); puts("Invalid input for the number of students.");
return 1; return 1;
} }
students = (char **)malloc(max_students * sizeof(char *)); students = (char **)malloc(max_students * sizeof(char *));
if (students == NULL) { if (students == NULL) {
puts("Memory allocation failed"); puts("Memory allocation failed.");
return 1; return 1;
} }
printf("Enter student names (press Enter twice to stop):\n");
while (fgets(name, MAX_NAME_LENGTH, stdin)) { while (fgets(name, MAX_NAME_LENGTH, stdin)) {
name[strcspn(name, "\n")] = 0; if (name[strcspn(name, "\n")] = '\0', !*name)
if (strlen(name) == 0) {
break; break;
}
int duplicate = 0; int is_duplicate = 0;
for (int i = 0; i < num_students; i++) { for (int i = 0; i < num_students; i++) {
if (strcmp(students[i], name) == 0) { if (strcmp(students[i], name) == 0) {
duplicate = 1; is_duplicate = 1;
break; break;
} }
} }
if (!duplicate) { if (!is_duplicate) {
if (num_students < max_students) { if (num_students < max_students) {
students[num_students] = strdup(name); students[num_students] = strdup(name);
if (students[num_students] == NULL) { if (students[num_students] == NULL) {
puts("Memory allocation failed"); puts("Memory allocation failed.");
free_memory(students, num_students); free_memory(students, num_students);
return 1; return 1;
} }
@ -61,14 +60,14 @@ int main() {
} }
if (num_students == 0) { if (num_students == 0) {
puts("Ziadne prihlasky"); puts("No applications were received.");
free(students); free(students);
return 1; return 1;
} }
qsort(students, num_students, sizeof(char *), compare); qsort(students, num_students, sizeof(char *), compare);
puts("Prijati studenti:"); puts("Accepted students:");
for (int i = 0; i < num_students; i++) { for (int i = 0; i < num_students; i++) {
puts(students[i]); puts(students[i]);
} }
@ -77,3 +76,4 @@ int main() {
return 0; return 0;
} }