Results 1 to 7 of 7

Thread: Trying to solve simple math equations from a text box

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    75

    Smile Trying to solve simple math equations from a text box

    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? thanks

  2. #2
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Trying to solve simple math equations from a text box

    Quote Originally Posted by Ashly6000
    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? 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.

    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!)
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    75

    Smile Re: Trying to solve simple math equations from a text box

    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.
    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

  4. #4
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Trying to solve simple math equations from a text box

    Quote Originally Posted by Ashly6000
    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
    Using RPN, you'd type something like

    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.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    75

    Smile Re: Trying to solve simple math equations from a text box

    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

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Trying to solve simple math equations from a text box

    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.
    My usual boring signature: Nothing

  7. #7
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Trying to solve simple math equations from a text box

    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!
    Last edited by jemidiah; Mar 8th, 2008 at 04:27 PM.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width