namespace WindowsFormsApplication1
{
class parenthese
{
public string correct(ref char[] s)
{
Stack h = new Stack();
for (int i=0;i<s.Length;i++)
{
if (s[i] == '(' || s[i] == '{' || s[i] == '[')//遇到(,{,[则将其入栈
{
h.Push(s[i]);
}
else if (s[i] == ')')
{
if (Convert.ToChar(h.Peek()) == '(')
{ h.Pop();
return "括号配对!";
}
else {return "括号不配对!"; }
}
else if (s[i] == '}')
{
if (Convert.ToChar(h.Peek()) == '{')
{
h.Pop();
return "括号配对!";
}
else { return "括号不配对!"; }
}
else if (s[i] == ']')
{
if (Convert.ToChar(h.Peek()) == '[')
{
h.Pop();
return "括号配对!";
}
else { return "括号不配对!"; }
}
else if (h.Count != 0) { return "括号不配对!"; }
}
}
}
}