Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Evaluating a string expression

Status
Not open for further replies.

DanSoft

Programmer
Nov 7, 2001
19
US
I need to evaluate a expression stored as a string and store that result to a variable. By the way, the string would contain other variables.
For example, i might need to evaluate
Code:
 "sin(x)+cos(y)"
given that I have values for x and y in those variables.
 
You have asked for the solution to a rather complex problem with a very short question. You appear to be attempting to create a pseudo-interpreter.

One way to do it would be to use Qbasic to evalute the expressions: 1) Convert the expressions into BASIC syntax. 2) Write them to a BAS file. 3) Shell to Qbasic, telling it to open and run the newly created BAS file, then exit (you would have to find a way to save the results of the expressions to the environment or another file that can be read by the calling program).

This would be very awkward and hard to implement.

Another way to do it would be to get into the trenches and work out a solution, one element at a time (essentially, you will end up doing work that has already been performed by the Microsoft programmers).

Evaluate each expression as a string using INSTR, LEFT$ MID$ and the others:[tt]
Expression$ = "sin(x)+cos(y)"
If Expression$ contains "sin(" then
Get the value of the variable following "sin("
Place SIN(x) in a temporary variable
If Expression$ contains "cos(" then
Get the value of the variable following "cos(
Place COS(y) in a temporary variable
If "sin(?)" and "cos(?)" are separated by "+" then
Add the two temporary variables
Place the result in another variable
Do tons of other stuff
EndIf
EndIf
EndIf
[/tt]
I hope you can appreciate how complicated this could become in a very short amount of time. The pseudo-code example I just noted would be a very poor way to interpret strings as expressions but I hope it helps you see the nature of your question.

You want us to tell you how to build a space station without asking us whether or not it will work or if it is even a good idea. It probably won't fly and it will serve as a source of frustration... but it will be a great learning experience and you will probably have a bit of fun in the process.

Start with thread314-22099. This doesn't even address the problems of evaluating the symbolic representations of functions and variables from a string but it does talk about ways to perform math on numbers (some rather large numbers) that are stored as strings. It might not be a bad way for you to "get your feet wet."

Good luck with your interesting project.
VCA.gif
 
This is probably off-topic for the Qbasic forum but I would like to point out that other versions of BASIC make this task a bit easier. If you use Visual Basic you might try downloading the ScriptControl from microsoft.com. This will allow most of what you want to accomplish.

Even without the ScriptControl, if Scripting Host is installed on the system (I believe it comes with Internet Explorer), you might be able to acheive partial funtionality. You could, for example, have the user type a line of VBscript in a text box and click a button which would save it to a script and then run it with the ShellExecute API. Returning a value to your application could be difficult but not impossible.

Some development tools provide an Eval function (not sure... but I think Foxpro is among them) that does exactly what you desire.

Look around. I'm not trying to discourage you from doing this with Qbasic... you just might find an easier way to go.
VCA.gif
 
OK. I finally figured it out, but I've only implemented it in C++. Also, that project is dead now!

--Dan Hirsch
CEO of DanSoft
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top