Results 1 to 3 of 3

Thread: Calculation problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    4

    Question

    1.
    Can I do calculations in text boxes? Suppose the text property of a textbox is "2*4+5" and I want the result in an integer type variable iResult so that iResult = 13. So whenever I call iResult it will give me the integer 13, not the string "2*4+5".

    2.
    Suppose, this is the code I have

    for x = 1 to 10
    y = text1.text
    print x
    next x

    Now I will enter "2 * x + 5" in the textbox and the output would be 7,9,11...
    Andaleeb

  2. #2
    Lively Member
    Join Date
    May 2000
    Location
    Norway
    Posts
    112
    I think you will have to make your own calculator. That last part there with X can be complicated. Anyway, I made this code for you to help you get started.
    Code:
    Sub Main()
        Dim Acc As Double
        Dim A(100) As Variant
        Dim i As Integer
        Dim c As Integer
        Dim Calculate As String
        Dim curUnit As String
        Dim pos As Long
        Calculate = "20*3+42/3"
        pos = 1
        i = 1
        'Gets all the components.
        While pos < Len(Calculate) + 1
            curUnit = getUnit(Calculate, pos)
            pos = pos + Len(curUnit)
            A(i) = curUnit
            i = i + 1
        Wend
        
        'The calculator part
        Acc = Val(A(1))
        For c = 2 To i - 1 Step 2
            Select Case A(c)
                Case "*": Acc = Acc * Val(A(c + 1))
                Case "-": Acc = Acc - Val(A(c + 1))
                Case "+": Acc = Acc + Val(A(c + 1))
                Case "/": Acc = Acc / Val(A(c + 1))
                Case Else: MsgBox "unknown"
            End Select
        Next c
        MsgBox Acc
    End Sub
    
    Function isNumber(number As String) As Boolean
        If Trim(Str(Val(number))) = number Then isNumber = True
    End Function
    
    Function getUnit(Calc As String, pos As Long) As String
        getUnit = Mid(Calc, pos, 1)
        If isNumber(getUnit) Then
            getUnit = getUnit & getNumber(Calc, pos + 1)
        End If
    End Function
    
    Function getNumber(Calc As String, pos As Long) As String
        getNumber = Mid(Calc, pos, 1)
        If isNumber(getNumber) Then
            getNumber = getNumber & getNumber(Calc, pos + 1)
        Else
            getNumber = ""
        End If
    End Function

  3. #3
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    If you feel that you could follow some C or pascal code and are interested, I have a small calculator program written in both Turbo Pascal and Turbo C. It inputs an equation and displays the result.

    It is a very good example of parsing.

    If interested, contact me offline.

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