hrbust 1170 语法检查

语法检查-括号匹配
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 571(151 users) Total Accepted: 178(135 users) Rating: Special Judge: No
Description
欢迎大家加入ACM!
要深入的学习ACM的相关知识,首先就必须学会一门编程语言,推荐C/C++。
不过对于初学者,因为没编过多少代码,经常出现变异错误,其实就是语法错误。
现在要你检查一段代码的语法是否否正确。
为了简化问题,我们只检查 (、)、{、} 括号是否匹配了,并且在输入的代码中不包含字符的’(‘,’)’,’{‘,’}’。
其他语法错误不检查,如 “#include

include

#include<stdio.h>
#include<stack>
#include<string.h>
using namespace std;
char a[1003];
int main()
{
    stack<char>s1;
    stack<char>s2;
    while(gets(a))
    {
        //printf("  \n");
        if(strcmp(a,"Ctrl+Z")==0)
        {
            if(s1.empty()&&s2.empty())
            {
                printf("Right\n");
            }
            else
            {
                printf("Wrong\n");
                while(!s1.empty())
                {
                    s1.pop();
                }
                while(!s2.empty())
                {
                    s2.pop();
                }
            }
        }
        int l=strlen(a);
        for(int i=0; i<l; i++)
        {
            if(a[i]=='(')
                s1.push(a[i]);
            if(a[i]=='{')
                s2.push(a[i]);
            if(a[i]=='}')
            {
                if(!s2.empty())
                {
                    char aaa=s2.top();
                    if(aaa=='{')
                        s2.pop();
                        else s2.push(a[i]);
                }
                  else s2.push(a[i]);
            }
            if(a[i]==')')
            {
                if(!s1.empty())
                {
                    char aaa=s1.top();
                    if(aaa=='(')
                        s1.pop();
                        else s1.push(a[i]);
                }
                  else s1.push(a[i]);
            }
        }
        memset(a,0,sizeof(a));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值