Ideas:
Set a stack in the algorithm, each read into an empty number
One: If the closing parenthesis: '} ') ' ' (Two cases):
1: To the top of the stack of the most urgent expectations to be dissolved, the stack top elements need to stack;
2: illegal situation, that is, the top of the stack of the most urgent expectations do not match, it needs to be (parentheses) stacked;
Two: if the left parenthesis: ' (' { ') ' ['
As a new and more pressing stack of anticipation;
the code of the sequential stack is no longer repeating : Click the Open link
Brackets match # include "Stack.h" int main () {stack st;initstack (&st); int flag = 1;while (flag) {/* A string of parentheses to test */char str[100]; cout<< "Please enter a string of parentheses:" <<endl;scanf ("%s", str), char *p = str;//point to the string array char e;while (*p! = '} ') {if (*p = = ' [' | | *p = ') {' | | *p = = ' (') {Push (&st,*p);p + +;} Else{if (*p = = '] ') {if (st.base[st.top-1] = = ' [') Pop (&st,&e); Elsepush (&st,*p);} if (*p = = ') ') {if (st.base[st.top-1] = = ' (') Pop (&st,&e); Elsepush (&st,*p);} if (*p = = '} ') {if (st.base[st.top-1] = = ' {') Pop (&st,&e); Elsepush (&st,*p);} p++;}} if (St.top = = 0) cout<< "bracket matching" <<endl;elsecout<< "bracket mismatch" <<endl;cout<< "Continue:1 Break:0 "<<endl;cin>>flag;} Destory (&st); return 0;}
Improved post code:
Parentheses match # include "Stack.h" int main () {stack st;initstack (&st); int flag = 1;while (flag) {char str[100];cout<< " Please enter a string of parentheses: "<<endl;scanf ("%s ", str); Why is it wrong to use cin???? Char *p = Str;char E;while (*p! = ')} {if (*p = = ' [' | | *p = = ' {' | | *p = ' (') {Push (&st,*p);p + +;} Else{if (*p = = '] ' && st.base[st.top-1] = = ' [') | | (*p = = '} ' && st.base[st.top-1] = = ' {') | | (*p = = ') ' && st.base[st.top-1] = = ' (') ) Pop (&st,&e); Elsepush (&st,*p);p + +;}} if (St.top = = 0) cout<< "bracket matching" <<endl;elsecout<< "bracket mismatch" <<endl;cout<< "Continue:1 Break:0 "<<endl;cin>>flag;} Destory (&st); return 0;}
Application of the stack bracket matching