没用栈设置了一个tag值。该数配队成功置tag。
以下是代码。
int main()
{
string str;
cin >> str;
int n = 0;
int i = 0;
int temp;
int sym = 1;
char a[200];
bool tags[200] = { 0 };
while (n < str.length())
{
if(str[n] == ')'|| str[n] == ']')
{
temp = i;
a[++i] = str[n];
while(temp > 0)
{
if ((a[temp] == '(' && a[i] == ')' && tags[temp]==0) || (a[temp] == '[' && a[i] == ']' && tags[temp] == 0))
{
tags[temp] = 1;
tags[i] = 1;
break;
}
else temp--;
}
}
else
{
a[++i] = str[n];
}
n++;
}
for (int j = 1; j <=n; j++)
{
if (a[j]=='('&&tags[j]==0)cout<< a[j]<< ')';
else if (a[j]=='['&&tags[j]==0)cout<<a[j]<<']';
else if (a[j]==')'&&tags[j]==0)cout<< '('<<a[j];
else if (a[j]==']'&&tags[j]==0)cout<<'['<< a[j];
else cout<< a[j];
}
return 0;
}