括号匹配(不用堆栈)

来源:互联网 发布:淘宝等级怎么升的快 编辑:程序博客网 时间:2024/05/03 06:10

#include <stdio.h>
int IsPiPei(const char * p)
{
 int counter=0;
 while (*p)
 {
  if (*p=='(')
  {
   counter++;
  }
  else if (*p==')')
  {
   if(--counter<0)
   {
    return 0;//不匹配
   }
  }
  p++;
 }
 if (counter)
 {
  return 0;//不匹配
 }
 else
 {
  return 1;
 }
}

void main()
{
 char * str="a()b()e)(";
 if (IsPiPei(str))
 {
  printf("匹配成功!/n");
 }
 else
 {
  printf("匹配不成功!/n");
 }
}

原创粉丝点击