#include <iostream>
#include "linkedStack.h"
#include "exceptions.h"
using namespace std;
//括号匹配
void printMatchedPairs(string exper)
{
linkedStack<int> s;
int length = exper.size(); //字符串的长度
for (int i = 0; i < length; i++)
{
if (exper.at(i) == '(')
s.push(i);
else
if (exper.at(i) == ')')
{
try
{
//从栈中删除匹配的左括号
cout << s.top() << ' ' << i << endl;
s.pop();
}
catch (stackEmpty)
{
//栈为空 没有匹配的括号
cout << "No match for right parenthesis at" << s.top() << endl;
}
}
}
//栈不为空 剩余在栈中的左括号是不匹配的
while (!s.empty())
{
cout << "No match for left parenthesis at " << s.top() << endl;
s.pop();
}
}
栈应用 一(括号匹配)
最新推荐文章于 2022-04-06 15:27:36 发布