符号配对

请编写程序检查C语言源程序中下列符号是否配对:/**/()[]{}

输入格式:

输入为一个C语言源程序。当读到某一行中只有一个句点.和一个回车的时候,标志着输入结束。程序中需要检查配对的符号不超过100个。

输出格式:

首先,如果所有符号配对正确,则在第一行中输出YES,否则输出NO。然后在第二行中指出第一个不配对的符号:如果缺少左符号,则输出?-右符号;如果缺少右符号,则输出左符号-?

输入样例1:

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

输出样例1:

NO
/*-?

输入样例2:

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

输出样例2:

NO
?-]

输入样例3:

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

输出样例3:

YES
//此题中 形如/* ( */也为错误
#include 
#define STACK_INIT_SIZE 100
#define STACKINCREAMENT 10
#define OVERFLOW -1
#define ERROR 0
#define FALSE 0
#define TRUE 1
#define OK 1
using namespace std;
const int MAXN=1e5+10;
const int inf=1e9;
typedef int Status;
typedef char SElemType;
typedef struct
{
    SElemType *base;
    SElemType *top;
    int stacksize;
} Stack;
Status InitStack(Stack &S)
{
    S.base = (SElemType *)malloc(STACKINCREAMENT * sizeof(SElemType));
    if(!S.base) exit(OVERFLOW);
    S.top = S.base;
    S.stacksize = STACKINCREAMENT;
    return OK;
}
Status DestroyStack(Stack &S)
{
    free(S.base);
    S.base = NULL;
    S.top = NULL;
    S.stacksize = 0;
    return OK;
}
Status StackEmpty(Stack &S)
{
    if(S.top == S.base)
        return TRUE;
    return FALSE;
}
int StackLength(Stack S)
{
    return S.top - S.base;
}
Status GetTop(Stack S, SElemType &e)
{
    if(S.top == S.base)
        return ERROR;
    e = *(S.top - 1);
    return OK;
}
Status Push(Stack &S, SElemType e)
{
    if(S.top - S.base == S.stacksize)
    {
        S.base = (SElemType *)realloc(S.base, (S.stacksize + STACKINCREAMENT) * sizeof(SElemType));
        if(!S.base) exit(OVERFLOW);
        S.top = S.base + S.stacksize;
        S.stacksize += STACKINCREAMENT;
    }
    *S.top++ = e;
    return OK;
}
Status Pop(Stack &S, SElemType &e)
{
    if(S.base == S.top)
        return ERROR;
    e = *--S.top;
}
Status GetBase(Stack &S, SElemType &e)
{
    if(S.base == S.top)
        return ERROR;
    e = *S.base;
}
int main()
{
    char s[MAXN * 10], sign[MAXN],temp, ErroKind = ' ';
    int cnt = 0;
    Stack S;
    InitStack(S);
    while(gets(s) && s[0] != '.')
    {
        int len = strlen(s);
        for(int i = 0; i < len; ++i)
        {
            if(s[i] == '(' || s[i] == ')' || s[i] == '[' || s[i] == ']' || s[i] == '{' || s[i] == '}')
            {
                sign[cnt++] = s[i];
            }
            else if(s[i] == '/' && s[i + 1] == '*')
            {
                sign[cnt++] = 'a';
                ++i;
            }
            else if(s[i] == '*' && s[i + 1] == '/')
            {
                sign[cnt++] = 'b';
                ++i;
            }
        }
    }
    for(int i = 0; i < cnt ; ++i)
    {
        if(sign[i] == '(' || sign[i] == '[' || sign[i] == '{' ||sign[i] == 'a')
            Push(S, sign[i]);
        else if(sign[i] == ')')
        {
            if(GetTop(S, temp) && temp == '(')
                Pop(S, temp);
            else if(!GetTop(S, temp))
            {
                ErroKind = ')';
                break;
            }
            else
            {
                GetTop(S, temp);
                ErroKind = temp;
                break;
            }
        }
        else if(sign[i] == ']')
        {
            if(GetTop(S, temp) && temp == '[')
                Pop(S, temp);
            else if(!GetTop(S, temp))
            {
                ErroKind = ']';
                break;
            }
            else
            {
                GetTop(S, temp);
                ErroKind = temp;
                break;
            }
        }
        else if(sign[i] == '}')
        {
            if(GetTop(S, temp) && temp == '{')
                Pop(S, temp);
            else if(!GetTop(S, temp))
            {
                ErroKind = '}';
                break;
            }
            else
            {
                GetTop(S, temp);
                ErroKind = temp;
                break;
            }
        }
        else if(sign[i] == 'b')
        {
            if(GetTop(S, temp) && temp == 'a')
                Pop(S, temp);
            else if(!GetTop(S, temp))
            {
                ErroKind = 'b';
                break;
            }
            else
            {
                GetTop(S, temp);
                ErroKind = temp;
                break;
            }
        }
    }
    if(ErroKind == ' ')
    {
        if(StackEmpty(S))
         {
             printf("YES\n");
             return 0;
         }
         else
            GetBase(S, ErroKind);
    }
    printf("NO\n");
    if(ErroKind == '(' || ErroKind == '{' || ErroKind == '[')
        printf("%c-?\n", ErroKind);
       else if(ErroKind == ')' || ErroKind == '}' || ErroKind == ']')
        printf("?-%c\n", ErroKind);
    else if(ErroKind == 'a')
        printf("/*-?\n");
    else if(ErroKind == 'b')
        printf("?-*/\n");

}

