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>
#define MAX_STUDENTS 1000 // Maximum number of students
#include <ctype.h>
int main() {
int num_students, i;
char names[MAX_STUDENTS][101];
char name[101];
// Read the maximum number of students
if (scanf("%d", &num_students) != 1) {
puts("Nespravny vstup");
return 1;
}
if (num_students <= 0 || num_students > MAX_STUDENTS) {
puts("Nespravny vstup");
return 1;
}
char database[200][30];
char string[30];
int error = 0;
int count = 0;
int i = 0;
memset(database, 0, sizeof(database));
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
for (i = 0; i < MAX_STUDENTS; i++) {
if (fgets(name, 101, stdin) == NULL || strcmp(name, "\n") == 0) {
break; // Stop reading when encountering an empty line or EOF
}
name[strcspn(name, "\n")] = '\0'; // Remove the trailing newline character
strcpy(names[i], name); // Add the name to the list
}
while (strlen(r) > 1) {
memset(string, 0, 30);
r = fgets(string, 30, stdin);
if (r == NULL)
break;
error = 0;
for (int j = 0; j < 200; j++) {
if (i == 0) {
puts("Ziadne prihlasky");
return 1;
}
if (strcmp(database[j], string) == 0) {
error = 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
puts("Prijati studenti:");
for (int j = 0; j < num_students && j < i; j++) {
printf("%s\n", names[j]);
}
if (error == 0 && strlen(r) > 1) {
strcpy(database[i], r);
i++;
}
// 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 (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;
}