求指点迷津,代码是否有问题是否规范
#include<stdio.h>
#define SIZE 8
typedef char element;
typedef struct{
element data[StackSize+1];//0号位不存储
int base;
int top;
}stack;
stack* createstack(stack *S);//构造栈
int push(stack* S, element e);//入栈
int pop(stack* S, element *e);//出栈
int length(stack* S);//若栈存在,求长度
int main(int argc, char* argv[]) {
char brace[SIZE];//存储用户输入
char ch;//读取输入
int i;//记录输入的元素
stack p;
stack * Data = &p;
createstack(Data);
printf("输入括号(),{}.[],#结束\n");
for (i = 0; i < SIZE; i++) {
scanf("%c", &ch);
if (ch == '#') break;
brace[i] = ch;
}
for (int j = 0; j < i; j++) {
if (brace[j] == '(' || brace[j] == '{' || brace[j] == '[') {
push(Data, brace[j]);
}
}
if (!length(Data)) {
printf("栈空,匹配失败\n");
return 0;
}
int index = 0;
for (int j = 0; j < i; j++) {
if (brace[j] == ')' || brace[j] == '}' || brace[j] == ']') {
pop(Data, &ch);
if (ch == '(' && brace[j] == ')') {
index = 1;
}
else if (ch == '{' && brace[j] == '}') {
index = 1;
}
else if (ch == '[' && brace[j] == ']') {
index = 1;
}
else {
index = 0;
}
}
}
if (length(Data)) {//使用求长度判断栈内是否有元素
printf("匹配失败\n");
return 0;
}
if (index) printf("匹配成功");
else printf("匹配失败");
return 0;
}
stack* createstack(stack* S){
S->data[0] = 0;
S->top = 1;
S->base = 1;
return S;
}
int push(stack* S, element e) {
if (S == NULL) {
printf("栈不存在\n");
return 0;
}
if (S->top - S->base == StackSize) {//栈满
return 0;
}
S->data[S->top] = e;
S->top++;
return 1;
}
int pop(stack* S, element *e) {
if (S == NULL) {
printf("栈不存在\n");
return 0;
}
if (S->top == S->base) {
printf("栈空\n");
return 0;
}
S->top--;
*e = S->data[S->top];
return 1;
}
int length(stack* S) {
return S->top -1;//栈空,base = top = 1,则长度为0
}