2020-10-20 19:25:35 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
2020-10-21 09:29:36 +00:00
|
|
|
#include <ctype.h>
|
2020-10-20 19:25:35 +00:00
|
|
|
|
|
|
|
struct stack{
|
|
|
|
char data[100];
|
|
|
|
int size;
|
2020-10-21 14:14:51 +00:00
|
|
|
}stack;
|
2020-10-20 19:25:35 +00:00
|
|
|
|
2020-10-31 10:59:00 +00:00
|
|
|
///add bracket to stack and increment size
|
2020-10-26 13:58:37 +00:00
|
|
|
|
2020-10-20 19:25:35 +00:00
|
|
|
void push(char ch){
|
2020-10-21 14:14:51 +00:00
|
|
|
if(stack.size < 100){
|
|
|
|
stack.size++;
|
|
|
|
stack.data[stack.size] = ch;
|
2020-10-20 19:25:35 +00:00
|
|
|
}
|
2020-10-21 12:21:10 +00:00
|
|
|
else
|
2020-10-20 19:25:35 +00:00
|
|
|
exit(0);
|
2020-10-26 13:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///if i have opening bracket in stack, then decrement size
|
2020-10-20 19:25:35 +00:00
|
|
|
|
2020-10-21 12:46:30 +00:00
|
|
|
void pop(){
|
2020-10-21 14:14:51 +00:00
|
|
|
if(stack.size >= 0)
|
|
|
|
stack.size--;
|
2020-10-21 12:21:10 +00:00
|
|
|
else
|
2020-10-20 19:25:35 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2020-10-26 13:58:37 +00:00
|
|
|
///compare brackets
|
|
|
|
|
2020-10-20 19:25:35 +00:00
|
|
|
bool find_pair(char a, char b){
|
|
|
|
if(a == '(' && b == ')')
|
|
|
|
return true;
|
2020-10-21 12:21:10 +00:00
|
|
|
if(a == '{' && b == '}')
|
2020-10-20 19:25:35 +00:00
|
|
|
return true;
|
2020-10-21 12:21:10 +00:00
|
|
|
if(a == '<' && b == '>')
|
2020-10-20 19:25:35 +00:00
|
|
|
return true;
|
2020-10-21 12:21:10 +00:00
|
|
|
if(a == '[' && b == ']')
|
2020-10-20 19:25:35 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-26 13:58:37 +00:00
|
|
|
|
|
|
|
///function to find right pair
|
|
|
|
|
2020-10-20 19:25:35 +00:00
|
|
|
char right_pair(char a){
|
|
|
|
char b;
|
2020-10-21 12:21:10 +00:00
|
|
|
if(a == '(')
|
2020-10-20 19:25:35 +00:00
|
|
|
b = ')';
|
2020-10-21 12:21:10 +00:00
|
|
|
if(a == '{')
|
2020-10-20 19:25:35 +00:00
|
|
|
b = '}';
|
2020-10-21 12:21:10 +00:00
|
|
|
if(a == '[')
|
2020-10-20 19:25:35 +00:00
|
|
|
b = ']';
|
2020-10-21 12:21:10 +00:00
|
|
|
if(a == '<')
|
2020-10-20 19:25:35 +00:00
|
|
|
b = '>';
|
2020-10-21 12:21:10 +00:00
|
|
|
|
2020-10-20 19:25:35 +00:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2020-10-26 13:58:37 +00:00
|
|
|
///find the last opening bracket in the stack
|
|
|
|
|
2020-10-20 19:25:35 +00:00
|
|
|
char gettop(){
|
2020-10-21 14:14:51 +00:00
|
|
|
return stack.data[stack.size];
|
2020-10-20 19:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(){
|
|
|
|
|
2020-10-21 09:29:36 +00:00
|
|
|
|
2020-10-20 19:25:35 +00:00
|
|
|
char line[100];
|
2020-10-21 14:14:51 +00:00
|
|
|
stack.size = -1;
|
2020-10-20 19:29:41 +00:00
|
|
|
|
|
|
|
printf("Read: ");
|
2020-10-20 19:25:35 +00:00
|
|
|
char *x = fgets(line, 100, stdin);
|
2020-10-21 12:21:10 +00:00
|
|
|
printf("%s", line);
|
2020-10-20 19:25:35 +00:00
|
|
|
|
|
|
|
int l = strlen(line);
|
2020-10-21 14:14:51 +00:00
|
|
|
int i, j = -1, f = -1;;
|
2020-10-26 13:58:37 +00:00
|
|
|
char *o_bracket = calloc(l, sizeof(char));
|
|
|
|
char *c_bracket = calloc(l, sizeof(char));
|
2020-10-21 12:21:10 +00:00
|
|
|
|
2020-10-20 19:25:35 +00:00
|
|
|
for(i = 0; i < l; i++){
|
|
|
|
if(line[i] == '{' || line[i] == '(' || line[i] == '[' || line[i] == '<'){
|
|
|
|
push(line[i]);
|
2020-10-21 12:54:51 +00:00
|
|
|
j++;
|
2020-10-21 14:14:51 +00:00
|
|
|
f++;
|
2020-10-26 13:58:37 +00:00
|
|
|
o_bracket[j] = gettop(); //get opening bracket from the stack
|
|
|
|
c_bracket[f] = right_pair(o_bracket[j]); //get closing bracket
|
2020-10-20 19:25:35 +00:00
|
|
|
}
|
2020-10-26 13:58:37 +00:00
|
|
|
if(line[i] == '}' || line[i] == ')' || line[i] == ']' || line[i] == '>'){
|
|
|
|
|
|
|
|
if(stack.size == -1){ //if there is no opening bracket in the stack
|
2020-10-21 12:21:10 +00:00
|
|
|
printf("Unexpected closing bracket %c in %d\n", line[i], i);
|
2020-10-21 09:29:36 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
2020-10-26 13:58:37 +00:00
|
|
|
else if(!find_pair(gettop(), line[i])){ //if there is another closing bracket
|
|
|
|
printf("Crossed bracket %c in %d, expected %c \n", line[i], i, c_bracket[f]);
|
2020-10-21 09:29:36 +00:00
|
|
|
return 0;
|
2020-10-21 12:21:10 +00:00
|
|
|
}
|
|
|
|
else{
|
2020-10-26 13:58:37 +00:00
|
|
|
c_bracket[f] = 0; //if closing bracket is correct then reducee array
|
2020-10-31 10:59:00 +00:00
|
|
|
f--; //if i didn't find a correct bracket, then keep it in the array c_bracket
|
2020-10-21 09:29:36 +00:00
|
|
|
pop();
|
2020-10-21 12:21:10 +00:00
|
|
|
}
|
2020-10-20 19:25:35 +00:00
|
|
|
}
|
2020-10-21 09:29:36 +00:00
|
|
|
}
|
2020-10-21 14:14:51 +00:00
|
|
|
|
2020-10-21 12:46:30 +00:00
|
|
|
int begin = 0;
|
2020-10-26 13:58:37 +00:00
|
|
|
int end = strlen(c_bracket);
|
2020-10-21 14:14:51 +00:00
|
|
|
int temp;
|
2020-10-26 13:58:37 +00:00
|
|
|
|
|
|
|
//now i have all missing closing brackets in array
|
|
|
|
|
|
|
|
//flip array
|
2020-10-21 12:46:30 +00:00
|
|
|
while(begin < end){
|
2020-10-26 13:58:37 +00:00
|
|
|
temp = c_bracket[begin];
|
|
|
|
c_bracket[begin++] = c_bracket[--end];
|
|
|
|
c_bracket[end] = temp;
|
2020-10-21 12:46:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-26 13:58:37 +00:00
|
|
|
//if program pop all opening brackets from the stack, then stack size must be -1;
|
2020-10-21 14:14:51 +00:00
|
|
|
if(stack.size == -1){
|
2020-10-21 12:21:10 +00:00
|
|
|
printf("All brackets OK\n");
|
2020-10-20 19:25:35 +00:00
|
|
|
}
|
2020-10-26 13:58:37 +00:00
|
|
|
//if no, then program didn't find closing bracket
|
2020-10-21 12:21:10 +00:00
|
|
|
else {
|
2020-10-26 13:58:37 +00:00
|
|
|
printf("Missing closing brackets: %s\n", c_bracket);
|
2020-10-21 12:21:10 +00:00
|
|
|
}
|
2020-10-26 13:58:37 +00:00
|
|
|
|
|
|
|
free(o_bracket);
|
|
|
|
free(c_bracket);
|
|
|
|
|
2020-10-20 19:25:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|