简单判断表达式括号是否匹配

#include <iostream>
using namespace std;
int main()

{
string s;
cin>>s;
int count=0;
for(int i=0;i<s.length();i++)
{
if(s[i]=='(')
count++;
else if(s[i]==')')
if(count==0)
{
cout<<"not match"<<endl;
return 1;
}
count--;
}
cout<<"match"<<endl;
return 0;
}

用于判断表达式的括号是否匹配。同理可以扩展成判断{}的匹配。

posted @ 2012-01-04 18:16  lazycoding  阅读(490)  评论(0编辑  收藏  举报