user-0 提问: 一个字符串处理的算法题

回复:
user: user-1
content: 引用 1 楼 user-3 的回复:遇到左括号 入栈  ，遇到右括号 去匹配栈顶，匹配则出栈，不匹配直接返回false。最后检查栈不空 返回false，否则true。 算法思想就是这样，很简单，借用STL中的stack栈  #include <iostream> #include <stack> using namespace std; bool is_balance(const char *str) { 	//空串，也认为是平衡的 	if (!str) 		return true; 	//创建括号栈 	stack<char> sta; 	const char *p = str; 	while (*p) 	{ 		//遇到这四个括号即入栈 		if (*p == '(' || *p == ')' || *p == '[' || *p == ']') 		{ 			if (!sta.empty()) 			{ 				if ((*p == ')' && sta.top() == '(') || (*p == ']' && sta.top() == '[')) 					sta.pop();    //括号匹配则出栈 				else 				{ 					sta.push(*p); 				} 			} 			else   //空栈，则直接入栈 				sta.push(*p); 		} 		p++; 	} 	if (sta.empty()) 		return true; 	else 		return false; } int main(void) { 	char *strs[] = { "(10 20 [30 40] 50) 60 (70 80)", "(10 20 [30 40 50) 60", "(10 20 [30) 40] 50 60 (70 80)" }; 	for (int i = 0; i < sizeof(strs) / sizeof(*strs); i++) 	{ 		if (is_balance(strs[i])) 			cout << strs[i] << ends << "true" << endl; 		else 			cout << strs[i] << ends << "false" << endl; 	} 	cin.get(); 	return 0; }  经运行，通过测试

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: 遇到左括号 入栈  ，遇到右括号 去匹配栈顶，匹配则出栈，不匹配直接返回false。最后检查栈不空 返回false，否则true。