Week 5 item 3 - matching parentheses

[item - matching of brackets]Suppose three kinds of parentheses are allowed in an expression: parentheses, square bracke...
[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. bool isMatch(char *st)
  4. {
  5. int d=1, i;
  6. char c;
  7. SqStack *s;
  8. InitStack(s);
  9. for(i=0; st[i]!='\0'&&d; i++)
  10. {
  11. switch(st[i])
  12. {
  13. case'(':
  14. case'[':
  15. case'{':
  16. Push(s, st[i]);
  17. break;
  18. case')':
  19. Pop(s, c);
  20. if(c!='(') d=0;
  21. break;
  22. case']':
  23. Pop(s, c);
  24. if(c!='[') d=0;
  25. break;
  26. case'}':
  27. Pop(s,c);
  28. if(c!='{') d=0;
  29. break;
  30. }
  31. }
  32. if(StackEmpty(s)&&d==1)
  33. return true;
  34. else
  35. return false;
  36. }
  37. int main()
  38. {
  39. char st[50];
  40. printf("Please enter an expression:");
  41. scanf("%s", st);
  42. if(isMatch(st))
  43. printf("Correct pairing!!\n");
  44. else
  45. printf("Pairing error!!\n");
  46. return 0;
  47. }



31 May 2020, 10:37 | Views: 3571

Add new comment

For adding a comment, please log in
or create account

0 comments