标签:type std length pre 操作 sem highlight size %s
#include<stdio.h>
#define MaxSize 10
typedef char ElemType;
typedef struct
{
ElemType data[MaxSize];
int top;
}SList;
void InitSList(SList &S)
{
S.top=-1;
}
bool IsEmpty(SList S)
{
if(S.top==-1)
return true;
return false;
}
void Push(SList &S,ElemType e)
{
if(S.top==MaxSize-1)
return ;
S.data[++S.top]=e;
}
void Pop(SList &S,ElemType &e)
{
if(S.top==-1)
return ;
e=S.data[S.top--];
}
bool Check(ElemType str[],int length)
{
SList S; //创建一个栈
InitSList(S); //初始化栈
for(int i=0;i<length;i++)
{
if(str[i]==‘(‘ || str[i]==‘[‘ || str[i]==‘{‘) //左括号入栈
Push(S,str[i]);
else
{
if(IsEmpty(S)) //栈为空不匹配
return false;
ElemType E;
Pop(S,E); //出栈
if(E!=‘(‘ && str[i]==‘)‘)
return false;
if(E!=‘[‘ && str[i]==‘]‘)
return false;
if(E!=‘{‘ && str[i]==‘}‘)
return false;
}
}
return IsEmpty(S); //全部操作后栈为空匹配
}
void main()
{
ElemType str[]={‘(‘,‘[‘,‘]‘,‘)‘,‘(‘};
for(int i=0;i<5;i++)
printf("%c\t",str[i]);
printf("\n");
printf("括号%s\n",Check(str,5)?"匹配":"不匹配");
}
标签:type std length pre 操作 sem highlight size %s
原文地址:https://www.cnblogs.com/-slz-2/p/13232927.html