PDA

View Full Version : حرفه ای: محاسبه مجموعه ای از عملیات ریاضی که به صورت رشته ذخیره شده اند؟



arash020
پنج شنبه 19 بهمن 1391, 23:03 عصر
سلام
چجوری میتونم یه رشته اطلاعات شامل عمگر و عملوند رو یکجا و درست از نظر ترتیب اجرا و صحت جواب
به صورت ریاضی محاسبه کنم؟؟؟
مثلا فرض کنید ورودی باشه :
0.25*(0.5-0^2+1)
میخوام خروجی دقیقا جواب واقعی به صورت ریاضی باشه.

ممنون میشم راهنمایی کنید.

امین مستانی
جمعه 20 بهمن 1391, 11:35 صبح
سلام

این تابع برای زبان VB6 هست. راحت میشه به دات نت تبدیلش کرد :



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

arash020
جمعه 20 بهمن 1391, 23:09 عصر
تشکر قد دنیا