Update 'du6/program.c'

This commit is contained in:
Anzhelika Nikolaieva 2023-04-04 15:24:27 +00:00
parent f896c918c2
commit b0c07d2ae8

View File

@ -1,62 +1,81 @@
#include <stdio.h> include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#define MAX_STUDENTS 1000 // Maximum number of students
int main() { int main() {
int num_students, i;
char names[MAX_STUDENTS][101];
char name[101];
// Read the maximum number of students char database[200][30];
if (scanf("%d", &num_students) != 1) { char string[30];
puts("Nespravny vstup"); int error = 0;
return 1; int count = 0;
} int i = 0;
if (num_students <= 0 || num_students > MAX_STUDENTS) {
puts("Nespravny vstup"); memset(database, 0, sizeof(database));
return 1; memset(string, 0, 30);
} char *r;
r = fgets(string, 1000, stdin);
if (r == NULL)
return 0;
sscanf(string, "%d", &count);
if(strlen(r) == 1 || count < 1) {
puts("Nespravny vstup");
return 0;
}
for (int c = 0; c < 30; c++) {
if (isalpha(r[c])) {
error = 1;
break;
}
}
if (error != 0) {
puts("Nespravny vstup");
return 0;
}
// Read the list of applications while (strlen(r) > 1) {
for (i = 0; i < MAX_STUDENTS; i++) { memset(string, 0, 30);
if (fgets(name, 101, stdin) == NULL || strcmp(name, "\n") == 0) { r = fgets(string, 30, stdin);
break; // Stop reading when encountering an empty line or EOF if (r == NULL)
} break;
name[strcspn(name, "\n")] = '\0'; // Remove the trailing newline character error = 0;
strcpy(names[i], name); // Add the name to the list for (int j = 0; j < 200; j++) {
}
if (i == 0) { if (strcmp(database[j], string) == 0) {
puts("Ziadne prihlasky"); error = 1;
return 1; break;
} }
// Sort the list of names in alphabetical order }
for (int j = 0; j < i - 1; j++) {
for (int k = j + 1; k < i; k++) {
if (strcmp(names[j], names[k]) > 0) {
char temp[101];
strcpy(temp, names[j]);
strcpy(names[j], names[k]);
strcpy(names[k], temp);
}
}
}
// Print the list of accepted students if (error == 0 && strlen(r) > 1) {
puts("Prijati studenti:"); strcpy(database[i], r);
for (int j = 0; j < num_students && j < i; j++) { i++;
printf("%s\n", names[j]); }
}
// Print the list of rejected students }
if (i > num_students) { if (i == 0) {
puts("Neprijati studenti:"); puts("Ziadne prihlasky");
for (int j = num_students; j < i; j++) { return 0;
printf("%s\n", names[j]); }
} for (int j = 0; j < i; j++) {
} for (int k = 0; k < i; k++) {
if (strcmp(database[j], database[k]) < 0) {
return 0; char buffer[30];
//memset(buffer, 0, sizeof(buffer));
strcpy(buffer, database[j]);
strcpy(database[j], database[k]);
strcpy(database[k], buffer);
}
}
}
puts("Prijati studenti:");
for (int j = 0; j < i; j++) {
if (j == count) {
puts("Neprijati studenti:");
}
printf("%s", database[j]);
}
return 0;
} }