//将 /* ( */看错正确代码如下
 #include 
#define STACK_INIT_SIZE 100
#define STACKINCREAMENT 10
#define OVERFLOW -1
#define ERROR 0
#define FALSE 0
#define TRUE 1
#define OK 1
using namespace std;
const int MAXN=(1e5+10) * 10;
typedef int Status;
typedef char SElemType;
typedef struct
{
    SElemType *base;
    SElemType *top;
    int stacksize;
} Stack;
Status InitStack(Stack &S)
{
    S.base = (SElemType *)malloc(STACKINCREAMENT * sizeof(SElemType));
    if(!S.base) exit(OVERFLOW);
    S.top = S.base;
    S.stacksize = STACKINCREAMENT;
    return OK;
}
Status DestroyStack(Stack &S)
{
    free(S.base);
    S.base = NULL;
    S.top = NULL;
    S.stacksize = 0;
    return OK;
}
Status StackEmpty(Stack &S)
{
    if(S.top == S.base)
        return TRUE;
    return FALSE;
}
int StackLength(Stack S)
{
    return S.top - S.base;
}
Status GetTop(Stack S, SElemType &e)
{
    if(S.top == S.base)
        return ERROR;
    e = *(S.top - 1);
    return OK;
}
Status Push(Stack &S, SElemType e)
{
    if(S.top - S.base == S.stacksize)
    {
        S.base = (SElemType *)realloc(S.base, (S.stacksize + STACKINCREAMENT) * sizeof(SElemType));
        if(!S.base) exit(OVERFLOW);
        S.top = S.base + S.stacksize;
        S.stacksize += STACKINCREAMENT;
    }
    *S.top++ = e;
    return OK;
}
Status Pop(Stack &S, SElemType &e)
{
    if(S.base == S.top)
        return ERROR;
    e = *--S.top;
}
Status GetBase(Stack &S, SElemType &e)
{
    if(S.base == S.top)
        return ERROR;
    e = *S.base;
}
int main()
{
    char a[MAXN], temp;
    char ErroKind = ' ';
    Stack S;
    InitStack(S);
    while(scanf("%s", a)!= EOF && a[0] != '.' && a[1] != '\n')
    {
        int length = strlen(a);
        if(ErroKind == ' ')
        {
            for(int i = 0; i < length; ++i)
            {
                if(GetTop(S, temp) && temp == '/')
                {
                    if(a[i] == '*' && a[i + 1] == '/' && a[i - 1] != '/')
                            Pop(S, temp);
                }
                else
                {
                    if(a[i] == '(')
                        Push(S, a[i]);
                    else if(a[i] == ')')
                    {
                        if(GetTop(S, temp) && temp == '(')
                            Pop(S, temp);
                        else if(!GetTop(S, temp))
                        {
                            ErroKind = ')';
                            break;
                        }
                        else
                        {
                            GetTop(S, temp);
                            ErroKind = temp;
                            break;
                        }
                    }
                    else if(a[i] == '[')
                        Push(S, a[i]);
                    else if(a[i] == ']')
                    {
                         if(GetTop(S, temp) && temp == '[')
                            Pop(S, temp);
                        else if(!GetTop(S, temp))
                        {
                            ErroKind = ']';
                            break;
                        }
                        else
                        {
                            GetTop(S, temp);
                            ErroKind = temp;
                            break;
                        }
                    }
                    else if(a[i] == '{')
                        Push(S, a[i]);
                    else if(a[i] == '}')
                    {
                         if(GetTop(S, temp) && temp == '{')
                            Pop(S, temp);
                        else if(!GetTop(S, temp))
                        {
                            ErroKind = '}';
                            break;
                        }
                        else
                        {
                            GetTop(S, temp);
                            ErroKind = temp;
                            break;
                        }
                    }
                    else if(a[i] == '/' && a[i + 1] == '*')
                        Push(S, '/');
                    else if(a[i] == '*' && a[i + 1] == '/' && a[i - 1] != '*')
                    {
                        ErroKind = '6';
                        break;
                    }
                }
            }
        }
        else
            continue;
    }
    if(ErroKind == ' ')
    {
        if(StackEmpty(S))
            printf("YES");
        else
        {
            GetTop(S, temp);
            printf("NO\n");
            if(temp == '/')
                printf("/*-?");
            else
            {
                GetTop(S, temp);
                printf("%c-?", temp);
            }
        }

    }
    else
    {
        printf("NO\n");
        switch(ErroKind)
        {
        case ']':
        case ')':
        case '}':
            printf("?-%c", ErroKind);
            break;
        case  '/':
            printf("/*-?");
            break;
        case '6':
            printf("?-*/");
            break;
        default:
            printf("%c-?\n", ErroKind);
            break;
        }
    }

}


你可能感兴趣的:(数据结构-PTA)