usaa24/a1/program.c
Bohdan Kapliuk b96ad19785 a1
2024-10-27 22:45:45 +02:00

47 lines
1.5 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 = '\0';
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) {
if(expected != '\0'){
printf("Crossed bracket %c in %d, expected %c\n", vstup[j], j, expected);
return 0;
}
else{
printf("Unexpected closing bracket %c in %d\n", vstup[j], position[x-1]);
return 0;
}
}
x--;
expected = '\0';
break;
}
}
}
printf("All brackets OK\n");
return 0;
}