Typical examples of problems stack - matching brackets

1  // every right parenthesis will match the left parenthesis unmatched recently encountered! ! ! 
2 #include <the iostream>
 . 3 #include <Stack>
 . 4 #include < String .h>
 . 5  the using  namespace STD;
 . 6  const  int maxLength = 100 ; // Maximum string length 
. 7  void PrintMatchedPairs ( char * expression The) {
 . 8      Stack < int > S;
 . 9      int length = strlen (expression The);
 10      int J;
 . 11      for ( int= I 0 ; I <length; I ++ ) {
 12 is          IF (expression The [I] == ' ( ' ) s.push (I); // position into the stack 
13 is          the else  IF (expression The [I] == ' ) ' ) { // right bracket 
14              IF {(s.empty ()!) // stack is not empty 
15                  J = s.top (); // record the position matching the left bracket 
16                  COUT << " left bracket ie " << + J . 1 << " character with right parenthesis ie " << I + . 1 << "Characters matching success" << endl; 
 . 17                  s.pop (); // successful match, the stack 
18 is              } 
 . 19              the else COUT << " no right parenthesis ie " << I + . 1 << " left parenthesis character matching " << endl; 
 20 is          } 
 21 is      }
 22 is      the while (! {s.empty ())
 23 is          J = s.top ();
 24          s.pop ();
 25          COUT << " no ie left bracket " << J + . 1 < < "Symbol matches right parenthesis "<<endl; 
26     }
27 }
28 int main(){
29     char a[10];
30     for(int i=0;i<10;i++) cin>>a[i];
31     PrintMatchedPairs(a);
32     return 0;
33 }

 

Guess you like

Origin www.cnblogs.com/TYXmax/p/10987761.html