Advertisement
Cinestra

Incorrect Validating a postfix boolean expression

Mar 28th, 2023
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. bool validate(const string& postfix)
  2. {
  3. /*
  4. A postfix expression is valid if and only if:
  5. 1) The first two elements are operands(values), and
  6. 2) The last element is an operator, and
  7. 3) For every n values there are n-1 operator(s), and
  8. */
  9.  
  10. char first_element = postfix[0];
  11. char second_element = postfix[1];
  12. char last_element = postfix[postfix.size() - 1];
  13.  
  14. // Priority of 0 means that the element is an operand
  15. if (priority(first_element) != 0 && priority(second_element != 0))
  16. return false;
  17.  
  18. if (priority(last_element) == 0)
  19. return false;
  20.  
  21. int num_elements = 0;
  22. int num_operands = 0;
  23. bool not_exists = false;
  24.  
  25. // FIX FOR NEXT TIME: Make sure that you can actually have infinite number of not statements and it will still be able to run
  26.  
  27. for (int i = 0; i < postfix.size(); i++)
  28. {
  29. char ith_element = postfix[i];
  30.  
  31. if (priority(ith_element) == 0 && ith_element != ' ')
  32. num_elements++;
  33.  
  34. else if (priority(ith_element) == 1 || (priority(ith_element) == 2))
  35. num_operands++;
  36.  
  37. else if ()
  38. }
  39.  
  40. return (num_elements == (num_operands + 1));
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement