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");
return 1;
}
// Read the list of applications memset(database, 0, sizeof(database));
for (i = 0; i < MAX_STUDENTS; i++) { memset(string, 0, 30);
if (fgets(name, 101, stdin) == NULL || strcmp(name, "\n") == 0) { char *r;
break; // Stop reading when encountering an empty line or EOF r = fgets(string, 1000, stdin);
} if (r == NULL)
name[strcspn(name, "\n")] = '\0'; // Remove the trailing newline character return 0;
strcpy(names[i], name); // Add the name to the list 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;
}
if (i == 0) {
puts("Ziadne prihlasky");
return 1;
}
// Sort the list of names in alphabetical order while (strlen(r) > 1) {
for (int j = 0; j < i - 1; j++) { memset(string, 0, 30);
for (int k = j + 1; k < i; k++) { r = fgets(string, 30, stdin);
if (strcmp(names[j], names[k]) > 0) { if (r == NULL)
char temp[101]; break;
strcpy(temp, names[j]); error = 0;
strcpy(names[j], names[k]); for (int j = 0; j < 200; j++) {
strcpy(names[k], temp);
}
}
}
// Print the list of accepted students if (strcmp(database[j], string) == 0) {
puts("Prijati studenti:"); error = 1;
for (int j = 0; j < num_students && j < i; j++) { break;
printf("%s\n", names[j]); }
}
// Print the list of rejected students }
if (i > num_students) {
puts("Neprijati studenti:");
for (int j = num_students; j < i; j++) {
printf("%s\n", names[j]);
}
}
return 0; if (error == 0 && strlen(r) > 1) {
strcpy(database[i], r);
i++;
}
}
if (i == 0) {
puts("Ziadne prihlasky");
return 0;
}
for (int j = 0; j < i; j++) {
for (int k = 0; k < i; k++) {
if (strcmp(database[j], database[k]) < 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;
} }