/******************************
作者:cncoderalex
博客:http://blog.youkuaiyun.com/cncoderalex
*******************************/
#include <iostream>
#include <memory>
#include <string>
#include <stack>
#include <algorithm>
using namespace std;
char strIn[200];
char strOut[200];
void RoundBracketMatch()
{
strOut[0] = 0;
int len = strlen(strIn);
if (len <= 0)
return;
memset(strOut, ' ', sizeof(strOut));
stack<int> sk;
for (int i = 0; i < len; i++)
{
if (strIn[i] == '(')
{
sk.push(i);
}
else if (strIn[i] == ')')
{
if (sk.empty())
{
strOut[i] = ')';
}
else
{
sk.pop();
}
}
}
while (!sk.empty())
{
int index = sk.top();
strOut[index] = '(';
sk.pop();
}
strOut[len] = 0;
}
int main()
{
printf("http://blog.youkuaiyun.com/cncoderalex");
printf("\n");
stack<int> sk;
bool bRet = sk.empty();
while (scanf("%s", strIn) != EOF)
{
RoundBracketMatch();
printf("%s\n", strOut);
}
system("pause");
return 0;
}
括号匹配
最新推荐文章于 2024-09-22 10:16:35 发布