语法检查-括号匹配
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));
}
}