C 언어 순서 스 택 에서 사용 하 는 괄호 일치

//        :     
//  :nuaazdh
//  :2011 12 5 
#include <stdio.h>
#include <stdlib.h>

#define OK      1
#define ERROR   0
#define TRUE    1
#define FALSE   0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define BUFFERSIZE 256

typedef int Status; //      
typedef char SElemType;  //     
typedef struct{//     
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;

Status InitStack(SqStack *S);
    //      S
Status DestroyStack(SqStack *S);
    //   S,S    
Status ClearStack(SqStack *S);
    //  S    
Status StackEmpty(SqStack S);
    //  S   ,   TRUE,    FALSE
int StackLength(SqStack S);
    //  S     ,     
Status GetTop(SqStack S,SElemType *e);
    //     ,  e  S     ,   OK;    FALSE
Status Push(SqStack *S,SElemType e);
    //    e       
Status Pop(SqStack *S,SElemType *e);
    //  S   ,   S     , e    ,   OK,    ERROR
Status StackTraverse(const SqStack *S);
    //                 
Status BracketMatch(SqStack *S,const char *string);
    //     string      ,  TRUE;    FALSE

int main()
{
    char *string;
    SqStack stack;
    string=(char*)malloc(sizeof(char)*BUFFERSIZE);
    if(!string){
        printf("      .
"); exit(0); } while(1){ printf(" ( \"!\" ):"); gets(string); if(string[0]=='!')// break; if(TRUE==BracketMatch(&stack,string)){ printf("
.

"); }else{ printf("
.

"); } }//while return 0; } Status InitStack(SqStack *S){ // S S->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!S->base)// { printf(" .
"); exit(0); } S->top=S->base; S->stacksize=STACK_INIT_SIZE; return OK; } Status DestroyStack(SqStack *S){ // S,S if(!S)//S { printf(" , .
"); exit(0); } free(S); return OK; } Status ClearStack(SqStack *S){ // S if(!S)//S return FALSE; S->top=S->base;// return OK; } Status StackEmpty(SqStack S){ // S , TRUE, FALSE if(S.top==S.base) return TRUE; else return FALSE; } int StackLength(SqStack S){ // S , return S.stacksize; } Status GetTop(SqStack S,SElemType *e){ // , e S , OK; FALSE if(S.top==S.base){ //printf(" .
"); return FALSE; }else{ *e=*(S.top-1); //printf("%c",*e); return OK; } } Status Push(SqStack *S,SElemType e){ // e if(S->top-S->base>=S->stacksize){// , S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType)); if(!S->base) { printf(" .
"); exit(0); } S->top=S->base+S->stacksize;// S->stacksize+=STACKINCREMENT; } *S->top++=e; return OK; } Status Pop(SqStack *S,SElemType *e){ // S , S , e , OK, ERROR if(S->top==S->base){// printf(" .
"); return ERROR; } *e=*(--S->top); return OK; } Status StackTraverse(const SqStack *S){ // SElemType *p=S->base; if(S->base==S->top) { printf(" .
"); return FALSE; } //printf(" :"); while(p!=S->top) { printf("%c",*p++); } printf("
"); return OK; } Status BracketMatch(SqStack *S,const char *string){ // string , TRUE; FALSE const char *p=string; SElemType e; InitStack(S); while(*p!='\0'){// switch(*p){// p case '(':// , case '[': case '{': Push(S,*p); //printf("Push %c",*p); break; case ')': if(FALSE==GetTop(*S,&e)) return FALSE; if(e=='('){ if(ERROR==Pop(S,&e)) return FALSE; //printf("Push %c",*p); }else return FALSE; break; case ']': if(FALSE==GetTop(*S,&e)) return FALSE; if(e=='['){ if(ERROR==Pop(S,&e)) return FALSE; //printf("Push %c",*p); }else return FALSE; break; case '}': if(FALSE==GetTop(*S,&e)) return FALSE; if(e=='{'){ if(ERROR==Pop(S,&e)) return FALSE; //printf("Push %c",*p); }else return FALSE; break; default: ; }//switch p++; }//while if(!StackEmpty(*S))// , , return FALSE; return TRUE; }

좋은 웹페이지 즐겨찾기