usaa20/a2/program.c

121 lines
2.4 KiB
C
Raw Normal View History

2020-10-20 19:25:35 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <assert.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
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-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);
}
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;
}
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;
}
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;;
char *l_bracket = calloc(l, sizeof(char));
char *r_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++;
l_bracket[j] = gettop();
r_bracket[f] = right_pair(l_bracket[j]);
2020-10-20 19:25:35 +00:00
}
2020-10-21 12:21:10 +00:00
if(line[i] == '}' || line[i] == ')' || line[i] == ']' || line[i] == '>'){
2020-10-21 14:14:51 +00:00
if(stack.size == -1){
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-21 14:14:51 +00:00
else if(!find_pair(gettop(), line[i])){
printf("Crossed bracket %c in %d, expected %c \n", line[i], i, r_bracket[j]);
2020-10-21 09:29:36 +00:00
return 0;
2020-10-21 12:21:10 +00:00
}
else{
2020-10-21 14:14:51 +00:00
r_bracket[f] = 0;
f--;
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-21 14:14:51 +00:00
int end = strlen(r_bracket);
int temp;
//printf("%s", b);
2020-10-21 12:46:30 +00:00
while(begin < end){
2020-10-21 14:14:51 +00:00
temp = r_bracket[begin];
r_bracket[begin++] = r_bracket[--end];
r_bracket[end] = temp;
2020-10-21 12:46:30 +00:00
}
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-21 12:21:10 +00:00
else {
2020-10-21 14:14:51 +00:00
printf("Missing closing brackets: %s\n", r_bracket);
2020-10-21 12:21:10 +00:00
}
2020-10-21 14:14:51 +00:00
free(l_bracket);
free(r_bracket);
2020-10-20 19:25:35 +00:00
return 0;
}