/*----------------------------------------------------------------------------------
括号匹配程序
程序说明:
括号匹配的检验。
假设表达式中允许包含两种括号:圆括号和方括号,其嵌套的顺序随意,即([]())或[([][])]
等为正确的格式,[(])或([())或(()]均为不正确的格式。检验括号是否匹配的方法可用“期待
的急切程序”这个概念来描述。例如考虑下列括号序列:
[ ( [ ] [ ] ) ]
1 2 3 4 5 6 7 8
当计算机接受了第一个括号后,它期待着与其匹配的第八个括号的出现,然而等来的动量第二个
括号,此时第一个括号“[”只能暂时靠边,而迫切等待与第二个括号相匹配的、第七个括号“)"
的出现,类似地,因等来的是第三个括号“[”,其期待匹配的程序较第二个括号更急迫,则第
二个括号也只能靠边,让位于第三个括号,显然第二个括号的期待急迫性高于第一个括号;在接
受了第四个括号之后,第三个括号的期待得到满足,消解之后,第二个括号的期待匹配就成为当
前最急迫的任务了,。。。。,依次类推。
这一个程序也是数据结构这一本书上的例子,但是它没有提供代码,只有提供思想,所以我也将它
实现了。。不过,这一个程序也比较好玩.
------Seed
--------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------
useSqStack.c
功能:提供测试代码
edited by Seed , 2011
-------------------------------------------------------------------*/
#include<stdio.h>
#include"match.h"
int main(void)
{
printf("欢迎使用括号匹配程序: \n") ;
BranketMatch() ;
printf("Bye") ;
return 0 ;
}
/*-----------------------------------------------------
match.h
功能:提供函数声明、以及类型定义
---------------------------------------------------*/
#ifndef MATCH_H_
#define MATCH_H_
typedef int Status ;
typedef char SElemType ;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define STACK_ININ_SIZE 100
#define STACKINCREMENT 10
#define CMP(ch) (ch != ']' && ch != ')')
#define SUCCESS(ch,e) (('(' == e && ')' == ch) || ('[' == e && ch == ']'))
#define ELSE(ch) (ch != ']' && ch != ')' && ch !='[' && ch !='(' )
typedef struct
{
SElemType *base ;
SElemType *top ;
int stacksize ;
} SqStack ;
Status DestoryStack(SqStack *S) ;
Status ClearStack(SqStack *S) ;
Status Push(SqStack *S,SElemType e) ;
Status Pop(SqStack *S,SElemType *e) ;
Status GetTop(SqStack S,SElemType *e) ;
Status StackEmpty(SqStack S) ;
int cmp(SqStack *S,SElemType ch) ;
Status InitStack(SqStack *S) ;
void BranketMatch() ;
void PrintStack(SqStack *S) ;
#endif
/*-------------------------------------------------------
SqStack.c
功能:提供方法实现
-----------------------------------------------------*/
#include<stdio.h>
#include<string.h>
#include"match.h"
#include<stdlib.h>
Status DestoryStack(SqStack *S)
{
free(S->base ) ;
S->base = S->top = NULL ;
return OK ;
}//DestoryStack
Status ClearStack(SqStack *S)
{
S->top = S->base ;
return OK ;
}//ClearStack
Status Push(SqStack *S,SElemType e)
{
if(S->top -S->base >= S->stacksize )
{
S->base = (SElemType *)realloc(S->base ,(S->stacksize +STACKINCREMENT) * sizeof(SElemType)) ;
if(NULL == S->base )
{
puts("ERROR") ;
exit(OVERFLOW) ;
}
S->top = S->base + S->stacksize ;
S->stacksize += STACKINCREMENT ;
}
*(S->top )++ = e ;
return OK ;
}//Push
Status Pop(SqStack *S, SElemType *e)
{
if(S->top == S->base )
return ERROR ;
*e = * --S->top ;
return OK ;
}//Pop
Status GetTop(SqStack S,SElemType *e)
{
if(S.top == S.base )
return ERROR ;
*e = *(S.top - 1 );
return OK ;
}//GetTop
Status StackEmpty(SqStack S)
{
return (S.base == S.top ) ;
}// StackEmpty
#define CMP(ch) (ch != ']' && ch != ')') ///
#define SUCCESS(ch,e) (('(' == e && ')' == ch) || ('[' == e && ch == ']'))
#define ELSE(ch) (ch != ']' && ch != ')' && ch !='[' && ch !='(' )
int cmp(SqStack *S,SElemType ch)
{
SElemType e ;
if(ELSE(ch))
{
return -1 ;
}
else if(StackEmpty(*S) && CMP(ch))
{
return TRUE ;
}
else
{
GetTop(*S,&e) ;
if(SUCCESS(ch,e))
return FALSE ;
else if(!SUCCESS(ch,e) && (ch == ']' || ch == ')'))
return -1 ;
}
return TRUE ;
}//cmp
Status InitStack(SqStack *S)
{
S->base = (SElemType *) malloc(STACK_ININ_SIZE * sizeof(SElemType) ) ;
if(NULL == S->base)
{
puts("ERROR") ;
exit(OVERFLOW) ;
}
S->top = S->base ;
S->stacksize = STACK_ININ_SIZE ;
return OK ;
}//InitStack
void BranketMatch()
{
SqStack S ;
SElemType ch ;
InitStack(&S) ;
printf("请输入括号,如 '('、'('、'['、']' : ") ;
while((ch = getchar()) != 'q')
{
switch(cmp(&S,ch))
{
case 0 :
Pop(&S,&ch) ;
puts("匹配成功") ;
break ;
case 1 :
Push(&S,ch) ;
puts("匹配失败,改变急切度") ;
break ;
default :
puts("不合法的输入.") ;
break ;
}//switch-case
while(getchar() != '\n')
continue ;
PrintStack(&S) ;
printf("\n\n请输入括号,如 '('、'('、'['、']' : ") ;
}//while
ClearStack(&S) ;
DestoryStack(&S) ;
}//BranketMatch
void PrintStack(SqStack *S)
{
SElemType *temp ;
temp = S->base ;
printf("现在栈的内容:\n") ;
while(temp != S->top)
{
printf("%2c",*temp) ;
temp++ ;
}
}//PrintStack
数据结构应用-----------括号匹配的检验
最新推荐文章于 2025-01-18 11:57:38 发布