#include #include #include #include #include struct stack{ char data[100]; int size; }s; void push(char ch){ if(s.size < 100){ assert(s.size < 100); s.size++; s.data[s.size] = ch; } else{ exit(0); } } void pop(){ if(s.size > 0){ assert(s.size > 0); s.size--; } else { exit(0); } } bool find_pair(char a, char b){ if(a == '(' && b == ')') return true; else if(a == '{' && b == '}') return true; else if(a == '<' && b == '>') return true; else if(a == '[' && b == ']') return true; return false; } char right_pair(char a){ char b; if(a == '('){ b = ')'; } else if(a == '{'){ b = '}'; } else if(a == '['){ b = ']'; } else if(a == '<'){ b = '>'; } return b; } char gettop(){ return s.data[s.size]; } int main(){ //struct stack mystack; //memset(&mystack, 0 , sizeof(struct stack)); char line[100]; s.size = -1; printf("Read: "); char *x = fgets(line, 100, stdin); int l = strlen(line); int i, j; for(i = 0; i < l; i++){ if(line[i] == '{' || line[i] == '(' || line[i] == '[' || line[i] == '<'){ push(line[i]); } else if(line[i] == '}' || line[i] == ')' || line[i] == ']' || line[i] == '>'){ char a = gettop(); //printf("%c", a); if(!find_pair(gettop(), line[i])){ char b = right_pair(a); printf("Crossed bracket %c in %d, expected %c\n", line[i], i, b); return 0; } else{ pop(); } } } printf("All brackest are OK\n"); return 0; }