当循环的法官声明中有一个表达式时,我会出现问题
bool check_parenttheses(int p, int q)
{
int cnt = 1;
int position = q - 1;
int flag = position - p;
if(tokens[p].type == '(' && tokens[q].type == ')')
{
while(flag)
{
if(tokens[flag].type == '(')
cnt --;
else if(tokens[flag].type == ')')
cnt ++;
if(cnt <= 0)
return false;
flag --;
}
if(cnt == 1)
return true;
else
return false;
}
else
return false;
}
我使用此函数来判断表达式是否在一对()
p(0)内是表达式的开始,而q(17)是终点,
但是while-loop的法官陈述有一些问题。 第一次在while-loop中,标志变为16。 国旗突然变成477218604
bool check_parenttheses(int p, int q)
{
int cnt = 1;
int position = q - 1;
int flag = position - p;
if(tokens[p].type == '(' && tokens[q].type == ')')
{
while(flag)
{
if(tokens[flag].type == '(')
cnt --;
else if(tokens[flag].type == ')')
cnt ++;
if(cnt <= 0)
return false;
flag --;
}
if(cnt == 1)
return true;
else
return false;
}
else
return false;
}
I use this function to judge if a expression is inside a pair of ()
p(0) is the begin of the expression and q(17) is the end
but the while-loop's judge statement has some problem.
The first time in while-loop, flag becomes 16. But when I come to the while(flag)
the second time,
flag suddenly becomes 477218604
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论