this function will calculate a string formula which support cos, sin, log, abs, mod, and example : [10*2+(2+3)]/(5 mod 4) and you will get the answer, and this function it really easy to plugin with you code.
string calculator
' Programming by Visal .In
' Email : invisal@gmail.com
'
' if you like my code please vote for me in PSCODE
' here the link
' http://www.pscode.com/vb/scripts/showcode.asp?txtCodeId=59600&lngWId=1
Function Calc(exp As String) As Double
'start searching for ()
'everything between "(" and ")"
'will calculate first
Dim startpos As Integer
Dim Count As Integer
Dim lexp As String ' left expression
Dim rexp As String ' right expression
Dim cmid As String
Dim exp2 As String
Dim answer2 As Double
' \ and / are same division
exp = Replace(exp, "\", "/")
' "[" and "(" are the same also "]" and ")"
exp = Replace(exp, "[", "(")
exp = Replace(exp, "]", ")")
startpos = InStr(1, exp, "(")
'we have found "("
If startpos > 0 Then
'searching for ")"
For endpos = (startpos + 1) To Len(exp)
Select Case Mid(exp, endpos, 1)
Case "("
Count = Count + 1
Case ")"
If Count = 0 Then Exit For
Count = Count - 1
End Select
Next endpos
' if there no ")" we will create ")" automatic
If Count > 0 Then
exp = exp & Replace(Space(Count + 1), " ", ")")
endpos = endpos + Count
End If
' check if () is empty or not
If endpos <= startpos + 1 Then
' if it empty it mean 0
exp2 = "0"
exp2 = Replace(exp, "()", "0")
Else
' get expression between "(" and ")"
exp2 = Mid(exp, startpos + 1, endpos - (startpos + 1))
' calculate expression between "(" and ")"
answer2 = Calc(exp2)
' replace the expression between "(" and ")"
' with the result that we have calculate
exp2 = Replace(exp, "(" & exp2 & ")", answer2)
End If
Calc = Calc(exp2)
Exit Function
ElseIf InStr(1, exp, "abs", vbTextCompare) > 0 Then
' get absolute start position
startpos = InStr(1, exp, "abs", vbTextCompare)
' clear all the text in exp2
exp2 = ""
' get number
For endpos = startpos + 3 To Len(exp)
cmid = Mid(exp, endpos, 1)
If IsNumeric(cmid) = True Or cmid = "." Or cmid = "," Then
exp2 = exp2 & cmid
Else
Exit For
End If
Next endpos
If exp2 <> "" Then
' convert it into absolute
answer2 = Abs(CDbl(exp2))
' replace the adsolute number with the answer
exp2 = Replace(exp, "abs" & exp2, answer2)
Calc = Calc(exp2)
Else
Mid(exp, startpos, 3) = "000"
Calc = Calc(exp)
End If
Exit Function
ElseIf InStr(1, exp, "cos", vbTextCompare) > 0 Then
' get cosines start position
startpos = InStr(1, exp, "cos", vbTextCompare)
' clear all the text in exp2
exp2 = ""
' get cosines angle
For endpos = startpos + 3 To Len(exp)
cmid = Mid(exp, endpos, 1)
If IsNumeric(cmid) = True Or cmid = "." Or cmid = "," Then
exp2 = exp2 & cmid
Else
Exit For
End If
Next endpos
If exp2 <> "" Then
' calculate cosines
answer2 = Cos(CDbl(exp2))
' replace cosines with the answer
exp2 = Replace(exp, "cos" & exp2, answer2)
Calc = Calc(exp2)
Else
Mid(exp, startpos, 3) = "000"
Calc = Calc(exp)
End If
Exit Function
ElseIf InStr(1, exp, "sin", vbTextCompare) > 0 Then
' get sines start position
startpos = InStr(1, exp, "sin", vbTextCompare)
' clear all the text in exp2
exp2 = ""
' get sines angle
For endpos = startpos + 3 To Len(exp)
cmid = Mid(exp, endpos, 1)
If IsNumeric(cmid) = True Or cmid = "." Or cmid = "," Then
exp2 = exp2 & cmid
Else
Exit For
End If
Next endpos
If exp2 <> "" Then
' calculate sines
answer2 = Sin(CDbl(exp2))
' replace cosines with the answer
exp2 = Replace(exp, "sin" & exp2, answer2)
Calc = Calc(exp2)
Else
Mid(exp, startpos, 3) = "000"
Calc = Calc(exp)
End If
Exit Function
ElseIf InStr(1, exp, "log", vbTextCompare) > 0 Then
' get logarithmic start position
startpos = InStr(1, exp, "log", vbTextCompare)
' clear all the text in exp2
exp2 = ""
' get number
For endpos = startpos + 3 To Len(exp)
cmid = Mid(exp, endpos, 1)
If IsNumeric(cmid) = True Or cmid = "." Or cmid = "," Then
exp2 = exp2 & cmid
Else
Exit For
End If
Next endpos
If exp2 <> "" Then
' calculate logarithmic
answer2 = Log(CDbl(exp2))
' replace logarithmic with the answer
exp2 = Replace(exp, "log" & exp2, answer2)
Calc = Calc(exp2)
Else
Mid(exp, startpos, 3) = "000"
Calc = Calc(exp)
End If
Exit Function
ElseIf InStr(1, exp, "mod") > 1 Then
' modulo
startpos = InStr(1, exp, "mod")
lexp = Left(exp, startpos - 1)
rexp = Right(exp, Len(exp) - (startpos + 2))
Calc = Calc(lexp) Mod Calc(rexp)
Exit Function
ElseIf InStr(1, exp, "+") > 1 Then
' addition
startpos = InStr(1, exp, "+")
lexp = Left(exp, startpos - 1)
rexp = Right(exp, Len(exp) - startpos)
Calc = Calc(lexp) + Calc(rexp)
Exit Function
ElseIf InStr(1, exp, "-") > 1 Then
' subtract
startpos = InStr(1, exp, "+")
lexp = Left(exp, startpos - 1)
rexp = Right(exp, Len(exp) - startpos)
Calc = Calc(lexp) - Calc(rexp)
Exit Function
ElseIf InStr(1, exp, "*") > 1 Then
' multiply
startpos = InStr(1, exp, "*")
lexp = Left(exp, startpos - 1)
rexp = Right(exp, Len(exp) - startpos)
Calc = Calc(lexp) * Calc(rexp)
Exit Function
ElseIf InStr(1, exp, "/") > 1 Then
' divide
startpos = InStr(1, exp, "/")
lexp = Left(exp, startpos - 1)
rexp = Right(exp, Len(exp) - startpos)
answer2 = Calc(rexp)
If answer2 = 0 Then answer2 = 1
Calc = Calc(lexp) / answer2
Exit Function
ElseIf InStr(1, exp, "^") > 1 Then
' exponent
startpos = InStr(1, exp, "^")
lexp = Left(exp, startpos - 1)
rexp = Right(exp, Len(exp) - startpos)
Calc = Calc(lexp) ^ Calc(rexp)
Exit Function
Else
If IsNumeric(exp) = True Then
Calc = CDbl(exp)
Else
Calc = 0
End If
Exit Function
End If
End Function
sushmag 0 Newbie Poster
Samane_Bagheri 0 Newbie Poster
Mark A 0 Newbie Poster
JoeyJoeC 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.