title: The parentheses of the string [(())] match, the problem is relatively simple, also recorded.
Idea, the left parenthesis on the stack, encountered the right parenthesis on the stack an element to match the decision, construct a switch. At the same time, set a monitoring flag, the initial 1, once the mismatch will be placed to 0, that is, later no longer compare.
#include <iostream>using namespacestd;#defineMaxSize 10typedefstructstack{CharA[maxsize]; inttop;} Stack,*Stack;voidPush (Stack &s,CharCh/*into the stack*/{s->a[++s->top]=ch;}CharPop (Stack &s)/*out of the stack*/{ Charx=s->a[s->top--]; returnx;}intMain () {intI=0, flag=1; Charx; Char*string="[(((())))]"; Stack s= (Stack)malloc(sizeof(stack)); S->top=-1; while(string[i]!=' /') { Switch(string[i])/*left parenthesis in stack, right parenthesis out stack*/ { Case '(': Push (S,'('); Break; Case '[': Push (S,'['); Break; Case ')': if((X=pop (s))! ='(')/*match judgment after the stack is out*/Flag=0; Break; Case ']': if((X=pop (s))! ='[') Flag=0; Break; } I++; if(!flag)/*exit the loop as long as there is a mismatch*/ Break; }; if(flag) printf ("Mattch"); Elseprintf ("No Mattch"); return 0; }
bracket matching [Stack]