括号匹配

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define Stack_init_size 20
#define Stackincrement 5

struct  Stack
   {
      int *base;
      int *top;
      int stacksize;
   };

   void InitStack(struct Stack *s)
   {
      s->base=(char *)malloc(Stack_init_size*sizeof(char));
      if(!s->base)
      printf("Overflow");
      else
       {
  s->top=s->base;
  s->top++;
  s->stacksize=Stack_init_size;
  printf("Init Ok/n");

       }
  }

 

  void Push(struct Stack *s,char e)
    {
       if((s->top-s->base)>=s->stacksize)
 {
    s->base=(char *)realloc(s->base,(s->stacksize+Stackincrement)*sizeof(char));
    if(!s->base)
    printf("Overflow");

    s->stacksize+=Stackincrement;
    s->top=s->base+s->stacksize;
 }

        *(s->top)++=e;
   return ;
  }

  void clearstack(struct Stack *s)
  {
       if(s->top==s->base)
 return;
       s->top=s->base;
       return ;
}


  void Pop (struct Stack *S)
    {
       int e;
   if(S->top==S->base)
      printf("error");
   else
    {
      e=*S->top;
  /*    printf("%c",e);  */
    
     
    } 
 
   }
  
 
    


   
   

 

   void match(struct Stack *s,char c)
   {
  switch(c)
     {
  case '{':
  case '[':
  case '(': Push(s,c);break;
  case '}': if(((s->top-s->base)!=0)&&(*(--s->top)=='{'))
             {
     Pop(s);
                break;
      }
             else
               {
    printf("error");
                break;
               } 
  case ']': if(((s->top-s->base)!=0)&&(*(--s->top)=='['))
                {
     Pop(s);
                  break;
                }
                  else
               {
    printf("error");
                break;
               } 
        case ')': if(((s->top-s->base)!=0)&&(*(--s->top)=='('))
               {
                Pop(s);
                 break;
               }
                 else
               {
                printf("error");
                break;
               } 
    case '#':  printf("If there is no error ,you are right"); break;
    default: printf("error/n");
      }
   }

  void main()
  {  
     struct Stack s;
     char c;
    clrscr();
    InitStack(&s);
    printf("Enter the compond and end by #/n");
    do
    {
      scanf("%c",&c);
      match(&s,c);
    }
    while(c!='#');

 

  }

 

 

 


 

阅读更多
文章标签: struct c
上一篇括号匹配
下一篇My Proudest Moment
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