Results 1 to 14 of 14

Thread: math question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    67
    hey u guys i need a code for the following :

    Some one writes a + or - or * or / into a text box there are

    4 such text boxes and in between of those numbers like: (the [ ] stand for a text box):

    example:
    [ 2 ] [ - ] [ 3 ] [ + ] [ 5 ][ / ] [ 1 ]

    now how do i get the computer to solve that?

    im sure its easy cause i think i have done it before i just cant remember it now, thanks a lot

    I hope i didn't make it confusing
    Private Sub DeepCrap
    on error blame microsoft
    if microsoft = screwed then
    blame aol
    end if
    end deep crap

    Ahh VB6 Enterprise Edition

  2. #2
    Addicted Member
    Join Date
    Aug 2000
    Posts
    208
    I'm not sure If I understand:
    [ 2 ] [ - ] [ 3 ] [ + ] [ 5 ][ / ] [ 1 ]

    TOTAL= val(text1.text) - val(text2.text) _
    + val(text3.text) / val(text4.text)


    is it what you want ?


  3. #3
    Lively Member
    Join Date
    Aug 2000
    Location
    Texas
    Posts
    88
    It will be easier if you use textbox array.

    Code:
    Private Sub Command1_Click()
        Dim i As Integer
        Dim var1 As Integer
        Dim var2 As Integer
        
        var1 = CInt(Text1(0))
        For i = 0 To 6
            If Text1(i).Text = "+" Then
                var2 = CInt(Text1(i + 1))
                var1 = var1 + var2
            ElseIf Text1(i).Text = "-" Then
                var2 = CInt(Text1(i + 1))
                var1 = var1 - var2
            ElseIf Text1(i).Text = "*" Then
                var2 = CInt(Text1(i + 1))
                var1 = var1 * var2
            ElseIf Text1(i).Text = "/" Then
                var2 = CInt(Text1(i + 1))
                var1 = var1 / var2
            End If
        Next i
        'Result
        Text2.Text = var1
    End Sub
    [Edited by sanon on 09-23-2000 at 02:52 AM]

  4. #4
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Question: What about BEDMAS?

    In one of the replies, I note that no account is made for order of operation. Is this a requirement of your final solution?

    Regards

    Paul Lewis

  5. #5
    Guest
    If you mean you want a way of solving any simple equation that the user inputs, then there are Fortran-based .dll's around.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    67
    No order of operations don't make a diffrence to me That i'll take care of... and yes what i need is for the user to put in numbers like 4 2 3 5 12 (or whatever all go into their own text box) then in between they type in a -,+,*, or /

    Ie:
    4 + 2 - 3 * 5 - 12

    but im not sure what numbers or symbols they will enter... but say they enter: 4 + 2 - 3 * 5 - 12 then I want the computer to solve that but all of that is in its own text box so i can't just type in text1.text + text2.text because what if its not a plus but a minus?

    Thanks a lot I hope im not confusing you guys
    Private Sub DeepCrap
    on error blame microsoft
    if microsoft = screwed then
    blame aol
    end if
    end deep crap

    Ahh VB6 Enterprise Edition

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2000
    Posts
    67
    actually the symbols will be computer generated heres the code i got so far:
    <code>
    Dim A
    Dim B
    Dim C
    Dim D
    Dim E
    Dim F
    Dim G
    Dim H
    Dim I
    Dim J
    Private Sub Getarandom_Number()
    '// Picking a Random symbol//////////////////////////////////////////////////
    Randomize
    retval = Int((4 * Rnd) + 1)
    If retval = 1 Then retsign = "+"
    If retval = 2 Then retsign = "-"
    If retval = 3 Then retsign = "*"
    If retval = 4 Then retsign = "/"
    Text2.Text = retsign
    Randomize
    retval = Int((4 * Rnd) + 1)
    If retval = 1 Then retsign = "+"
    If retval = 2 Then retsign = "-"
    If retval = 3 Then retsign = "*"
    If retval = 4 Then retsign = "/"
    Text4.Text = retsign
    Randomize
    retval = Int((4 * Rnd) + 1)
    If retval = 1 Then retsign = "+"
    If retval = 2 Then retsign = "-"
    If retval = 3 Then retsign = "*"
    If retval = 4 Then retsign = "/"
    Text6.Text = retsign
    Randomize
    retval = Int((4 * Rnd) + 1)
    If retval = 1 Then retsign = "+"
    If retval = 2 Then retsign = "-"
    If retval = 3 Then retsign = "*"
    If retval = 4 Then retsign = "/"
    Text8.Text = retsign
    '//end picking a random symbol////////////////////////////////////////////////
    End Sub

    Private Sub Cmdok_Click()
    Getarandom_Number
    Attemptto_Solve
    End Sub

    Private Sub Attemptto_Solve()
    'Try and solve given the current numbers and symbols
    A = Text1.Text
    B = Text2.Text
    C = Text3.Text
    D = Text4.Text
    E = Text5.Text
    F = Text6.Text
    G = Text7.Text
    H = Text8.Text
    I = Text9.Text
    J = Text10.Text

    End Sub

  8. #8
    Guest
    You like going the long way around huh nitriolic2?

    you should check out SELECT CASE statements and DEFSTR

    Code:
    Dim A
    Dim B
    Dim C
    ...
    
    'would be replaced by
    Defstr A-J
    
    'or even better, use an array.
    
    'and then ...
    If retval = 1 Then retsign = "+" 
    If retval = 2 Then retsign = "-" 
    If retval = 3 Then retsign = "*" 
    If retval = 4 Then retsign = "/" 
    
    'would be replaced by
    
    Select case Retval
    case 1
    retsign = "+"
    case 2
    retsign = "-"
    case 3
    retsign = "*"
    case 4
    retsign = "/"
    end select
    and perhaps loop through a control array of textboxes instead of naming them explicitly.

    Just an idea.

  9. #9
    Lively Member
    Join Date
    Feb 2000
    Posts
    118

    A start

    Here is a start(I'll let you type out the rest)
    Code:
    Option Explicit
    
    Dim retval
    Dim retsign
    Dim A As Integer
    Dim B
    Dim C As Integer
    Dim D
    Dim E As Integer
    Dim F
    Dim G As Integer
    Dim H
    Dim i As Integer
    Dim J
    Private Sub Getarandom_Number()
    '// Picking a Random symbol/
    Dim i As Integer
    For i = 0 To 3
    Randomize
    retval = Int((4 * Rnd) + 1)
    If retval = 1 Then retsign = "+"
    If retval = 2 Then retsign = "-"
    If retval = 3 Then retsign = "*"
    If retval = 4 Then retsign = "/"
    Text2(i).Text = retsign
    Next i
    
    End Sub
    
    
    Private Sub Load_Values()
    
    A = Val(Text1(0).Text)
    B = Text2(0).Text
    C = Val(Text1(1).Text)
    D = Text2(1).Text
    E = Val(Text1(2).Text)
    F = Text2(2).Text
    G = Val(Text1(3).Text)
    H = Text2(3).Text
    i = Val(Text1(4).Text)
    End Sub
    
    Private Sub Command1_Click()
    Getarandom_Number
    Load_Values
    End Sub
    
    Private Sub Command2_Click()
    
         Dim var1 As Integer
         var1 = A
            If B = "+" Then
                var1 = var1 + C
            ElseIf B = "-" Then
                var1 = var1 - C
            ElseIf B = "*" Then
                
                var1 = var1 * C
            ElseIf B = "/" Then
                
                var1 = var1 / C
            End If
        Text3.Text = var1
    
      'just keep going
        
    End Sub
    Put your numbers in textbox array Text1(0 to 4) and operators in textbox array Text2(0 to 3). answer is text 3. To test it make sure the Text(0)is a higher number than Text(1) in case of division. This will only do the first equation. Repeat this with Var2 and so on then add var1, var2 etc. for the answer.
    Kokopeli
    VB6 SP3

  10. #10
    Member
    Join Date
    Aug 1999
    Location
    Houston
    Posts
    48

    symbolic programming

    What you want to do is called symolic programming. Do a search at zdnet.com or northernlight.com using the search terms:

    visual basic symbolic (or)
    visual basic equation symbolic

    hope this helps.

    -lp

  11. #11
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Well personally, I would do this as one text string (I'd get the input and concatenate it to one expression or just ask for input like "1+3-7/4", spaces don't matter) and I'd use a stack system.

    Parse the expression string and split it into terms by the * and / operators, then split the terms into sub-terms by the + and - operators. That would be done by functions that analyse substring expressions that are passed to them. When you have the subterms you apply the + and - operators to them with an add/subtract function, and when values for those functions are returned you apply the * and / operators to them with a multiply/divide function.

    Well that's how I'd do it anyway
    Harry.

    "From one thing, know ten thousand things."

  12. #12
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Err, HarryW, don't you mean...

    When you have the subterms you apply the + and - operators to them with an add/subtract function, and when values for those functions are returned you apply the * and / operators to them with a multiply/divide function.
    I think you mean that you would do the / and * first, then once those have returned, do the + and -? The way you wrote it, it seems like you'd propose doing it backwards

    But in case I read your suggestion backwards, I can tell you now that I agree with your general approach. I made the same sort of thing into a set of Java Classes for a project I worked on. When you start allowing user defined functions and brackets (not to mention exponents), life gets interesting

    Cheers
    Paul Lewis

  13. #13
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    You're right, I think I got it backwards, thank you for pointing that out

    Brackets are pretty easy to add, you just treat each bracketed expression as an individual expression and call your main evaluation function again, ie use recursion. I can imagine that user-defined functions and exponents make things interesting though Any idea how to do square roots?
    Harry.

    "From one thing, know ten thousand things."

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    For a square root, how about something like:
    Code:
    (number)^0.5
    ...that way you can use everything else in the normal way.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

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