Math formula from string to "code"

zoggolino

Newcomer
Joined
Jun 2, 2006
Messages
4
I'm programming a science tool. for this i have an input string like this: "y= 1.3*exp(-x*3)" from an input textbox. Now I have to convert this string into the form:

dim py as integer
dim px as integer

py = 1.3*math.exp(px*3), becaus i have to calculate the formula.

how can i do this. is there a possibilty to include a code fragment in a string into a vb project?

thanks a lot for any help.
 
I've written VB7 code that could evaluate an arithmatic expression with trigonometric and exponential functions in the past. If you sit down and really think about it, it isn't all that difficult to code. Precedence can be implemented by layered functions. PerformAddition calls PerformMultiply, which calls ParseValue which parses a constant and returns it to PerformMultiply, which continues calling ParseValue and examining operators, applying the multiplication and division operators until it reaches an operator not at its level of precedence, and returns to PerformAddition, which continues to call PerformMultiply and add the evaluated terms until it finds an operator above its level of precedence. Perentheses equals recursion. (That's a little simplified but you get the idea.)

When performance became an issue I wrote a program to extract the constants and store the expression in a tokenized format with the order of operations applied as the expression is tokenized so that what was left was a very simple sort of stack-based bytecode which could be interpreted very quickly.

Of course, if you have a ready-made solution or would prefer to use reflection-emit or codedom any of those solutions would certainly work.
 
I implemented the on-the-fly compiler from code zone. it works properly and was easy to apply.

thanks a lot
 
Back
Top