/*----------------------------------------------------------------------------------括弧匹配程式程式說明:括弧匹配的檢驗。假設運算式中允許包含兩種括弧:圓括弧和方括弧,其嵌套的順序隨意,即([]())或[([][])]等為正確的格式,[(])或([())或(()]均為不正確的格式。檢驗括弧是否匹配的方法可用“期待的急切程式”這個概念來描述。例如考慮下列括弧序列:[ ( [ ] [ ] ) ] 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 ;}//DestoryStackStatus ClearStack(SqStack *S){S->top = S->base ;return OK ;}//ClearStackStatus 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 ;}//PushStatus Pop(SqStack *S, SElemType *e){if(S->top == S->base )return ERROR ;*e = * --S->top ;return OK ;}//PopStatus GetTop(SqStack S,SElemType *e){if(S.top == S.base )return ERROR ;*e = *(S.top - 1 );return OK ;}//GetTopStatus 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 ;}//cmpStatus 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 ;}//InitStackvoid 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-casewhile(getchar() != '\n')continue ;PrintStack(&S) ;printf("\n\n請輸入括弧,如 '('、'('、'['、']' : ") ;}//whileClearStack(&S) ;DestoryStack(&S) ;}//BranketMatchvoid PrintStack(SqStack *S) {SElemType *temp ;temp = S->base ;printf("現在棧的內容:\n") ;while(temp != S->top){printf("%2c",*temp) ;temp++ ;}}//PrintStack