KnifeySpooney
By: a guest | Feb 9th, 2010 | Syntax:
VisualBasic | Size: 2.17 KB | Hits: 22 | Expires: Never
'Copyright 2010 Jared K (itsjareds@yahoo.com)
'Simple calculator
'Solves a simple string calculation
'in form a+b, a-b, a*b, a/b
'This program is free software: you can redistribute it and/or modify
'it under the terms of the GNU General Public License as published by
'the Free Software Foundation, either version 3 of the License, or
'(at your option) any later version.
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
'GNU General Public License for more details.
'You should have received a copy of the GNU General Public License
'along with this program. If not, see <http://www.gnu.org/licenses/>.
Function simpCalc(ByVal str As String) As Integer
Dim i As Integer
Dim idx As Integer
Dim subs As String
Dim nums(1) As String ' two numbers to calculate
Dim opType As Integer = -1 ' 0=add,1=sub,2=mult,3=div
While str.Length > 0
idx = IIf(opType > -1, 1, 0) ' returns 1 for index if on second num
subs = Microsoft.VisualBasic.Left(str, 1)
If Val(subs) > 0 Then
nums(idx) &= subs
ElseIf idx = 0 Then
If (subs = "+") Then
opType = 0
ElseIf (subs = "-") Then
opType = 1
ElseIf (subs = "*") Or (subs = "x") Then
opType = 2
ElseIf (subs = "/") Or (subs = "\") Then
opType = 3
End If
Else
End While
End If
str = str.Substring(1)
End While
Select Case (opType)
Case 0
i = Val(nums(0)) + Val(nums(1))
Case 1
i = Val(nums(0)) - Val(nums(1))
Case 2
i = Val(nums(0)) * Val(nums(1))
Case 3
i = Val(nums(0)) / Val(nums(1))
Case Else
i = 0
End Select
Me.txtDebug.Text = nums(0) & " " & nums(1)
Return i
End Function