Bit of a long read, It's my first question here :)
So, I want a function or the logic, whereby the code is able to validate an expression whether it is Infix, Postfix, or Prefix Accurately. So by accurate I mean it should check all conditions whether the expression itself is valid, what the expression is (In, Pre or Post), or tell us if it's invalid.
I googled this question but all I found was check the first three digits of the expression. But this logic doesn't hold true when the rest of the expression is wrong and I want a far more superior validator.
So here's my take and I want to find any mistakes/edge cases in these.
1] Check the first 3 digits for infix and postfix and the last 3 for postfix and they should be in their respective order. (e.g AB* for postfix and A*B for infix)
2] Every valid expression has N operands and N-1 operators (A+B-C has 3 operands and 2 operators) so check that too, so we can't have tailing operators (something invalid like AB+C-+--)
3] Check the full validity using a stack (if (operand) push it, if (operator), pop twice, valid if the stack is empty at the end). For Infix we use two stacks one for operand and one for the operator and then validate.
4] If all three are correct, then return with an appropriate answer. (can make 3 separate functions if need be)
Sorry for the long read. Is my logic perfect? Could there be any edge cases or mistakes in this one? Thanks!