diff --git a/a2/program.c b/a2/program.c index 18e7180..ba6bcfb 100644 --- a/a2/program.c +++ b/a2/program.c @@ -3,6 +3,7 @@ #include #include #include +#include struct stack{ char data[100]; @@ -67,8 +68,7 @@ char gettop(){ int main(){ - //struct stack mystack; - //memset(&mystack, 0 , sizeof(struct stack)); + char line[100]; s.size = -1; @@ -78,29 +78,34 @@ int main(){ 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; + + char a = gettop(); + if(a != '(' || a != '{' || a != '<' || a != '['){ + printf("Unexpected closing brocket %c in %d\n", line[i], i); + return 0; - } - else{ - pop(); - } + } + else if(s.size == -1 && !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(); + } } + + } + if(s.size == -1){ + printf("All brackest are OK\n"); } - printf("All brackest are OK\n"); - - return 0; }