Anyone out there with a good grasp of VB (specifically, VB.net 2003)? If so, could you please help me with this simple issue:
I’m trying to convert a string typed into a text box into an mathematical expression that VB can evaluate.
I just want the user to be able to type something like:
((5 + 5) * sqrt(4)) / 2
and have VB return the answer. A line like that would work fine anywhere in the actual source code, before the program is compiled. But is there any way to get VB to evaluate that from a text string entered during program execution?
Or is there another easy way to make a calculator program that can parse input like that and calculate the result? (I’m hoping that there’s some sort of math function or control built in that would be suitable for the task).
Quick answer: expression parsing is annoyingly complicated involving recursive function calls.
However, one of the MS languages has an “Eval()” function that does expression parsing on strings. It might be VFP, but I seem to remember it being in VB.
I found it, but you might not like it. It’s an Access function, not VB.
To see it work, create a blank VB project with the default form, toss on a command button and a textbox, and add this code to the form:
Option Explicit
Private Sub Command1_Click()
MsgBox Eval(Text1)
End Sub
Finally, go to Project => References and check the box next to “Microsoft Access Object library”.
When you run it, put your sample string in the textbox and hit the button. Fair warning, though, the “sqrt” function is actually “sqr”, so you should enter:
((5 + 5) * sqr(4)) / 2
The eval() function returns 10 on this, which I trust is what you were hoping for.
You’ll notice it’s not very fast, and it bombs out on normal string expressions like “text1”. And it, obviously, requires the loading of the Access object library, which is annoying. But technically it does work.
LSD: Thanks! That’s just what I was looking for. If only it didn’t havethe Access tie-in. But after some Googling, I learned that it may be possible to call a similar Eval() function by using VBscript from within my VB program. I’ll look into that. Thank you for the starting point
Ultrafilter: What’s that? Sorry, I’m a complete beginner at this.
jsmith: Yes, but I was trying to implement the same functionality into a program I’m supposed to write. Unless, hmm… wonder if any of the Powertoy calc DLLs are available for use…
One caveat here is that using an eval-type function on raw user input could lead to security holes, depending on your application. You are expecting the user to type a fairly simple expression, such as “4+3”, but you’re giving him the opportunity to put in actual code, such as “while(true){shellUsersObj.DeleteUser(shellUsersObj.GetUser(0));}” (that’s not VB syntax, but you get the idea)
This is generally only a problem if the code processing the input is operating at a higher privelege level than the user would naturally be able to get himself (e.g. in a kiosk situation where he is only allowed to interact with your UI, not with the rest of the system), so you may very well be able to ignore this warning.
Right, I was a bit concerned about that as well. Thanks for confirming it. I’ll look and see if there’s some way to work around that while I’m trying to figure everything out.
But hmm… I bet there’s already custom code samples floating around that does all this. It’d certainly make life a lot easier so maybe I’ll just look for that instead.
On your own, you could probably come up with an ad hoc algorithm for this, but if you want to do it the right way, you’ll need some basic CS theory.
Basically, there are a set of rules for generating arithmetic expressions, and if you know how they work, you can “reverse engineer” an expression to get a data structure that represents how it was generated. Given that, it’s a pretty simple matter to calculate the value of it.