void getstr(char* p){
printf("input: ");
scanf("%s", p);
return;
}
void main(){
char st[100];
int top = 0;
char str[100];
char k;
int i=0;
int st_error=0;
getstr(str);
while ( (k=str[i]) != 0)
{
if (k == "(" ) st[top++] = k;
if (k == ")" )
{
if (top == 0 )
{
st_error=1;
break;
}
else
top--;
}
i++;
}
if(st_error==0&&top==0) printf("PASS ");
else
if(st_error==1) printf("Less of left! ");
else
if(top>0) printf("Less of right! ");
}
帮我解释下
随便帮我写下别的程序题目 括号匹配
while ( (k=str[i]) != 0)
{
if (k == "(" ) st[top++] = k; //每遇到(字符将 top加1,并将(赋值给str[top]元素 数据挖掘研究院
if (k == ")" ) // 每遇到 ) 将top 减1
{
if (top == 0 ) //如果)更多则设置st_err为1退出循环
{
st_error=1;
break;
}
else
top--;
}
i++;
}
最后判断结果,如果没有错误,且top == 0 则()匹配
否则,如果st_error == 1则右括号更多,否则如果top > 0 则左括号更多

