2

I've written an application that converts a string into a mathematical expression for evaluation. This is done by converting the string into postfix and then by constructing an expression tree and solving it.

Now I want to know though, what is the most efficient way to do conversion into postfix?

Sample expression -

(2+(3*4+(4/(3*(4+6))))) or (3+4) or 3+4

shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90
user2831683
  • 967
  • 1
  • 10
  • 21
  • Your string already is a mathematical expression. You haven't told us how you did, it so your question about efficiency is unanswerable. – user207421 Mar 02 '14 at 09:11
  • This is the form in which the user will enter the string , now what I have to do is to convert it into a mathematical expression to be solved. – user2831683 Mar 02 '14 at 09:15
  • 2
    There's no such thing as "the most efficient way". There are only methods that have a chance to be more efficient than other methods for a specific class of tasks in a specific class of environments. You should not care about any of this unless your program is demonstrably too slow. – n. m. could be an AI Mar 02 '14 at 11:38

2 Answers2

3

I would suggest you consult Sedgewick's Algorithms, 4th ed. The code from the book for converting arithmetic expressions into postfix form is available from the website.

hivert
  • 10,579
  • 3
  • 31
  • 56
blazs
  • 4,705
  • 24
  • 38
0

I suppose this question is about the algorithm, BUT - If I would have to something like that I would use something like BOOST::Python to just exec the string as python code and get the result. I like to avoid writing code, If I can..

WeaselFox
  • 7,220
  • 8
  • 44
  • 75