PTA stack (bracket matching)

Keywords: C++ C

#include<bits/stdc++.h>
using namespace std;


#define STACK_INIT_SIZE 10000
#define STACKINCREMENT 10
#define TRUE        1
#define FALSE       0
#define OK          1
#define ERROR       0
#define INFEASIBLE -1
#define OVERFLOW   -2
using namespace std;
typedef char SElemType, Status;
typedef struct
{
    SElemType *base;
    SElemType *top;
    int stacksize;
}SqStack;
Status InitStack(SqStack &S)
{
    S.base = (SElemType *)malloc(sizeof(SElemType)*STACK_INIT_SIZE);
    if (!S.base)
        exit(OVERFLOW);
    S.top = S.base;
    S.stacksize = STACK_INIT_SIZE;
    return OK;
}
Status Push(SqStack &S, SElemType e)
{
    if (S.top - S.base >= S.stacksize)
    {
        S.base = (SElemType*)malloc(sizeof(SElemType)*(S.stacksize + STACKINCREMENT));
        if (!S.base)
            exit(OVERFLOW);
        S.top = S.base + S.stacksize;
        S.stacksize += STACKINCREMENT;
    }
    *S.top++ = e;
    return OK;

}
Status Pop(SqStack &S)
{
    if (S.top == S.base)
        return ERROR;
    S.top--;
    return OK;
}
Status GetTop(SqStack &S, SElemType &e)
{
    if (S.base == S.top)
        return ERROR;
    e = *(S.top - 1);
    return OK;
}
Status Empty(SqStack S)
{
    if (S.top == S.base)
        return OK;
    else
        return ERROR;
}


const int maxn = 1000 + 5;
char s[maxn];
bool Find(SqStack &S, char ch)
{
    char tmp[maxn]; //Initialize to“\n";
    memset(tmp, '\n', sizeof(tmp)); //Memset It is used to set a memory space to a certain character. It is generally used to initialize the defined string to' 'or'/0';
    int num = 0; 
    while (!Empty)
    {
        SElemType e2;
        GetTop(S, e2);
        if (e2 == ch)
        {
            Pop(S);
            for (int i = num - 1; i >= 0; i--)
                Push(S, tmp[i]);
            return true;
        }
        else
        {
            tmp[num++] = e2;
        }
        Pop(S);
    }
    for (int i = num - 1; i >= 0; i--)
        Push(S, tmp[i]);
    return false;
}
void judge(char ch)
{
    if (ch == '(')
        printf("(-?\n");
    else if (ch == '[')
        printf("[-?\n");
    else if (ch == '{')
        printf("{-?\n");
    else if (ch == '<')
        printf("/*-?\n");
}

void fun(SqStack &Sta, char ch)
{
    int flag = 1;;
    if (!Empty(S)
    {
        SElemType e;
        GetTop(Sta, e);
        if (e == '(')
            Pop(Sta);
        else if (flag)
        {
            printf("NO\n");
            flag = 0;
            judge(e);
        }
    }
}


int main()
{
    SqStack Sta;
    InitStack(Sta);
    int flag = 1;
    while (gets(s))
    {
        if (s[0] == '.') break;
        int len = strlen(s);
        for (int i = 0; i < len; i++)
        {
            if (s[i] == '(' || s[i] == '[' || s[i] == '{')
                Push(Sta, s[i]);
            else if (s[i] == '/'&&s[i + 1] == '*'&&i + 1 < len)
            {
                ++i;
                Push(Sta, '<');
            }

            else if (s[i] == ')')
            {

                if (!Empty(Sta)
                {
                    SElemType e;
                    GetTop(Sta, e);
                    if (e == '(')
                        Pop(Sta);
                    else if (flag)
                    {
                        printf("NO\n");
                        flag = 0;
                        judge(e);
                    }
                }
                else if (flag)
                {
                    flag = 0;
                    printf("NO\n");
                    printf("?-)\n");
                }


            }
            else if (s[i] == ']')
            {

                if (!Empty(Sta)
                {
                    SElemType e;
                    GetTop(Sta, e);
                    if (e == '[')
                        Pop(Sta);
                    else if (flag)
                    {
                        printf("NO\n");
                        flag = 0;
                        judge(e);
                    }
                }
                else if (flag)
                {
                    flag = 0;
                    printf("NO\n");
                    printf("?-]\n");
                }


            }
            else if (s[i] == '}')
            {

                if (!Empty(Sta)
                {
                    SElemType e;
                    GetTop(Sta, e);
                    if (e == '{')
                        Pop(Sta);
                    else if (flag)
                    {
                        printf("NO\n");
                        flag = 0;
                        judge(e);
                    }
                }
                else if (flag)
                {
                    flag = 0;
                    printf("NO\n");
                    printf("?-}\n");
                }


            }
            else if (s[i] == '*'&&s[i + 1] == '/'&&i + 1 < len)
            {
                ++i;
                if (!Empty(Sta)
                {
                    SElemType e;
                    GetTop(Sta, e);
                    if (e == '<')
                        Pop(Sta);
                    else if (flag)
                    {
                        printf("NO\n");
                        flag = 0;
                        judge(e);
                    }
                }
                else if (flag)
                {
                    flag = 0;
                    printf("NO\n");
                    printf("?-*/\n");
                }

            }
        }
    }
    if (flag)
    {
        if (!Empty(Sta)
            printf("YES\n");
        else
        {
            SElemType e;
            GetTop(Sta, e);
            printf("NO\n");
            judge(e);
        }
    }
}
7-2 symbol pairing (20 points)

Please write a program to check whether the following symbols are paired in the C language source program: / * and * /, (and), [and], {and}.

Input format:

Input as a C language source program. When reading a line with only one period. And a carriage return, it marks the end of input. No more than 100 symbols need to be matched in the program.

Output format:

First, if all symbols are paired correctly, then YES is output in the first line, otherwise NO is output. Then in the second line, the first unmatched symbol is indicated: if the left symbol is missing, then the? - right symbol is output; if the right symbol is missing, then the left symbol -?.

Enter example 1:

void test()
{
    int i, A[10];
    for (i=0; i<10; i++) /*/
        A[i] = i;
}
.

Output example 1:

NO
/*-?

Enter example 2:

void test()
{
    int i, A[10];
    for (i=0; i<10; i++) /**/
        A[i] = i;
}]
.

Output example 2:

NO
?-]

Enter example 3:

void test()
{
    int i
    double A[10];
    for (i=0; i<10; i++) /**/
        A[i] = 0.1*i;
}
.

Output example 3:

YES

Posted by yumico23 on Sun, 08 Dec 2019 11:28:36 -0800