This commit is contained in:
Bohdan Kapliuk 2024-10-27 22:32:13 +02:00
parent ad46eb5145
commit 8457a6d283

View File

@ -1,8 +1,44 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main(){
#define LINESIZE 100
int main() {
char vstup[LINESIZE];
char *brackets = "{[(<";
char *brackets1 = "}])>";
char stack[LINESIZE];
int position[LINESIZE];
int x = 0;
fgets(vstup, LINESIZE, stdin);
for (int j = 0; j < strlen(vstup); j++) {
for (int i = 0; i < 4; i++) {
if (vstup[j] == brackets[i]) {
stack[x] = brackets[i];
position[x] = j;
x++;
break;
} else if (vstup[j] == brackets1[i]) {
char expected;
if (stack[x-1] == '{') expected = '}';
else if (stack[x-1] == '[') expected = ']';
else if (stack[x-1] == '(') expected = ')';
else if (stack[x-1] == '<') expected = '>';
if (vstup[j] != expected) {
printf("Crossed bracket %c in %d, expected %c\n", vstup[j], j, expected);
return 0;
}
x--;
break;
}
}
}
if (x > 0) {
printf("Unmatched opening bracket %c at position %d\n", stack[x-1], position[x-1]);
} else {
printf("All brackets OK\n");
}
return 0;
}