i want to place an equation in a text box at run time. ie: Y = mx + b, then having m, x and b as numbers in other boxes i want to solve for y? can this be done?
if not, can someone explain why?:confused: thanks
Printable View
i want to place an equation in a text box at run time. ie: Y = mx + b, then having m, x and b as numbers in other boxes i want to solve for y? can this be done?
if not, can someone explain why?:confused: thanks
What you have to do is parse the string representing the equation and depending on how complicated your equation is this may not be easy.Quote:
Originally Posted by Ashly6000
However, if
A) you're going to use always the same equation, then there's no need to add it at run time (and you could use Microsoft's Equation Editor' to make it look nicer)
B) you're going to use equations of different types then you'll have to think about the number of textboxes you need for the different constants (m and b in your example) and maybe you'll want to create some of them at run-time depending on the user input equation.
A number of years ago I wrote some code for doing this but it was so difficult that I finally decided to use RPN
http://en.wikipedia.org/wiki/Reverse_Polish_notation
as in the HP handheld calculators to input the equations (much easier to parse!)
can you give me an example of what you did, withyout using RPN. for the equation i gave Y = mx + b only and maybe explain how this is parsed so that it can be solved.........if not possible, then illustrate it using RPN using this equation from the text box, as different equations will be used. thanks:)Quote:
B) you're going to use equations of different types then you'll have to think about the number of textboxes you need for the different constants (m and b in your example) and maybe you'll want to create some of them at run-time depending on the user input equation.
Using RPN, you'd type something likeQuote:
Originally Posted by Ashly6000
xm*b+
in the textbox.
Unfortunately I wrote my code in assembler for a VAX machine from the time of mammoths... I haven't used that in about 12 years so the chance I could even understand the code (in case I found it) is a small one.
At any rate, when parsing the string in the textbox try to identify operators like +, -, *, /, etc, for example, if you've typed mx+b you'd find that the third character is + so split the string into mx and b. Then, you know x is the variable so, any letter next to x (m) would be a variable to multiply x, etc.
thanks..gee, looks good.wish i could see it work.is there any way to show this in a small app? thanks for this, i wasn't aware this was possible:)Quote:
At any rate, when parsing the string in the textbox try to identify operators like +, -, *, /, etc, for example, if you've typed mx+b you'd find that the third character is + so split the string into mx and b. Then, you know x is the variable so, any letter next to x (m) would be a variable to multiply x, etc.
What's your language? There was something in VB6 that would do this automatically, though I forget what it was. I haven't seen the same thing in .NET.
One of the problems you are going to have is with typos. If somebody enters something totally goofy, you will need a means to deal with it. Parentheses are also a problem.
I wrote a program that does something like this, but not enough like this for your purposes, I think. In my case, an equation was a string of characters, and I had to solve the equation for a whole set of data for each variable, which is sort of like what you are doing, but your set size is 1. I also had the advantage that my equations were generated, so they were always correct, whereas you are talking about typed in equations, which means that you'd have to add a whole bunch of error checking.
The way I handled it was to work along the string, and if I encountered an "(", I'd look for the matching ")" (there could be nested ones), and pass everything within the "()", but not including them, to the same function. In other words, the function would parse a string, any string, and return something. However, since I was working on a set, it didn't return just one thing. In your case, you'd just need to return a single number.
It won't be all that easy.
If you are indeed running in VB6, you might have gotten lucky. You can use a Scripting object set to run VBScript's Eval function to evaluate complex arithmetic expressions just like VB does [I forget the specifics, but you can either search for it from here or ask someone on the VB6 board]. This would get you the value of Y without much work at all. However if you're just using linear equations, this is pointlessly overcomplicated as it's pretty easy to get m and b.
In fact, are you always using a linear slope-intercept equation? Will it always be of the form m*x+b inside the textbox? If so you can use what's been suggested and use InStr [in VB6 and probably something similar in .NET] to locate the first * and + characters and then Mid to extract the numbers m and b.
Edit: 1000th post, yay!