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,219
  • 8
  • 64
  • 88
user2831683
  • 827
  • 1
  • 8
  • 20
  • 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. 1.8e9-where's-my-share m. 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,396
  • 3
  • 29
  • 55
blazs
  • 4,520
  • 20
  • 37
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,100
  • 7
  • 42
  • 73