Original address: http://www.cppblog.com/GUO/archive/2010/09/12/126483.html
/* Bracket matching problem, more classic, using stacks to implement (from the Internet) 1. There are four possibilities for matching parentheses: ① the right and left brackets are incorrect ② the closing parenthesis is more than the opening parenthesis ③ the opening parenthesis is more than the closing parenthesis ④ right and left brackets match the correct 2. Algorithm idea: Sequential scan arithmetic expression (represented as a string), when encountered three types of opening parenthesis to let the parentheses into the stack, when scanning to a certain type of the right parenthesis, compare the current stack top elements match, if the match, the fallback stack continue to judge, if the current stack top element and the current scan of the parentheses do not match, The left and right brackets are paired in an incorrect order; If the string is currently a type of closing parenthesis and the stack is empty, the closing parenthesis is more than the opening parenthesis, and if the stack is non-empty (that is, the stack has some type of opening parenthesis), the opening parenthesis is more than the closing parenthesis, otherwise the parentheses are paired correctly. 3. Program Realization: */#include <iostream>using namespace std; #define MAXSIZE 100struct sstack{Char sign[maxsize]; int top;}; int Initsstack (Sstack &ss) {ss.top=-1; return 1;} int Isemptysstack (Sstack &ss) {if (ss.top==-1) return 1; return 0;} int Pushsstack (Sstack &ss,char c) {ss.sign[++ss.top]=c; return 1;} int Upsstack (Sstack &ss) {if (Isemptysstack (SS)) {cout<< "stack empty" <<endl; return 0; } ss.top--; return 1;} Char topsstack (Sstack &ss) {if (Isemptysstack (SS)) {cout << "stack Empty" <<endl; return 0; } return ss.sign[ss.top];} int main () {string S; cout<< "input expression:";cin>>s; int Length=s.length (); int i; Sstack SS; Initsstack (SS); for (I=0;i<length;++i) {if (s[i]== ' | | | s[i]== ' [' | | s[i]== ' {') Pushsstack (Ss,s[i]); else if (s[i]== ') ' &&! Isemptysstack (ss) &&topsstack (ss) = = ' (') upsstack (ss); else if (s[i]== ') ' &&! Isemptysstack (ss) &&topsstack (ss)! = ' (') cout<< "bracket matching order Incorrect" <<endl; else if (s[i]== '] ' &&! Isemptysstack (ss) &&topsstack (ss) = = ' [') Upsstack (ss); else if (s[i]== '] ' &&! Isemptysstack (ss) &&topsstack (ss)! = ' [') cout<< "bracket match order Incorrect" <<endl; else if (s[i]== '} ' &&! Isemptysstack (ss) &&topsstack (ss) = = ' {') upsstack (ss); else if (s[i]== '} ' &&! Isemptysstack (ss) &&topsstack (ss)! = ' {') cout<< "bracket match order Incorrect" <<endl;else if ((s[i]== ') ' | | s[i]== '] | | s[i]== '} ') &&isemptysstack (SS)) cout<< "closing parenthesis more than opening parenthesis" <<endl; } if (! Isemptysstack (SS)) cout<< "opening parenthesis more than closing parenthesis" <<endl; else if (i= (length-1) &&isemptysstack (SS)) cout<< "parentheses match correctly" <<endl; System ("PAUSE"); return 0;}
Bracket matching problem (c + +, stack)