LX括号配对
#include<malloc.h>
#include<stdio.h>
#include<math.h>
#include<process.h> // exit()
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef char SElemType;
typedef int Status; // Status是函数的类型,其值是函数结果状态代码,如OK等
#define STACK_INIT_SIZE 10 // 存储空间初始分配量
#define STACKINCREMENT 2 // 存储空间分配增量
struct SqStack
{
SElemType *base; // 在栈构造之前和销毁之后,base的值为NULL
SElemType *top; // 栈顶指针
int stacksize; // 当前已分配的存储空间,以元素为单位
}; // 顺序栈
Status InitStack(SqStack &S)
{
S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
S.top=S.base;
if(!S.base) return ERROR;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status StackEmpty(SqStack S)
{
if(S.base==S.top) return TRUE;
else return FALSE;
}
Status Push(SqStack &S,SElemType e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType *)realloc(S.base,(STACK_INIT_SIZE+S.stacksize)*sizeof(SElemType));
if(!S.base) return ERROR;
S.top=S.base+S.stacksize;
S.stacksize=S.stacksize+STACK_INIT_SIZE;}
*S.top=e;
S.top++;
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
if(S.base==S.top) return ERROR;
e=*--S.top;
return OK;
}
void check()
{ // 对于输入的任意一个字符串,检验括号是否配对
SqStack s;
SElemType ch[80],*p,e;
if(InitStack(s)) // 初始化栈成功
{
printf("请输入:\n");
scanf("%s",ch);
p=ch;
while(*p) // 没到串尾
switch(*p)
{
case '(':
case '[':
case '{':
Push(s,*p);p++;
break; // 左括号入栈,且p++
case ')':
case ']':
case '}':
if(!StackEmpty(s)) // 栈不空
{
Pop(s,e); // 弹出栈顶元素
if(*p==')'&&e!='('||*p==']'&&e!='['||*p=='}'&&e!='{')
// 弹出的栈顶元素与*p不配对
{
printf("1.括号不匹配\n");
exit(ERROR);
}
else
{
p++;
break; // 跳出switch语句
}
}
else // 栈空
{
printf("3.括号不匹配\n");
exit(ERROR);
}
default: p++; // 其它字符不处理,指针向后移
}
if(StackEmpty(s)) // 字符串结束时栈空
printf("括号匹配成功\n");
else
printf("2.括号不匹配\n");
}
}
void main()
{
check();
}