当循环的法官声明中有一个表达式时,我会出现问题

发布于 2025-01-24 13:17:08 字数 738 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文