Share Pastebin
Guest
Public paste!

KnifeySpooney

By: a guest | Feb 9th, 2010 | Syntax: VisualBasic | Size: 2.17 KB | Hits: 22 | Expires: Never
Copy text to clipboard
  1. 'Copyright 2010 Jared K (itsjareds@yahoo.com)
  2.  
  3. 'Simple calculator
  4. 'Solves a simple string calculation
  5. 'in form a+b, a-b, a*b, a/b
  6.  
  7. 'This program is free software: you can redistribute it and/or modify
  8. 'it under the terms of the GNU General Public License as published by
  9. 'the Free Software Foundation, either version 3 of the License, or
  10. '(at your option) any later version.
  11.  
  12. 'This program is distributed in the hope that it will be useful,
  13. 'but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. 'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. 'GNU General Public License for more details.
  16.  
  17. 'You should have received a copy of the GNU General Public License
  18. 'along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.    
  20.     Function simpCalc(ByVal str As String) As Integer
  21.         Dim i As Integer
  22.         Dim idx As Integer
  23.         Dim subs As String
  24.         Dim nums(1) As String ' two numbers to calculate
  25.        Dim opType As Integer = -1 ' 0=add,1=sub,2=mult,3=div
  26.        While str.Length > 0
  27.             idx = IIf(opType > -1, 1, 0) ' returns 1 for index if on second num
  28.            subs = Microsoft.VisualBasic.Left(str, 1)
  29.             If Val(subs) > 0 Then
  30.                 nums(idx) &= subs
  31.             ElseIf idx = 0 Then
  32.                 If (subs = "+") Then
  33.                     opType = 0
  34.                 ElseIf (subs = "-") Then
  35.                     opType = 1
  36.                 ElseIf (subs = "*") Or (subs = "x") Then
  37.                     opType = 2
  38.                 ElseIf (subs = "/") Or (subs = "\") Then
  39.                     opType = 3
  40.                 End If
  41.             Else
  42.                 End While
  43.             End If
  44.         str = str.Substring(1)
  45.         End While
  46.         Select Case (opType)
  47.             Case 0
  48.                 i = Val(nums(0)) + Val(nums(1))
  49.             Case 1
  50.                 i = Val(nums(0)) - Val(nums(1))
  51.             Case 2
  52.                 i = Val(nums(0)) * Val(nums(1))
  53.             Case 3
  54.                 i = Val(nums(0)) / Val(nums(1))
  55.             Case Else
  56.                 i = 0
  57.         End Select
  58.         Me.txtDebug.Text = nums(0) & " " & nums(1)
  59.         Return i
  60.     End Function