括号匹配程序

来源:互联网 发布:东方有线网络客服电话 编辑:程序博客网 时间:2024/04/30 02:47

//均已调试通过,编译器为DEV C++

#include <stdio.h>
#define  Stack_Size 100
typedef struct Sqstack{
        char *base;
        char *top;
        int stacksize;
        }Sqstack;
 //***********创建堆栈*************************      
     void CreatStack(Sqstack *s)
          {
               int stacksize;  //别忘了
               s->base=(char *)malloc(Stack_Size*sizeof(char));
               if(!s->base)
                 exit(1);
               s->top=s->base;
               stacksize=Stack_Size;
          }
 //*****************进栈******************        
     void Push(Sqstack *s,char ch)
         {
               int stacksize;
               if(s->top-s->base>=stacksize)
                  exit(1);
               *(s->top++)=ch;
         }
 //******************出栈**********************       
     void Pop(Sqstack *s,char *ch)// //输入参数要以指针方式才能返回数值,否则
            //只是原数的复本,修改它原参数不变

        {
              if(s->base==s->top)
                 printf("The string is not maching./n");
              *ch=*(--s->top);
        }
 //****************主函数***************************      
  int main()
  {
      again:            //有问题。。。
             printf("Welcome to ues ./n");
            Sqstack S;
             char ch;
            int sign=0;
            CreatStack(&S);
           printf("please input string:/n");
              while((ch=getchar())!='/n')
               {
                   switch(ch){
                      case'(':Push(&S,'(');
                              break;
                      case'[':Push(&S,'[');
                              break;
                      case'{':Push(&S,'{');
                              break;       
                      case')':Pop(&S,&ch);
                             if(ch!='(')
                                sign=1;
                                 break;
                      case']':Pop(&S,&ch);
                              if(ch!='[')
                                sign=1;
                                 break;
                      case'}':Pop(&S,&ch);
                              if(ch!='{')
                                 sign=1;
                                 break;          
                     default:break;
                     }
               if(sign==1)
                       break;
                        
            }
            if(S.base==S.top&&sign==0)
               printf("The string is maching./n");
          else
             
                 printf("The string is not maching./n");
          printf("Do you want again(y or n )?/n");
             ch=getch();
             printf("%c/n",ch);
             if(ch=='y'||ch=='Y')
                     goto again; 
          return 0;
}                          

原创粉丝点击