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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User input of variable names and formulas

Status
Not open for further replies.

Wollomi

Programmer
Jan 12, 2000
2
AU
I am writing an application in which I want to enable the user to define variable names and enter formulas into text boxes on a form, which are then used within the code. It is to be used by ecologists in creating and testing simple ecosystem models. For example the user might enter 'Salinity' as the name of variable one, 'Sunlight' as variable two, 'Biomass' as the name of variable 3, and then define a relationship in a fourth text box such as 'Biomass = Biomass + Biomass * Sunlight / Salinity'. I can of course do this within the code, but how do I give the user control of the variable names and formulas? This is the last problem standing in the way of creating this application and and it has me stumped. My eternal gratitude goes to the person with the solution.
 
You could define a 2 dimensional array for the maximum number of user names you will allow. The first dimension of each item will contain the variable name, the second will contain the value (if any). You would then have to parse the computational text box in order to :<br>
1) break up the text into either variable names <br>
or mathematical operators.<br>
2) validate any variable names against the ones stored <br>
in the array.<br>
3) validate the mathematical operators (*,/,-,+,etc).<br>
4) perform the mathematical operations in the correct <br>
order (the most difficult part I think).<br>
Maybe this will provide a start.
 
I'm racking my brain here, but I recall a small advertisement in the back of an older Microsoft Systems Journal for an ActiveX component that acted as an &quot;editor&quot; for computer languages. It had predefined templates for Java, C, BASIC, etc. but you could also define your own custom languages. It had color syntax highlighting, undo/redo, and a fair number of other features.<br>
<br>
I've never used it, but you might want to pick up a copy of MSJ at the newsstand and see if it's still in there. Maybe the company will send you an eval version.<br>
<br>
Chip H.<br>
<br>
<br>

 
The thing you want is called a &quot;Recursive Descent Parser&quot; and, quite frankly, it is not trivial.<br>
<br>
There used to be an example of one that came with VB3 - it was a demo program, a spreadsheet but I haven't seen it in any more recent version.<br>
<br>
If I was in your position I probably wouldn't attempt to write my own - I'd buy one if I could.<br>
<br>
Or &lt;smile&gt; you could cheat.<br>
<br>
If you have Excel on the machine you might consider creating a spreadsheet behind the scenes. This is not difficult, control Excel using OLE commands; examples in VB Books Online.<br>
<br>
I suggest you cheat.<br>
<br>
Mike<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
I seem to remember seeing code for RCDs on Planet Source Code. Try doing a search under &quot;Formula&quot; - That seemed to be the common thread in the titles. If you have no luck, email me. I wrote one as an intellectual exercise many years ago. I could probably dig up the code and send it to you. I can't remember if it handled variables but they are actually quite simple to implement.<br>
<br>
-Ian<br>

 
I agree with Mike &quot;Cheat&quot; Excel is very powerful and has a robust formula build feature.<br>
Plus the end user might know Excel already<br>
Or at least they could go to Excel and look in help.<br>
<br>
So you get a good application and help and examples<br>
what more could you want.<br>

 
Thanks everyone for your suggestions. I followed IanAtCidac's suggestion and found that a fellow by the name of Konstantin Tretyakov had written a routine he calls an Expression Evaluator, which is in fact a very impressive Recursive Descent Parser as Mike Lacey mentioned. It returns the result of a text expression sent to it, which is what I need. I had thought of using Excel as Mike suggested, but this application will need to iterrate through dozens of formula for many thousands of cells many thousands of times. I thought that having to reference excel might be too slow. Even if the formulae are incorporated directly into the code, I anticipate that simulations may take hours take hours or days to complete. Chiphs suggestion sounds attractive, as inserting the formulae into a module code at run time and then running it from there sounds to me to be the fastest solution. However, the closest I can come to his code editor is the
 
hey! &lt;smile&gt; Wollomi - please finish off your post...<br>
<br>
the rdp implementation (Tretyakov) you mention sounds useful<br>
<br>
-ml <p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top