尝试用函数递归判断括号使用的正确性
``
#include <iostream>
using namespace std;
int check(char str[], char c, int *n);
int main()
{
char str[60];
while(cin.getline(str, 60))
{
int i = 0;
int flag = check(str, '\0', &i);
if(flag == 1)
cout << "yes" << endl;
else
cout << "no" << endl;
}
return 0;
}
int check(char str[], char c, int *n)
{
while(str[*n] != '\0')
{
if(c == '(') //判断是否与 ( 相对应
{
if(str[*n] == c+1)
return 1;
}
else if(str[*n] == c+2) //判断是否与 [ 或 { 相对应
return 1;
if(str[*n] == ')' || str[*n] == ']' || str[*n] == '}')
{
return 0;
}
else if(str[*n] == '(' || str[*n] == '[' || str[*n] == '{')
{
(*n) ++;
int flag = check(str, str[(*n)-1], n);
if(flag == 0)
return 0;
}
(*n)++;
}
if(c == '\0')
return 1;
else
return 0;
}