数据结构与算法分析语言平衡符号

来源:互联网 发布:台湾直播软件 编辑:程序博客网 时间:2024/06/07 05:55
/*语言平衡符号*/
#include <stdio.h>
#include <stdlib.h>
#include "three18.h"
//建栈
Stack CreateStack()
{
    Stack s;
    s=(Stack)malloc(sizeof(struct Node));
    if(s==NULL)
    {
        printf("error");
        return NULL;
    }
    s->next=NULL;
    return s;

}
//出栈,弹出栈顶元素
char PopTop(Stack s)
{
    Position p;
    char ch;
    if(s->next==NULL)
    {
        printf("error");
        return NULL;
    }
    else
    {
        p=s->next;
        ch=p->c;
        s->next=s->next->next;
        free(p);

    }
    return ch;

}
//入栈
void Push(Stack s,char c)
{
    Position p;
    p=(Position)malloc(sizeof(struct Node));
    if(p==NULL)
    {
        printf("error");
        return ;
    }
    else
    {
        p->c=c;
        p->next=s->next;
        s->next=p;
    }
}
main()
{
    Stack s=CreateStack();
    char ch;
    while((ch=getchar())!='#')
    {
        if(ch=='{'||ch=='['||ch=='(')        /*开放符号*/  
            Push(s,ch);  
        
        else if(ch=='}'||ch==']'||ch==')')
        {
            if(s->next==NULL)
            {
                printf("error...");
                return ;
            }
            else{
                    switch (ch)
                    {
                        case '}':
                            if(PopTop(s)!='{')
                                printf("不匹配..");
                            break;
                        case ']':
                            if(PopTop(s)!='[')
                            printf("不匹配....");
                            break;
                        case ')':
                            if(PopTop(s)!='(')
                            printf("不匹配.......");
                            break;

                     }
                }
        }
    
    
    
    }
    if(s->next==NULL)
    {
        printf("sucess");
    }
    else
        printf("不匹配\\\\\\\\");//有多余不能配成一对的符号



}