pvjc24/cv10/program.c

82 lines
1.9 KiB
C
Raw Normal View History

2024-04-23 07:41:48 +00:00
#include <stdio.h>
2024-04-17 21:09:04 +00:00
#include <stdlib.h>
#include <string.h>
2024-04-19 13:39:33 +00:00
#define MAX_NAME_LENGTH 100
int compare(const void *a, const void *b) {
2024-04-23 07:41:48 +00:00
return strcmp(*(const char **)a, *(const char **)b);
2024-04-17 21:09:04 +00:00
}
2024-04-23 08:10:22 +00:00
void free_memory(char **array, int length) {
for (int i = 0; i < length; i++) {
free(array[i]);
}
free(array);
}
2024-04-17 21:09:04 +00:00
int main() {
2024-04-19 13:39:33 +00:00
int max_students;
2024-04-23 08:10:22 +00:00
char name[MAX_NAME_LENGTH];
char **students = NULL;
int num_students = 0;
2024-04-23 07:41:48 +00:00
2024-04-23 09:22:59 +00:00
2024-04-23 09:13:00 +00:00
printf("Enter the maximum number of students that can be accepted: ");
2024-04-23 08:10:22 +00:00
if (scanf("%d\n", &max_students) != 1 || max_students <= 0) {
2024-04-23 09:13:00 +00:00
puts("Invalid input for the number of students.");
2024-04-23 07:41:48 +00:00
return 1;
2024-04-17 21:09:04 +00:00
}
2024-04-23 08:10:22 +00:00
2024-04-23 09:22:59 +00:00
2024-04-23 08:10:22 +00:00
students = (char **)malloc(max_students * sizeof(char *));
2024-04-23 07:41:48 +00:00
if (students == NULL) {
2024-04-23 09:13:00 +00:00
puts("Memory allocation failed.");
2024-04-23 07:41:48 +00:00
return 1;
}
2024-04-23 08:10:22 +00:00
2024-04-23 09:13:00 +00:00
printf("Enter student names (press Enter twice to stop):\n");
2024-04-23 09:22:59 +00:00
while (fgets(name, MAX_NAME_LENGTH, stdin) && num_students < max_students) {
name[strcspn(name, "\n")] = '\0';
if (strlen(name) == 0) {
2024-04-23 08:10:22 +00:00
break;
2024-04-23 09:22:59 +00:00
}
2024-04-23 08:10:22 +00:00
2024-04-23 09:22:59 +00:00
int duplicate = 0;
2024-04-23 08:10:22 +00:00
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i], name) == 0) {
2024-04-23 09:22:59 +00:00
duplicate = 1;
2024-04-19 13:39:33 +00:00
break;
}
}
2024-04-23 08:10:22 +00:00
2024-04-23 09:22:59 +00:00
if (!duplicate) {
students[num_students] = strdup(name);
if (students[num_students] == NULL) {
puts("Memory allocation failed.");
free_memory(students, num_students);
return 1;
2024-04-23 07:41:48 +00:00
}
2024-04-23 09:22:59 +00:00
num_students++;
2024-04-17 21:09:04 +00:00
}
}
2024-04-23 08:10:22 +00:00
if (num_students == 0) {
2024-04-23 09:13:00 +00:00
puts("No applications were received.");
2024-04-23 07:41:48 +00:00
free(students);
return 1;
2024-04-17 21:09:04 +00:00
}
2024-04-23 08:10:22 +00:00
2024-04-23 09:22:59 +00:00
qsort(students, num_students, sizeof(char *), compare);
2024-04-23 08:10:22 +00:00
2024-04-23 09:13:00 +00:00
puts("Accepted students:");
2024-04-23 08:10:22 +00:00
for (int i = 0; i < num_students; i++) {
2024-04-23 09:05:21 +00:00
puts(students[i]);
2024-04-17 21:09:04 +00:00
}
2024-04-23 08:10:22 +00:00
2024-04-23 09:22:59 +00:00
free_memory(students, num_students);
2024-04-23 08:10:22 +00:00
2024-04-17 21:09:04 +00:00
return 0;
}