usaa20/a2/program.c

117 lines
2.2 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;
}s;
void push(char ch){
if(s.size < 100){
s.size++;
s.data[s.size] = ch;
}
2020-10-21 12:21:10 +00:00
else
2020-10-20 19:25:35 +00:00
exit(0);
}
2020-10-21 12:21:10 +00:00
char pop(){
2020-10-20 19:25:35 +00:00
if(s.size > 0){
s.size--;
2020-10-21 12:21:10 +00:00
return (s.data[s.size]);
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);
}
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(){
return s.data[s.size];
}
int main(){
2020-10-21 09:29:36 +00:00
2020-10-20 19:25:35 +00:00
char line[100];
s.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 12:21:10 +00:00
int i, j = 0;
char *a = calloc(l, sizeof(char));
char *b = calloc(l, sizeof(char));
2020-10-20 19:25:35 +00:00
for(i = 0; i < l; i++){
2020-10-21 12:21:10 +00:00
//a[i] = gettop();
// b[i] = right_pair(a[i]);
2020-10-20 19:25:35 +00:00
if(line[i] == '{' || line[i] == '(' || line[i] == '[' || line[i] == '<'){
push(line[i]);
2020-10-21 12:21:10 +00:00
a[j] = gettop();
b[j] = right_pair(a[j]);
j++;
//printf("%c", b[i]);
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] == '>'){
//a[i] = gettop();
//b[i] = right_pair(a[i]);
if(s.size == -1){
printf("Unexpected closing bracket %c in %d\n", line[i], i);
2020-10-21 09:29:36 +00:00
return 0;
}
2020-10-21 12:21:10 +00:00
else if(!find_pair(gettop(), line[i])){
2020-10-21 09:29:36 +00:00
printf("Crossed bracket %c in %d, expected %c\n", line[i], i, b);
return 0;
2020-10-21 12:21:10 +00:00
}
else{
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 12:21:10 +00:00
i = 0;
2020-10-21 09:29:36 +00:00
if(s.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 {
printf("Missing closing brackets: %s \n", b);
}
free(a);
free(b);
2020-10-20 19:25:35 +00:00
return 0;
}