Stack ADT application: symmetric symbol matching judgment

Enter a line of symbols and end with # to determine whether the symmetry symbols match. Symmetrical symbols include:
{}, [], (), <> The
output is divided into the following situations:
(1) Symmetrical symbols are matched, output "right."
(2) If there is a mismatch at the end of processing, output Two lines: the
first line: Matching failure. The
second line: loss of right character $$… where " $$"… is the right matching symbol corresponding to the nesting order.
(3) When a certain symbol is mismatched, two or three lines will be output: the
first line: The N character '$'is wrong.", where N is the serial number of the error symbol and $ is the error symbol; the
second line : Loss of left character $.” where $ is the left matching symbol of the current symbol. (If any) The third line: loss of right character $$…” where $$… is the right matching symbol corresponding to the nesting order.
For example:
Case 1:
Input

(a.b)>#

Output

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

Case 2:
Input

({
    
    ()#

Output

Matching failure.
loss of right character }).

Case 3:
Input

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

Output

right.

Case 4:
Input

([)]#

Output

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

It took a long time to pass. . . The output of the fourth case is not very sure. . .

#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;
}

Guess you like

Origin blog.csdn.net/upc122/article/details/105374023