usaa24/a1/program.c
Bohdan Kapliuk d723f30ad8 a1
2024-10-27 22:38:39 +02:00

44 lines
1.3 KiB
C

#include <stdio.h>
#include <string.h>
#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);
printf("Read: %s", vstup);
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("Unexpected closing bracket %c in %d\n", stack[x-1], position[x-1]);
} else {
printf("All brackets OK\n");
}
return 0;
}