This commit is contained in:
Nataliia Kobryn 2025-10-20 19:37:43 +02:00
parent 8d9bc08d2f
commit a4d2f90f18
2 changed files with 16 additions and 11 deletions

BIN
a1/prog

Binary file not shown.

View File

@ -56,42 +56,47 @@ char getExpectedClosing(char open) {
default: return '?'; default: return '?';
} }
} }
int main() { int main() {
char input[MAX_SIZE]; char input[MAX_SIZE];
if (fgets(input, MAX_SIZE, stdin) != NULL) { if (fgets(input, MAX_SIZE, stdin) != NULL) {
input[strcspn(input, "\n")] = '\0'; input[strcspn(input, "\n")] = '\0';
printf("Read: %s\n", input); // Додано "Read: " printf("Read: %s\n", input);
int err = 0; int err = 0;
if (is_valid_brackets(input, &err)) { if (is_valid_brackets(input, &err)) {
printf("All brackets OK\n"); // Змінено з "True" printf("All brackets OK\n");
} else { } else {
char stack[MAX_SIZE]; char stack[MAX_SIZE];
int top = -1; int top = -1;
for (int i = 0; i < strlen(input) ; i++) {
for (int i = 0; i < err; i++) {
char c = input[i]; char c = input[i];
if (c == '(' || c == '[' || c == '{' || c == '<') { if (c == '(' || c == '[' || c == '{' || c == '<') {
stack[++top] = c; stack[++top] = c;
} else if (c == ')' || c == ']' || c == '}' || c == '>') { } else if (c == ')' || c == ']' || c == '}' || c == '>') {
top--; if (top >= 0 && c == getExpectedClosing(stack[top])) {
top--;
} else {
printf("Crossed bracket %c in %d, expected %c\n",
c, err, (top >= 0) ? getExpectedClosing(stack[top]) : '?');
return 0;
}
} }
} }
char c = input[err - 1]; char c = input[err - 1];
if (c == ')' || c == ']' || c == '}' || c == '>') { if (c == ')' || c == ']' || c == '}' || c == '>') {
if (top <-1) { if (top < -1) {
printf("Unexpected closing bracket %c in %d\n", c, err-1); printf("Unexpected closing bracket %c in %d\n", c, err);
} else {
printf("Crossed bracket %c in %d, expected %c \n", c, err-1, getExpectedClosing(stack[err-2]));
} }
} else { } else {
printf("Missing closing brackets: "); printf("Missing closing brackets: ");
for (int i = top; i > -1; i--) { for (int i = top; i > -1; i--) {
printf("%c", getExpectedClosing(stack[i])); printf("%c", getExpectedClosing(stack[i]));
} }
printf("\n"); printf("\n");
} }
} }
} else { } else {
printf("Error reading input\n"); printf("Error reading input\n");