Week 5 item 3 - matching parentheses

[item - matching of brackets]
Suppose three kinds of parentheses are allowed in an expression: parentheses, square brackets, and braces. Write an algorithm to determine whether the various left parentheses in the expression match the right parentheses.  

For example, input 2 + (3 + 4) * 2 + {[3]} - 8, output matching is correct; input 2 + (3 + 4 * [2) + {[3]} - 8, output matching is wrong.


Tip:
(1) When the left bracket is encountered, the stack will issue a symbol to see if it is matched. If pairing, continue until you have read all the symbols and the stack is empty. If there is a mismatch in the middle, or____ Then we can draw the conclusion that there is no match.  
(2) You can also design a function whose return value is Boolean, whose parameter is the expression to be matched, and whose parameter is a string.


Both solutions have bugs: when the number of parentheses entered is greater on the left than on the right, for example{ }}}}}}}}}, a "match the correct bug" will appear.


Solution 1: for the header file sqstack.h, see[ Sequential stack algorithm library ], using Chain stack You can copy

  1. <span style="font-family:SimSun;font-size:12px;">#include <stdio.h>  
  2. #include "sqstack.h"  
  3. int main()  
  4. {  
  5.     char c;  
  6.     char st[50];  
  7.     int d=1, i;  
  8.     SqStack *s;  
  9.     InitStack(s);  
  10.     printf("Please enter an expression:");  
  11.     scanf("%s", st);  
  12.     for(i=0; st[i]!='\0'&&d; i++)  
  13.     {  
  14.         switch(st[i])  
  15.         {  
  16.         case'(':  
  17.         case'[':  
  18.         case'{':  
  19.             Push(s, st[i]);  
  20.             break;  
  21.         case')':  
  22.             Pop(s, c);  
  23.             if(c!='(') d=0;  
  24.             break;  
  25.         case']':  
  26.             Pop(s, c);  
  27.             if(c!='[') d=0;  
  28.             break;  
  29.         case'}':  
  30.             Pop(s,c);  
  31.             if(c!='{') d=0;  
  32.             break;  
  33.         }  
  34.     }  
  35.     if(StackEmpty(s)&&d==1)  
  36.         printf("Correct pairing!!\n");  
  37.     else  
  38.         printf("Pairing error!!\n");  
  39.     return 0;  
  40. }</span>  


Solution 2: write a special function

  1. #include <stdio.h>  
  2. #include "sqstack.h"  
  3.   
  4. bool isMatch(char *st)  
  5. {  
  6.     int d=1, i;  
  7.     char c;  
  8.     SqStack *s;  
  9.     InitStack(s);  
  10.     for(i=0; st[i]!='\0'&&d; i++)  
  11.     {  
  12.         switch(st[i])  
  13.         {  
  14.         case'(':  
  15.         case'[':  
  16.         case'{':  
  17.             Push(s, st[i]);  
  18.             break;  
  19.         case')':  
  20.             Pop(s, c);  
  21.             if(c!='(') d=0;  
  22.             break;  
  23.         case']':  
  24.             Pop(s, c);  
  25.             if(c!='[') d=0;  
  26.             break;  
  27.         case'}':  
  28.             Pop(s,c);  
  29.             if(c!='{') d=0;  
  30.             break;  
  31.         }  
  32.     }  
  33.     if(StackEmpty(s)&&d==1)  
  34.         return true;  
  35.     else  
  36.         return false;  
  37. }  
  38.   
  39. int main()  
  40. {  
  41.   
  42.     char st[50];  
  43.     printf("Please enter an expression:");  
  44.     scanf("%s", st);  
  45.     if(isMatch(st))  
  46.         printf("Correct pairing!!\n");  
  47.     else  
  48.         printf("Pairing error!!\n");  
  49.     return 0;  
  50. }  



Posted on Sun, 31 May 2020 10:37:40 -0400 by jovanross