我写了一段代码,做简单的加减乘除,可为什么不出结果?比如我输入10+2,出来的是10+2这个字符串,而不是12这个结果?Private Sub calculateB_Click()
If a.Value = True Then
choice = "+"
Else
If b.Value = True Then
choice = "-"
Else
If c.Value = True Then
choice = "*"
Else
If d.Value = True Then
choice = "/"
End If
End If
End If
End If
displayB.Text = numA & choice & numB
End Sub
Option Explicit Private Function Eval(ByVal s As String) As Variant
Dim Lb As Long, Rb As Long
Lb = InStrRev(s, "(")
While Lb <> 0
Rb = InStr(Lb, s, ")")
s = Replace(s, Mid$(s, Lb, Rb - Lb + 1), CStr(Eval(Mid$(s, Lb + 1, Rb - Lb - 1))))
Lb = InStrRev(s, "(")
Wend
If IsNumeric(s) Then
Eval = Val(s)
Else
Dim High As Long
High = InStr(s, "+")
If High <> 0 Then
Eval = Eval(Left$(s, High - 1)) + Eval(Right$(s, Len(s) - High))
Exit Function
End If
High = InStrRev(s, "-")
If High <> 0 Then
Eval = Eval(Left$(s, High - 1)) - Eval(Right$(s, Len(s) - High))
Exit Function
End If
High = InStr(s, "*")
If High <> 0 Then
Eval = Eval(Left$(s, High - 1)) * Eval(Right$(s, Len(s) - High))
Exit Function
End If
High = InStrRev(s, "/")
If High <> 0 Then
Eval = Eval(Left$(s, High - 1)) / Eval(Right$(s, Len(s) - High))
Exit Function
End If
High = InStrRev(s, "%")
If High <> 0 Then
Eval = Eval(Left$(s, High - 1)) Mod Eval(Right$(s, Len(s) - High))
Exit Function
End If
High = InStrRev(s, "!=")
If High <> 0 Then
Eval = Eval(Left$(s, High - 1)) <> Eval(Right$(s, Len(s) - High - 1))
Exit Function
End If
High = InStrRev(s, "==")
If High <> 0 Then
Eval = (Eval(Left$(s, High - 1)) = Eval(Right$(s, Len(s) - High - 1)))
Exit Function
End If
End If
End Function '主函数
Public Sub main()
Dim Expression As String
Expression = "149.5+((100+(6+(90-5*2*2)*4+(1-1))+202)%441)*2*2+0.88+150.5"
MsgBox Eval(Expression)
End Sub
If a.Value = True Then
displayB.Text = numA + numB
Else
If b.Value = True Then
displayB.Text = numA - numB
Else
If c.Value = True Then
displayB.Text = numA*numB
Else
If d.Value = True Then
displayB.Text = numA/ numB
End If
End If
End If
End If
displayB.Text = numA & choice & numB
End Sub
If a.Value = True Then
displayB.Text = numA + numB
Else
If b.Value = True Then
displayB.Text = numA - numB
Else
If c.Value = True Then
displayB.Text = numA*numB
Else
If d.Value = True Then
displayB.Text = numA/ numB
End If
End If
End If
End If
End Sub
private function 计算结果(byval 操作符 as string) as double
select case 操作符
case "+"
计算结果= num1+num2
case "-"
计算结果= num1-num2
case "*"
计算结果= num1*num2
case "/"
if num2<>0 then
计算结果= num1/num2
else
'计算结果 除以0
end if
end selectend function
Dim scr
Set scr = CreateObject("MSScriptControl.ScriptControl")
scr.Language = "vbscript"
'通过脚本计算器来计算出结果
displayB.Text = scr.Eval(numA & choice & numB )
Dim scr
Set scr = CreateObject("MSScriptControl.ScriptControl")
scr.Language = "vbscript"
'通过脚本计算器来计算出结果
Text1 = scr.Eval("1+2+3*5")
返回 18
function 计算(计算公式 as string) as double
Dim scr
Set scr = CreateObject("MSScriptControl.ScriptControl")
scr.Language = "vbscript"
'通过脚本计算器来计算出结果
on error resume next
计算 = scr.Eval(计算公式)end function
调用方式 Text1 = 计算("1+1+2")