VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



A Basic Maths Function, works out sums + - / * and uses brackets.

by Cinetices (1 Submission)
Category: Math/Dates
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sat 27th December 2003
Date Added: Mon 8th February 2021
Rating: (1 Votes)

A Basic Maths Function, works out sums + - / * and uses brackets.

Rate A Basic Maths Function, works out sums + - / * and uses brackets.



' Syntax for use :  variable = doMaths(sum)
' Example MsgBox doMaths("2*(5*2)+(3*(5*2))")
' Should return an answer of 50
' You may distribute or edit this code any way you like. :¬)

' Any problems e-mail me at [email protected]


Function isInteger(Text) As Boolean
For i = 1 To Len(Text)
For x = 0 To 9
If Trim(Mid(Text, i, 1)) = Trim(x) Then
p = p & Mid(Text, i, 1)
End If
Next x
Next i
If Trim(p) = Trim(Text) Then isInteger = True
End Function

Function doMaths(sumText) As Variant
On Error Resume Next
start:
If isInteger(Mid(sumText, 1, 1)) = False Then
sumText = Mid(sumText, 2, Len(sumText) - 1)
GoTo start
End If
sumText = "0+0+" & sumText

Dim LastX
Dim LastOperator
Dim Temp_Sum
Dim Totals
Totals = 0
x = 1
LastX = 1
LastOperator = "PLUS"
Temp_Sum = 0

For i = 1 To Len(sumText)

If x = Len(sumText) Then
p = Mid(sumText, LastX, x)
If p <> "" And isInteger(p) = True Then
If Totals > 0 Then
If LastOperator = "MULTI" Then Temp_Sum = Temp_Sum * p
If LastOperator = "DIVIDE" Then Temp_Sum = Temp_Sum / p
If LastOperator = "PLUS" Then Temp_Sum = Temp_Sum + p
If LastOperator = "MINUS" Then Temp_Sum = Temp_Sum - p
End If
End If
Exit For
ElseIf Mid(sumText, x, 1) = "*" Then
p = Mid(sumText, LastX, x - LastX)
If p <> "" And isInteger(p) = True Then
If Totals > 0 Then
If LastOperator = "MULTI" Then Temp_Sum = Temp_Sum * p
If LastOperator = "DIVIDE" Then Temp_Sum = Temp_Sum / p
If LastOperator = "PLUS" Then Temp_Sum = Temp_Sum + p
If LastOperator = "MINUS" Then Temp_Sum = Temp_Sum - p
End If
End If
LastOperator = "MULTI"
Totals = Totals + 1
LastX = x + 1
x = x + 1
ElseIf Mid(sumText, x, 1) = "+" Then
p = Mid(sumText, LastX, x - LastX)
If p <> "" And isInteger(p) = True Then
If Totals > 0 Then
If LastOperator = "MULTI" Then Temp_Sum = Temp_Sum * p
If LastOperator = "DIVIDE" Then Temp_Sum = Temp_Sum / p
If LastOperator = "PLUS" Then Temp_Sum = Temp_Sum + p
If LastOperator = "MINUS" Then Temp_Sum = Temp_Sum - p
End If
End If
LastOperator = "PLUS"
LastX = x + 1
Totals = Totals + 1
x = x + 1

ElseIf Mid(sumText, x, 1) = "-" Then

p = Mid(sumText, LastX, x - LastX)
If p <> "" And isInteger(p) = True Then
If Totals > 0 Then
If LastOperator = "MULTI" Then Temp_Sum = Temp_Sum * p
If LastOperator = "DIVIDE" Then Temp_Sum = Temp_Sum / p
If LastOperator = "PLUS" Then Temp_Sum = Temp_Sum + p
If LastOperator = "MINUS" Then Temp_Sum = Temp_Sum - p
End If
End If
LastOperator = "MINUS"
LastX = x + 1
Totals = Totals + 1
x = x + 1
ElseIf Mid(sumText, x, 1) = "/" Then
p = Mid(sumText, LastX, x - LastX)
If p <> "" And isInteger(p) = True Then
If Totals > 0 Then
If LastOperator = "MULTI" Then Temp_Sum = Temp_Sum * p
If LastOperator = "DIVIDE" Then Temp_Sum = Temp_Sum / p
If LastOperator = "PLUS" Then Temp_Sum = Temp_Sum + p
If LastOperator = "MINUS" Then Temp_Sum = Temp_Sum - p
End If
End If
LastOperator = "DIVIDE"
LastX = x + 1
Totals = Totals + 1
x = x + 1
ElseIf Mid(sumText, x, 1) = "(" Then
LastX = x + 1
x = Find(sumText, x + 1, ")", "(", ")", "", "")
p = doMaths(Mid(sumText, LastX, x - LastX))
If p <> "" And isInteger(p) = True Then
If Totals > 0 Then
If LastOperator = "MULTI" Then Temp_Sum = Temp_Sum * p
If LastOperator = "DIVIDE" Then Temp_Sum = Temp_Sum / p
If LastOperator = "PLUS" Then Temp_Sum = Temp_Sum + p
If LastOperator = "MINUS" Then Temp_Sum = Temp_Sum - p
End If
End If
LastX = x + 1
Totals = Totals + 1
x = x + 1
Else
x = x + 1
End If

Next i
doMaths = Temp_Sum * 1
End Function

Function Find(Text, startAt, endchar, newchar, newend, newchar2, newend2) As Integer
x = startAt
For i = startAt To Len(Text)
If Mid(Text, x, 1) = endchar Then
Find = x
ElseIf Mid(Text, x, 1) = newchar Then
x = Find(Text, x + 1, newend, "", "", "", "") + 1
ElseIf Mid(Text, x, 1) = newchar2 Then
x = Find(Text, x + 1, newend2, "", "", "", "") + 1
Else
x = x + 1
End If
Next i
End Function



Download this snippet    Add to My Saved Code

A Basic Maths Function, works out sums + - / * and uses brackets. Comments

No comments have been posted about A Basic Maths Function, works out sums + - / * and uses brackets.. Why not be the first to post a comment about A Basic Maths Function, works out sums + - / * and uses brackets..

Post your comment

Subject:
Message:
0/1000 characters