如何在该栈上增加文本中括号是否匹配的检测?

如何在此基础上增加文本中括号是否匹配的检测?


#include
#define TRUE 1
#define FALSE 0
#define Stack_Size 20

typedef int SElemType;
typedef struct{
    SElemType elem[Stack_Size];
    int top;
}SeqStack;

void InitStack(SeqStack *S)//初始化顺序栈 
{
    S->top=-1;
}

int IsEmpty(SeqStack *S)//判断栈是否为空 
{
    if(S->top==-1) return(TRUE);
    else return(FALSE); 
}

int IsFull(SeqStack *S)//判断栈是否为满 
{
    if(S->top==Stack_Size-1) return(TRUE);
    else return(FALSE);
}

int Push(SeqStack *S,SElemType x)//入栈 
{
    if(S->top==Stack_Size-1)
    {
        printf("栈已满!");
        return(FALSE);
    }
    S->top++;
    S->elem[S->top]=x;
    return(TRUE);
}

int Pop(SeqStack *S,int x)//出栈 
{
    if(S->top==-1)
    {
        printf("栈为空!");
        return(FALSE);
    }
    else
    {
        x=S->elem[S->top];
        S->top--;
        return(TRUE);
    }
}

int GetTop(SeqStack S)
{
    if(S.top==-1)
        return(FALSE);
    int x = S.elem[S.top];
    return x;
}

int PrintStack(SeqStack S)
{
    int i;
    if(S.top==-1)
    {
        printf("栈为空");
        return(FALSE);
    }
    for( i =0;i<=S.top;i++)
    {
        printf("          |%d|\n",S.elem[S.top-i]);
    }
    return(TRUE);
}

int main()
{
    SeqStack S;
    InitStack(&S);
    int chose;
    int push,pop;
    printf("\n---------------------------------------------\n");
    printf("请选择功能:\n1:入栈   2:出栈  3:判空 4:判满  5:读取栈顶元素 6:遍历 \n");
    scanf("%d",&chose);
    while(chose==0 || chose ==1 ||chose==2 ||chose==3 ||chose==4 ||chose==5 ||chose==6 ||chose==7 )
    {
        switch(chose)
        {
            case 0:
                scanf("%d",&chose);continue;
            case 1:
                printf("请输入入栈数:");
                scanf("%d",&push);
                Push(&S,push);
                printf("入栈后为:\n");
                PrintStack(S);
                chose = 0;
                break;
            case 2:
                printf("将栈顶元素 %d 出栈\n",GetTop(S));
                Pop(&S,pop);
                printf("出栈后为:\n");
                PrintStack(S);
                chose = 0;
                break;
            case 3:
                if(IsEmpty(&S)){
                    printf("栈空\n");
                }else
                    printf("栈非空\n");
                chose = 0;
                break;
            case 4:
                if(IsFull(&S)){
                    printf("栈满\n");
                }else
                    printf("栈非满\n");
                chose = 0;
                break;
            case 5:
                printf("栈顶元素为%d",GetTop(S));
                chose = 0;
                break;
            case 6:
                PrintStack(S);
                chose = 0;
                break;
        }
        printf("\n---------------------------------------------\n");
        printf("请选择功能:\n1:入栈   2:出栈  3:判空 4:判满  5:读取栈顶元素 6:遍历  \n");
    }
    return 0;
}

仅供参考:

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define STACK_INIT_SIZE 10
#define STACK_GROW_SIZE 5
#define ELEMTYPE char
#define OK 1
#define ERROR 0
typedef struct { /*建立一个栈的首结点*/
    ELEMTYPE * base;
    ELEMTYPE * top;
    int stacksize;
} SpStack;
int InitStack(SpStack *s) { /*建立空的栈并返回首地址*/
    s->base=((ELEMTYPE*)malloc(STACK_INIT_SIZE*sizeof(ELEMTYPE)));
    if (!s->base) return ERROR;
    s->top=s->base;
    s->stacksize=STACK_INIT_SIZE;
    return OK;
}
int StackEmpty(SpStack *s) { /*判断栈是否为空*/
    if (s->top==s->base) return OK;
    else                 return ERROR;
}
int Push(SpStack *s,ELEMTYPE e) { /*往栈顶插入元素即进栈*/
    if (s->top-s->base>=s->stacksize) { /*判断是否栈满*/
        s->base=((ELEMTYPE*)realloc(s->base,(s->stacksize+STACK_GROW_SIZE)*sizeof(ELEMTYPE)));
        if (!s->base) return ERROR;
        s->stacksize+=STACK_GROW_SIZE;
        s->top=s->base+s->stacksize;
    }
    *s->top++=e;
    return OK;
}
int Pop(SpStack *s,ELEMTYPE *e) { /*让栈顶元素依次输出即出栈*/
    if (StackEmpty(s)) return ERROR;
    *e=*(--s->top);
    return OK;
}
int Comp(ELEMTYPE a,ELEMTYPE b) {
    if ((a=='('&&b!=')')
      ||(a=='['&&b!=']')
      ||(a=='{'&&b!='}')) {
        return ERROR;
    } else return OK;
}
int Count(SpStack *s) {
    ELEMTYPE e[STACK_INIT_SIZE*2];
    ELEMTYPE e1;
    int i;

    InitStack(s);
    fgets(e,STACK_INIT_SIZE*2,stdin);
    if ('\n'==e[strlen(e)-1]) e[strlen(e)-1]=0;
    printf("%s\n",e);
    for (i=0;e[i]!='\0';i++) {
        switch (e[i]) {
        case '(':
        case '[':
        case '{':
            Push(s,e[i]);
            break;
        case ')':
        case ']':
        case '}':
            if (StackEmpty(s)) {
                printf("%*s↖右括号多余\n",i+1,"");
                return(ERROR);
            } else Pop(s,&e1);
            if (!Comp(e1,e[i])) {
                printf("%*s↖左右匹配出错\n",i+1,"");
                return(ERROR);
            }
        }
    }
    if (!StackEmpty(s)) {
        printf("%*s↖左括号多余\n",i,"");
        return(ERROR);
    } else {
        printf("匹配正确\n");
        return(OK);
    }
}
void main() {
    SpStack s;
    Count(&s);
    free(s.base);
}