栈ADT应用:对称符号匹配判断

输入一行符号,以#结束,判断其中的对称符号是否匹配。对称符号包括:
{ } 、 [ ] 、 ( )、 < >
输出分为以下几种情况:
(1)对称符号都匹配,输出 “ right. “
(2) 如果处理到最后出现了失配,则输出两行:
第一行:Matching failure.
第二行:loss of right character $$ … 其中"$$ “… 是按嵌套顺序对应的右匹配符号。
(3)处理到某个符号时失配了,则输出两行或三行:
第一行: The N character '$' is wrong.” ,其中N是出错符号的序号,$是出错的符号;
第二行: loss of left character $.” 其中 $ 是当前符号的左匹配符号。(如果有的话)第三行:loss of right character $$ …” 其中 $$… 是按嵌套顺序对应的右匹配符号。
例如:
Case 1:
输入

(a.b)>#

输出

The 6 character >' is wrong.
loss of left character <.

Case 2:
输入

({()#

输出

Matching failure.
loss of right character }).

Case 3:
输入

as(*x<{(({<>}))}>)#

输出

right.

Case 4:
输入

([)]#

输出

The 3 character ')' is wrong.
loss of left character (.
loss of right character ]).

折腾了好久才全部通过。。。第四种情况的输出不太好确定。。。

#include<cstdio>
#include<cstdlib>
using namespace std;
const int maxsize=1005;
class Stack
{
private:
    char a[maxsize];
    int up;
    int down;
public:
    Stack();
    void Push(char e);
    void Pop();
    int Size();
    char Top();
    bool Empty();
};
Stack::Stack()
{
    up=0;
    down=0;
}
void Stack::Push(char e)
{
    if(up==maxsize)exit(0);
    a[up++]=e;
}
void Stack::Pop()
{
    if(up==down)exit(0);
    up--;
}
int Stack::Size()
{
    return up-down;
}
char Stack::Top()
{
    if(up==down)exit(0);
    return a[up-1];
}
bool Stack::Empty()
{
    return up==down?true:false;
}
int main()
{
    Stack sta;
    char ch;
    int len=0;
    bool f=false;
    while((ch=getchar())!='#')
    {
        len++;
        if(ch=='{'||ch=='['||ch=='('||ch=='<')sta.Push(ch);
        else if(ch=='}'||ch==']'||ch==')'||ch=='>')
        {
            if(ch=='}')
            {
                if(sta.Empty())
                {
                    printf("The %d character '}' is wrong.\nloss of left character {.\n",len);
                    f=true;
                    continue;
                }
                if(sta.Top()=='{')sta.Pop();
                else
                {
                    f=true;
                    printf("The %d character '}' is wrong.\nloss of left character {.\n",len);
                    printf("loss of right character ");
                    while(!sta.Empty())
                    {
                        if(sta.Top()=='[')printf("]");
                        else if(sta.Top()=='{')printf("}");
                        else if(sta.Top()=='<')printf(">");
                        else if(sta.Top()=='(')printf(")");
                        sta.Pop();
                    }
                    printf(".\n");
                    return 0;
                }
            }
            else if(ch==']')
            {
                if(sta.Empty())
                {
                    printf("The %d character ']' is wrong.\nloss of left character [.\n",len);
                    f=true;
                    continue;
                }
                if(sta.Top()=='[')sta.Pop();
                else
                {
                    f=true;
                    printf("The %d character ']' is wrong.\nloss of left character [.\n",len);
                    printf("loss of right character ");
                    while(!sta.Empty())
                    {
                        if(sta.Top()=='(')printf(")");
                        else if(sta.Top()=='[')printf("]");
                        else if(sta.Top()=='<')printf(">");
                        else if(sta.Top()=='{')printf("}");
                        sta.Pop();
                    }
                    printf(".\n");
                    return 0;
                }
            }
            else if(ch==')')
            {
                if(sta.Empty())
                {
                    printf("The %d character ')' is wrong.\nloss of left character (.\n",len);
                    f=true;
                    continue;
                }
                if(sta.Top()=='(')sta.Pop();
                else
                {
                    f=true;
                    printf("The %d character ')' is wrong.\nloss of left character (.\n",len);
                    printf("loss of right character ");
                    while(!sta.Empty())
                    {
                        if(sta.Top()=='[')printf("]");
                        else if(sta.Top()=='(')printf(")");
                        else if(sta.Top()=='<')printf(">");
                        else if(sta.Top()=='{')printf("}");
                        sta.Pop();
                    }
                    printf(".\n");
                    return 0;
                }
            }
            else if(ch=='>')
            {
                if(sta.Empty())
                {
                    printf("The %d character '>' is wrong.\nloss of left character <.\n",len);
                    f=true;
                    continue;
                }
                if(sta.Top()=='<')sta.Pop();
                else
                {
                    f=true;
                    printf("The %d character '>' is wrong.\nloss of left character <.\n",len);
                    printf("loss of right character ");
                    while(!sta.Empty())
                    {
                        if(sta.Top()=='[')printf("]");
                        else if(sta.Top()=='<')printf(">");
                        else if(sta.Top()=='(')printf(")");
                        else if(sta.Top()=='{')printf("}");
                        sta.Pop();
                    }
                    printf(".\n");
                    return 0;
                }
            }
        }
    }
    if(!f)
    {
        if(sta.Empty())printf("right.");
        else
        {
            printf("Matching failure.\nloss of right character ");
            while(!sta.Empty())
            {
                if(sta.Top()=='{')printf("}");
                else if(sta.Top()=='(')printf(")");
                else if(sta.Top()=='[')printf("]");
                else if(sta.Top()=='<')printf(">");
                sta.Pop();
            }
            printf(".");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值