Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool validate(const string& postfix)
- {
- /*
- A postfix expression is valid if and only if:
- 1) The first two elements are operands(values), and
- 2) The last element is an operator, and
- 3) For every n values there are n-1 operator(s), and
- */
- char first_element = postfix[0];
- char second_element = postfix[1];
- char last_element = postfix[postfix.size() - 1];
- // Priority of 0 means that the element is an operand
- if (priority(first_element) != 0 && priority(second_element != 0))
- return false;
- if (priority(last_element) == 0)
- return false;
- int num_elements = 0;
- int num_operands = 0;
- bool not_exists = false;
- // FIX FOR NEXT TIME: Make sure that you can actually have infinite number of not statements and it will still be able to run
- for (int i = 0; i < postfix.size(); i++)
- {
- char ith_element = postfix[i];
- if (priority(ith_element) == 0 && ith_element != ' ')
- num_elements++;
- else if (priority(ith_element) == 1 || (priority(ith_element) == 2))
- num_operands++;
- else if ()
- }
- return (num_elements == (num_operands + 1));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement