I have developed a mathematical parser using a simple stack algorithm that will handle binary (+, -, *, /)
operators, unary (+,-)
operators, parenthesis and variables.
This parser takes and expression like 10+20+(Thickness/(10+20+Height))
and gives the output as 30+(Thickness/(30+Height))
after evaluation (Note: variables won't be evaluated).
But if i gave the expression like 10+(Thickness/(10+Height+20))+20
, parser gives the output as 10+(Thickness/(10+Height+20))+20
.
I followed below steps for the evaluation:
- Tokenized the expression
- Converted the infix notation to Postfix and evaluated.
- if the expression is
10+20+(Thickness/(10+20+Height))
then postfix will be like10 20 + Thickness 10 20 + Height + / +
and I'm able to evaluate to value 30 for first two operands, but, if my expression is like10+(Thickness/(10+Height+20))+20
then post fix will be10 Thickness 10 Height + 20 + / + 20 +
since first two operands involve variableThickness
I am not able to evaluate it.
So, i would like to reorder the expression 10+(Thickness/(10+Height+20))+20
into 10+20+(Thickness/(10+20+Height))
.
Is there any algorithm to sort the Mathematical expression so that numbers will come first then the variables in an expression or just simply the expression as much as possible.
My main goal is to evaluate an expression which has variable and the values of variables are unknown.