user-0 提问: 括号配对问题，[]()就是YES [)](]就是NO ，怎么不行啊！求改正

回复:
user: user-1
content: 用栈结构 来做存储和运算

user: user-2
content: 仅供参考#include<stdio.h> #include<malloc.h> #include<string.h> #define STACK_INIT_SIZE 10 #define STACK_GROW_SIZE 5 #define ELEMTYPE char #define OK 1 #define ERROR 0 typedef struct { /*建立一个栈的首结点*/     ELEMTYPE * base;     ELEMTYPE * top;     int stacksize; } SpStack; int InitStack(SpStack *s) { /*建立空的栈并返回首地址*/     s->base=((ELEMTYPE*)malloc(STACK_INIT_SIZE*sizeof(ELEMTYPE)));     if (!s->base) return ERROR;     s->top=s->base;     s->stacksize=STACK_INIT_SIZE;     return OK; } int StackEmpty(SpStack *s) { /*判断栈是否为空*/     if (s->top==s->base) return OK;     else                 return ERROR; } int Push(SpStack *s,ELEMTYPE e) { /*往栈顶插入元素即进栈*/     if (s->top-s->base>=s->stacksize) { /*判断是否栈满*/         s->base=((ELEMTYPE*)realloc(s->base,(s->stacksize+STACK_GROW_SIZE)*sizeof(ELEMTYPE)));         if (!s->base) return ERROR;         s->stacksize+=STACK_GROW_SIZE;         s->top=s->base+s->stacksize;     }     *s->top++=e;     return OK; } int Pop(SpStack *s,ELEMTYPE *e) { /*让栈顶元素依次输出即出栈*/     if (StackEmpty(s)) return ERROR;     *e=*(--s->top);     return OK; } int Comp(ELEMTYPE a,ELEMTYPE b) {     if ((a=='('&&b!=')')       ||(a=='['&&b!=']')       ||(a=='{'&&b!='}')) {         return ERROR;     } else return OK; } int Count(SpStack *s) {     ELEMTYPE e[STACK_INIT_SIZE*2];     ELEMTYPE e1;     int i;      InitStack(s);     fgets(e,STACK_INIT_SIZE*2,stdin);     if ('\n'==e[strlen(e)-1]) e[strlen(e)-1]=0;     printf("%s\n",e);     for (i=0;e[i]!='\0';i++) {         switch (e[i]) {         case '(':         case '[':         case '{':             Push(s,e[i]);             break;         case ')':         case ']':         case '}':             if (StackEmpty(s)) {                 printf("%*s↖右括号多余\n",i+1,"");                 return(ERROR);             } else Pop(s,&e1);             if (!Comp(e1,e[i])) {                 printf("%*s↖左右匹配出错\n",i+1,"");                 return(ERROR);             }         }     }     if (!StackEmpty(s)) {         printf("%*s↖左括号多余\n",i,"");         return(ERROR);     } else {         printf("匹配正确\n");         return(OK);     } } void main() {     SpStack s;     Count(&s);     free(s.base); }

user: user-3
content: 用栈 http://blog.csdn.net/tanghuachun/article/details/7470845

user: user-4
content: 来个递归版的，借用1楼的测试数据：  #include <cstdio> #include <cctype>  class Matcher{ private:     class CharStream{     private:         const char* p;     public:         explicit CharStream(const char* s):p(s){}         bool hasNext(){             for(; isspace(*p); ++p) ;             return *p != '\0';         }         char next(){              return *p++ ;         }     };     //helper function inside     static bool isValid(CharStream& in);     static bool isValid(int level, char left, CharStream& in); public:     static bool isLeft(char c){         return c == '(' || c == '[' || c == '{';      }     static bool isMatch(char l, char r){          return l == '(' && r == ')' ||                l == '[' && r == ']' ||                l == '{' && r == '}' ;     }     //interface to outside     static bool checkValid(const char* s); }; bool Matcher::checkValid(const char* s){     CharStream in(s);     return isValid(in); } bool Matcher::isValid(CharStream& in) {     if(!in.hasNext()) return true;          char c = in.next();     return isLeft(c) && isValid(1, c, in);//try match front left } bool Matcher::isValid(int level, char left, CharStream& in) {     if(!in.hasNext()) return false;       //left dismatch          char c = in.next();     if(!isLeft(c) && isMatch(left, c) ||        isLeft(c) && isValid(level+1, c, in) && in.hasNext() && isMatch(left, in.next())){        if(level == 1) return isValid(in); //left part has been matched, test right part        else return true;                  //inside has been matched     }     else return false;                    //left dismatch }  int main() {     char* test[] = {"[\n]()",                              "[)](]",                              "(((\t[[]])))",                              "()( )[]()[   ]",                              "   ()( [()])[] ",                       ")([]",                       "([) ]",                              "(09)",                           "9()",                              "( )[",                              "()",                     " \t\n  "                     };     for(int i = 0; i < sizeof(test)/sizeof(char*); ++i){         printf("case %d: ", i);         if(Matcher::checkValid(test[i])) puts("valid");         else puts("invalid");     }          getchar();     return 0; }

user: user-5
content: #include<stdio.h> #include<string.h>  bool IsMatch(const char* str, unsigned int length) {     if (!str)     {         return false;     }     if (length % 2 != 0)     {         return false;     }      char* left = new char[length];      unsigned int lIndex = 0;     for (unsigned int i = 0; i < length; ++i)     {         if (str[i] == '(' || str[i] == '[')         {             left[lIndex++] = str[i];         }         else if (str[i] == ')')         {             if (lIndex == 0 || left[--lIndex] != '(')             {                 return false;             }         }         else if (str[i] == ']')         {             if (lIndex == 0 || left[--lIndex] != '[')             {                 return false;             }         }     }     return true; }  int  main() {     char* test[] = {         "[]()",         "[)](]",         "((([[]])))",         "()()[]()[]",         "()([()])[]",         ")([]",         "([)]",         "(09)"         "9()",         "()[",         "()"     };     for (unsigned int i = 0; i < sizeof(test) / sizeof(char*); ++i)     {         printf("%u\n", IsMatch(test[i], strlen(test[i])));     }     return 0; }