Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Questions (Windows Mobile)
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Questions (Windows Mobile) Post any question regarding Basic4ppc.

Calculating a textbox's text?

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-03-2008, 09:24 PM
Senior Member
 
Join Date: Sep 2008
Posts: 161
Default Calculating a textbox's text?

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.
Reply With Quote
  #2 (permalink)  
Old 10-04-2008, 06:35 AM
klaus's Avatar
Basic4ppc Expert
 
Join Date: Oct 2007
Location: Fully, Switzerland
Posts: 4,463
Awards Showcase
Forum Contributer Beta Tester Competition Winner 
Total Awards: 3
Default

You should have a look at this program.
http://www.basic4ppc.com/forum/code-...evaluator.html

Best regards.
__________________
Klaus
Switzerland

Beginner's Guide / User's Guide
Reply With Quote
  #3 (permalink)  
Old 10-04-2008, 01:24 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,726
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculating distance abastien Code Samples & Tips 8 01-10-2009 08:49 AM
Text an einen bereits bestehenden Text "anhängen" JOTHA German Forum 7 09-12-2008 01:08 PM
Label/Text Control text centering TWELVE Basic4ppc Wishlist 1 06-04-2008 03:20 PM
calculating differences in time sunnyboyj Questions (Windows Mobile) 1 02-07-2008 07:36 PM


All times are GMT. The time now is 02:36 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0