#include <iostream>
#include <stack>
#include <cstdlib>
using namespace std;
void PrintMatchedPairs(char *expr)
{
stack<int> s;
int data;
int num = strlen(expr);
for(int i=0; i<num; i++)
{
if(expr[i] == '(')
s.push(i);
else if( expr[i] == ')')
{
if(s.empty())
cout<<"Unmatched ')' at "<<i<<endl;
else
{
cout<<"Matched "<<s.top()<<' '<<i<<endl;
s.pop();
}
}
}
//打印未被匹配的'('
while(!s.empty())
{
cout<<"Unmatched '(' at "<<s.top()<<endl;
s.pop();
}
}
int main()
{
PrintMatchedPairs(")(8+9))()(");
system("pause");
return 0;
}
栈学习--简单的括号匹配
最新推荐文章于 2024-08-19 14:14:32 发布