# string calculator

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.

``````' Programming by Visal .In
' Email : invisal@gmail.com
'
' if you like my code please vote for me in PSCODE
' 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

' \ 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 ")"

' 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

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

' 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

' 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

' 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

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)

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``````

wats the output format of this pgm...

Hello!
Does it really work?!
your source code is very LONG!
in here, use "select case" is better than "Ifelse"
be successful!
Miss.Samane:)

Dosen't work with Visual Basic 5 until you add the following function;

``````Private Function Replace(ByVal convertString As String, ByVal swapThis As String, Optional ByVal withThis As String) As String
' Swaps 'swapThis' with 'withThis' in 'convertString'.
Replace = ""
If swapThis = "" Then swapThis = "?": withThis = swapThis
Do While (convertString <> "")
If Left\$(convertString, Len(swapThis)) = swapThis Then
Replace = Replace + withThis
convertString = Mid\$(convertString, Len(swapThis) + 1)
Else
' Old slow code.
' swapString = swapString + Left\$(convertString, 1)
' convertString = Mid\$(convertString, 2)
' New faster code.
Replace = Replace + Left\$(convertString, InStr(convertString + swapThis, swapThis) - 1)
convertString = Mid\$(convertString, InStr(convertString + swapThis, swapThis))
End If
Loop
End Function``````

startpos = InStr(1, exp, "-") instead of startpos = InStr(1, exp, "+") after the subtract section to make it work

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.