Results 1 to 14 of 14

Thread: [RESOLVED] Using variables to store equations

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    4

    Resolved [RESOLVED] Using variables to store equations

    Hi everyone, I have a question that i've tried my hardest to figure out (with- out and help) but I can't seem to get anywhere, here it is:

    Lets say I declared "x" as it's own variable (single) and "y" as it's own variable (single), then I wanted to save the equation "y^(2+x)" in another variable - lets say "strEquation" (and I would guess string to be the most appropriate).

    How could I solve the equation in "strEquation" if "x" was a value and "y" was a different value?

  2. #2
    Fanatic Member
    Join Date
    Aug 2005
    Posts
    606

    Re: Using variables to store equations

    post what you have so far?
    Life is about making some things happen, not waiting around for something to happen.

  3. #3
    Fanatic Member
    Join Date
    Aug 2005
    Posts
    606

    Re: Using variables to store equations

    Code:
    Private Sub Command1_Click()
    
    Dim x As Double
    Dim y As Double
    Dim strEquation As Double
    Dim answer As Double
    x = Text1.Text
    y = Text2.Text
    strEquation = y ^ (2 + x)
    answer = strEquation
    
    Text3.Text = answer
    End Sub
    Life is about making some things happen, not waiting around for something to happen.

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    4

    Re: Using variables to store equations

    none of my past attempts were any good, but to give you a better picture lets say:

    I type "x+y^2" (or any other equation) into TextBox1 then when Command1 is pressed it displayed an answer in Label1 if "x" is a value I typed in TextBox2 and "y" was a value in TextBox3. How could I use any formula (with Xs and Ys in) typed in TextBox1 along with the values of x and y to give answer.
    ie.

    Dim y as Single
    Dim x as Single
    Dim strEquation as String

    strEquation = TextBox1
    y = TextBox3
    x = TextBox2
    Label1 = strEquation 'But this needs to be whatever the equation is...

  5. #5
    Fanatic Member
    Join Date
    Aug 2005
    Posts
    606

    Re: Using variables to store equations

    ok I think I see what you want to do:

    1)You'll have to parse the string in text1
    2) then use some conditional statements to convert x to what is in text2 and convert y to what is in text3. Hopefully text2.text & text3.text are integers
    3) label1.caption = strEquation

    Try looking into the Mid & InStr functions for the parsing
    Life is about making some things happen, not waiting around for something to happen.

  6. #6
    New Member
    Join Date
    Nov 2009
    Posts
    15

    Re: Using variables to store equations

    We have strings of numbers that come in from OCR and then we use different math formulas against those numbers to see if they equal a check digit. One is add up all the numbers but the last two then mod 36 and that should equal the last two digits.
    Every time we have a new formula we need to recompile the code. I have two ideas for fixing this but have not been able to test much.
    First idea
    Take the string that represents your formula and write it into tempFormula.vbs where this vbs returns the awnser. You could also use python or any other scripting language.
    Second idea
    form your formula into a select statement and send it to SQL then you'll get your answer as a record set.

    Both require a fair amount of string parsing and the ability to convert your formula into the other language.

  7. #7
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Using variables to store equations

    You can't declare it as a String. How is VB going to perform math on a String?
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  8. #8
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Using variables to store equations

    Have a look at the Microsoft Script Control - you can give it a string equation and it will evaluate it:
    Code:
    Private Sub Command1_Click()
        MsgBox ScriptControl1.Eval("2^(1+2)")
    End Sub

  9. #9

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    4

    Re: Using variables to store equations

    Thank you all for the responses , and after reading through them I searched around abit on different websites about that scriptcontrol and found this:

    TextBox1 has the equation
    TextBox2 has teh value of X
    label 6 is teh solution

    Private Sub CommandButton1_Click()

    FIn = TextBox1.Text
    xVar = Val(TextBox2.Text)

    FLenIn = Len(FIn)
    Nx = 0

    For i = 1 To FLenIn
    If (Mid(FIn, i, 1) = "x" And Mid(FIn, i + 1, 1) <> "p") Then
    Nx = Nx + 1
    End If
    Next i

    FLenOut = FLenIn + 2 * Nx
    Fout = ""

    i = 0: j = 0
    Do While j <= FLenOut
    i = i + 1
    j = j + 1
    If (Mid(FIn, i, 1) = "x" And Mid(FIn, i + 1, 1) <> "p") Then
    j = j + 2
    Fout = Fout & xVar
    Else
    Fout = Fout & Mid(FIn, i, 1)
    End If
    Loop

    Label5.Caption = Fout
    Label6.Caption = Evaluate(Fout)


    End Sub
    it works (if I copy and paste some of the script for "y" as well), but I don't really understand exactly what it does, can anyone recommend a website that helps someone learn/improve their knowledge on in-depth VB6 scripting (or any scripting) like the dilemma I had?

  10. #10
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Using variables to store equations

    As long as your formulas are always laid out in full (e.g. A * B + C rather than AB + C) then you can do something as simple as just using the replace function to put the values in the correct place:
    Code:
    Private Sub Command1_Click()
        Dim strFormula As String
    
        strFormula = Text1.Text ' e.g. "x+y^2"
    
        strFormula = Replace(strFormula, "x", Text2.Text)
        strFormula = Replace(strFormula, "y", Text3.Text)
    
        MsgBox ScriptingControl1.Eval(strFormula)
    End Sub

  11. #11

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    4

    Re: Using variables to store equations

    Quote Originally Posted by bushmobile View Post
    As long as your formulas are always laid out in full (e.g. A * B + C rather than AB + C) then you can do something as simple as just using the replace function to put the values in the correct place:
    Code:
    Private Sub Command1_Click()
        Dim strFormula As String
    
        strFormula = Text1.Text ' e.g. "x+y^2"
    
        strFormula = Replace(strFormula, "x", Text2.Text)
        strFormula = Replace(strFormula, "y", Text3.Text)
    
        MsgBox ScriptingControl1.Eval(strFormula)
    End Sub
    ty; this is very useful in the program i am trying to create

  12. #12
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Using variables to store equations

    Now that we've helped you, you can help us by marking the thread as resolved. If you have JavaScript enabled you can do that by selecting the Mark Thread Resolved item from the Thread Tools menu. Otherwise please insert "[Resolved]" at the start of the Subject and select the green checkmark from the Post Icons. Also if someone has been particularly helpful you have the ability to affect their forum "reputation" by rating their post. Only those ratings that you give after you have 20 posts will actually count, but in all cases the person you rate will see your rating and know that you appreciate their help.

  13. #13
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Using variables to store equations

    Quote Originally Posted by bushmobile View Post
    Have a look at the Microsoft Script Control - you can give it a string equation and it will evaluate it:[code]Private Sub
    This is good to know and admittedly I was unaware of it. That said, my reply was based on the OPs first post. In it he didn't mention that the formulas were in TextBoxes. The OP said he wanted to store his formulas in variables.

    This is handy to know though. Thanks for posting it.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  14. #14
    Lively Member
    Join Date
    Jan 2009
    Posts
    93

    Re: [RESOLVED] Using variables to store equations

    Not that this matters now, but you could simply create a function and pass in the X and Y value, run the equation and return the results.

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