7-2 符号配对 (20 分)

题目:

请编写程序检查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

思路:

1.首先处理一下输入,把所有字符串中的左括号与右括号单独提出来组成一个字符串。这样后边如果有错误,也好调试找bug。

2.对于'/*'和'*/'可以分别处理为'<'和'>',这两个尖括号的ASCII码值相差为2。

3.遍历这个字符串,当碰到左括号的时候就统统压入栈中,碰到右括号的时候,如果当前的栈是空的就输出‘NO’,否则就看看是否匹配,不匹配就把栈中的那个括号弹出并说明栈中的那个符号不匹配。

4.很坑的是这个题没有说明只要有一不匹配就输出,然后结束就行,最后两个样例怎么都出错,搜了下答案才知道这么坑。

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <malloc.h>
#include <bits/stdc++.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
using namespace std;
const int maxn = 1e4+10;
typedef long long ll;
typedef char SElemType;
typedef struct
{
    SElemType* base;
    SElemType* top;
    int stacksize;
} SqStack;

int InitStack(SqStack& S)
{
    S.base = (SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
    if(!S.base)
        exit(-2);
    S.top = S.base;
    S.stacksize = STACK_INIT_SIZE;
    return 1;
}

int GetTop(SqStack S, SElemType& e)
{
    if(S.base == S.top)
        return 0;
    e = *(S.top-1);
    return 1;
}

int Push(SqStack& S,SElemType e)
{
    if(S.top - S.base >= S.stacksize)
    {
        S.base = (SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
        if(!S.base)
            exit(-2);
        S.top = S.base+S.stacksize;
        S.stacksize += STACKINCREMENT;
    }
    *S.top++ = e;
    return 1;
}

int Pop(SqStack& S)
{
    if(S.top == S.base)
        return 0;
    *S.top,S.top--;
    return 1;
}

bool isEmpty(SqStack& S)
{
    if(S.base == S.top)
        return true;
    return false;
}


int main()
{
    SqStack s;
    InitStack(s);
    string tmp,str="";
    bool ok = false,first = true;
    while(getline(cin,tmp))
    {
        if(tmp==".")break;
        for(int i = 0; i<tmp.size(); i++)
        {
            if(tmp[i]=='[' || tmp[i]==']' || tmp[i]=='{' || tmp[i]=='}'|| tmp[i]=='(' || tmp[i]==')')
                str += tmp[i];
            else if(tmp[i]=='/' && tmp[i+1]=='*')
            {
                str += "<";
                i++;
            }
            else if(tmp[i]=='*' && tmp[i+1]=='/')
            {
                str += ">";
                i++;
            }
        }
    }
    //cout<<str<<endl;
    for(int i = 0; i<str.size(); i++)
    {
        char op;
        GetTop(s,op);
        if(str[i]=='(' || str[i]=='[' ||str[i]=='{' || str[i]=='<')
        {
            Push(s,str[i]);
            continue;
        }
        else if(isEmpty(s))
        {
            printf("NO
");
            if(str[i]=='>')
                printf("?-*/
");
            else
                printf("?-%c
",str[i]);
            ok = true;
            break;
        }
        else if(str[i]-op!=1 && str[i]-op!=2)
        {
            printf("NO
");
            if(op=='<')
                printf("/*-?
");
            else
                printf("%c-?
",op);
            Pop(s);
            ok = true;
            i--;
            break;
        }
        else
            Pop(s);
    }
    if(!ok)
    {
        if(isEmpty(s))
            printf("YES
");
        else
        {
            char op;
            GetTop(s,op);
            printf("NO
");
            if(op=='<')printf("/*-?
");
            else
                printf("%c-?
",op);
        }
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/sykline/p/9748825.html