Calculating a textbox's text?

Discussion in 'Questions (Windows Mobile)' started by N1c0_ds, Oct 3, 2008.

  1. N1c0_ds

    N1c0_ds Active Member Licensed User

    Let's say I write "3/5*265^2" in a textbox, would there be a way to calculate the result? I can't figure out a way to do this in a simple way.
     
  2. klaus

    klaus Expert Licensed User

  3. Erel

    Erel Administrator Staff Member Licensed User

    You can use this code:
    Code:
    Sub Globals
        
    'Declare the global variables here.
        Dim numPri(30), numVal(30),numOwned(30)
        
    Dim Opr(30),priority(60),words(30)
        
    Dim numI, oprI
        pMAX = 
    0
    End Sub

    Sub App_Start
        
    Msgbox(Eval("2+4-5"))
        
    Msgbox(Eval(TextBox1.Text))
    End Sub

    Sub Eval(fmla)
        fmla = StrToLower(fmla)
        fmla = 
    "0+" & fmla & "+0"
        fmla = StrReplace(fmla,
    "-(","-1*(")
        fmla = StrReplace(fmla,
    " ","")
        fmlaLen = StrLength(fmla)
        c = 
    0
        i2 = 
    0
        
    For i = 0 To fmlaLen
            priority(i) = 
    0
        
    Next
        I = StrIndexOf(fmla,
    "(",0)
        
    If I>-1 Then
            p = 
    4
            fmla=StrRemove(fmla,I,
    1)
            priority(I)=p
            index = i
            
    Do While Index <= StrLength(fmla) - 1
                
    If StrAt(fmla,Index) = "(" Then
                    p  = p + 
    4
                    fmla = StrRemove(fmla,Index,
    1)
                    index = index - 
    1
                
    Else If StrAt(fmla,Index) = ")" Then
                    p = p - 
    4
                    fmla = StrRemove(fmla,Index,
    1)
                    index = index - 
    1
                
    End If
                priority(Index)=p
                index = index + 
    1
            
    Loop
        
    End If
        
    For i = 0 To StrLength(fmla) - 1
            
    If StrIndexOf("+-*/^",StrAt(fmla,i),0) >= 0 Then
                words(c) = SubString(fmla,i2,i-i2)
                c = c + 
    1
                i2 = i + 
    1
            
    End If
        
    Next
        words(c) = 
    0
        
    For i = 0 To c+1
            NumPri(i) = 
    0
            NumVal(i) = 
    0
            NumOwned(i) = 
    0
        
    Next
        I=-
    1
        oprI=
    0
        numI=
    0
        
    For Index = 0 To c-2
            nn=words(index)
            
    If StrLength(nn) = 0 Then
                words(Index+
    1)=StrAt(fmla,I+1) & words(Index+1)
            
    Else
                numI = numI +
    1
                i = i + StrLength(nn) + 
    1
                numVal(numI) = nn 
                Opr(numI) = CvrtOprToInt(StrAt(fmla,I))
                numPri(numI) = priority(I-
    1) + calcPRI(Opr(numI))
                
    If numPri(numI)>pMAX Then pMAX = numPri(numI)
            
    End If
        
    Next
        oprI=numI
        numI = numI + 
    1
        numVal(numI) = words(Index)
        numPri(numI) = priority(I-
    1)
        
    Return calcformula
    End Sub

    Sub calcFormula
        
    For p = pMAX To 0 Step -1
            
    For I = 1 To numI - 1
                
    If numPri(I) = p Then 
                    calcBinOpr(I, Opr(I), I + 
    1)
                    numOwned(I + 
    1) = I
                    ans = numVal(
    1)
                
    End If
            
    Next
        
    Next
        pMAX=
    0
        
    Return ans
    End Sub

    Sub calcBinOpr(a,opr1,b) 
        
    Do While numOwned(a) > 0 
            a = NumOwned(a)
        
    Loop
        
    Do While numOwned(b) > 0 
            b = numOwned(b)
        
    Loop
        
    Select opr1
            
    Case 1
                NumVal(a) = NumVal(a) +  NumVal(b)
            
    Case 2
                NumVal(a) = NumVal(a) - NumVal(b)
            
    Case 3
                NumVal(a) = NumVal(a) * NumVal(b)
            
    Case 4
                NumVal(a) = NumVal(a) / NumVal(b)
            
    Case 5
                NumVal(a) = NumVal(a) ^ NumVal(b)
        
    End Select
    End Sub
                
    Sub CvrtOprToInt (oprS) 
        
    Select oprS
            
    Case "+"
                
    Return 1
            
    Case "-"
                
    Return 2
            
    Case "*"
                
    Return 3
            
    Case "/"
                
    Return 4
            
    Case "^"
                
    Return 5
        
    End Select
    End Sub

    Sub calcPRI(I) 
        
    Select I
            
    Case 1,2
                
    Return 0
            
    Case 3,4
                
    Return 1
            
    Case 5
                
    Return 2
        
    End Select
    End Sub
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice