0

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:

  1. Tokenized the expression
  2. Converted the infix notation to Postfix and evaluated.
  3. if the expression is 10+20+(Thickness/(10+20+Height)) then postfix will be like 10 20 + Thickness 10 20 + Height + / + and I'm able to evaluate to value 30 for first two operands, but, if my expression is like 10+(Thickness/(10+Height+20))+20 then post fix will be 10 Thickness 10 Height + 20 + / + 20 + since first two operands involve variable Thickness 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.

Nasreddine
  • 36,610
  • 17
  • 75
  • 94
chanakyudu
  • 21
  • 1
  • Hey, do check this question: http://stackoverflow.com/questions/333737/evaluating-string-342-yield-int-18 – Stavm Apr 18 '16 at 10:34
  • Hi Thank you for quick reply, I did check that post, But it requires values for variables before evaluating the expression.I need something similar to the question posted in http://stackoverflow.com/questions/17018463/how-to-simplify-a-c-style-arithmetical-expression-containing-variables-during-co – chanakyudu Apr 18 '16 at 11:58

0 Answers0