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
- */
- // The NOT character messes things up a lot for whatever reason
- // The NOT character can be the second element
- // And there can be an infinite number of not elements while only one character
- char first_element = postfix[0];
- char second_element = postfix[1];
- char last_element = postfix[postfix.size() - 1];
- if (priority(first_element) != 0)
- return false;
- if (priority(second_element == 2) || priority(second_element) == 3)
- return false;
- int num_letters = 0;
- int non_not_operands = 0;
- for (int i = 0; i < postfix.size(); i++)
- {
- if (priority(postfix[i]) == 0 && postfix[i] != ' ')
- num_letters++;
- else if (priority(postfix[i]) == 2 || priority(postfix[i] == 3))
- non_not_operands++;
- }
- return (num_letters == (non_not_operands + 1));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement