Results 1 to 7 of 7
Thread: assigning formulas (vb6)

20020827, 18:25 #1
 Join Date
 Jul 2001
 Posts
 132
 Thanks
 0
 Thanked 0 Times in 0 Posts
assigning formulas (vb6)
Hello all.....again
I have an input box where the user would enter a formula in terms of ' X ', how do I make it so that vb regonizes this as a formula and allows me to give it values of ' X ' and get the answers from it??
thanks

20020827, 18:57 #2
 Join Date
 Dec 2000
 Location
 Hollywood (sorta), California, USA
 Posts
 2,759
 Thanks
 0
 Thanked 0 Times in 0 Posts
Re: assigning formulas (vb6)
Give us an example of what the user puts in and what you want to come out.
Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
<img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

20020827, 23:28 #3
 Join Date
 Jul 2001
 Posts
 132
 Thanks
 0
 Thanked 0 Times in 0 Posts
Re: assigning formulas (vb6)
Okay, my code will go something like this.
myfunction = inputbox("Enter your function in terms of x ")
the user enters log(x)*sin(x) or whatever they want
now I want to be able to use this function and pass it values for ' x ' and have it spit out a number
so something like this I guess
x=2
answer = myfunction(x)
So how Do I do that??

20020827, 23:53 #4
 Join Date
 Dec 2000
 Location
 Hollywood (sorta), California, USA
 Posts
 2,759
 Thanks
 0
 Thanked 0 Times in 0 Posts
Re: assigning formulas (vb6)
How many possible functions can the user enter?
If you can push your input string into the immediate window of the VBE object then
get the result, you have it made.
Otherwise, all I can think of is a complex parsing and isolating routine that
converts your function names into an expression that VB will execute.
I don't know. Maybe there's a control out there that'll do the math indicated
in an input string.
Sorry, I'm not much help after all. Maybe a brighter bulb will shine on this...Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
<img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

20020828, 03:50 #5
 Join Date
 Dec 2000
 Location
 Sacramento, California, USA
 Posts
 16,775
 Thanks
 0
 Thanked 1 Time in 1 Post
Re: assigning formulas (vb6)
What is it that your function is supposed to be doing? Unless the user builds his formula in something other than an input box, you personally are going to have to write code to parse out the components of the formula entered before you can even figure out which part is the variable. Perhaps if you explain what you're trying to accomplish, you'll get more helpful responses. If it's a scenario generator, you might be better off with Excel. If it's a calculator, there are prebuilt scientific calculators that will take care of it for you.
Charlotte

20020828, 05:37 #6
 Join Date
 Jul 2001
 Posts
 132
 Thanks
 0
 Thanked 0 Times in 0 Posts
Re: assigning formulas (vb6)
Okay, let's try this again.
I am mainly using this exercise as a learning tool for myself, but there is an application for me and some of my friends.
I want to use the function entered by the user and figure out certain values. The math behind it is irrelevant as I have already coded it and made it work. The problem is that I have hardcoded the function into my program and then pass values for ' X ' into that function to solve for the answers that i am looking for. I want to take this one step further, I would like the user to enter their own function always in terms of ' X' and then have the program pass values of 'X' to it to solve for the required answers.
Therefore, the question still remains, how do I get a function from a user and make it work????? I don;t care about the errors that can occur for now, one step at time. Just the converting a text string that comes from an Input box and turning it into a formula that I can pass variables to and have an answer come out.
Let's do a simple example of what i am trying to do
myfunction = inputbox("Enter your function in terms of X)
the user enters X^2 into the input box
now in my code let's say I do
x=3
answer = myfunction(x)
therefore answer would be 9. The problem is that myfunction is a string!!!!!!!!! So this will not work. I want to make it work exactly like this.
I hope this helps to clarify things
Thanks

20020828, 06:43 #7
 Join Date
 Mar 2002
 Posts
 84,353
 Thanks
 0
 Thanked 29 Times in 29 Posts
Re: assigning formulas (vb6)
As Kevin and charlotte pointed out, you'll have to write a mathematical expression parser. If this is a learning excercise, the fun is in finding it out yourself, of course! <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15> You must build lists of strings you want to look for (operators like + and , parentheses, function names like sin, etc.) and examine the string bit by bit to see if you find one of these. You'll end up with lots of "Select Case ... End Select" blocks, I suppose.
If you want to cheat, you can do a Google search for something like "mathematical expression parser". For instance, this site states is has downloadable VB source code for a parser. I just found the url using Google, I haven't tried it myself.