怎么把算式自动计算?

在一个文本框text1输入算式,比如3+6+8+104,怎么在按下按钮后自动计算,把结果放到另一个文本框text2

aluckdog
浏览 184回答 2
2回答

至尊宝的传说

Private Type Parents As Stringvalue As DoubleEnd TypePrivate Type Pluss As Stringvalue As DoubleEnd Type'下面是实现此功能的函数的定义Public Function ValueOfExpression(ByVal Express As String) As DoubleDim Pa() As Parent, ParNum As Integer, Ps() As Plus, OperNum As IntegerDim str0 As String'按括号分解表达式 Express:Begin-----/*Dim lenExp As Integer, Lenstr1 As Integer, i As Integer, j As Integer, k As Integer, str1 As String, str2 As String, intPar As IntegerDim intStart As Integer, intEnd As IntegerlenExp = Len(Express)For i = 1 To lenExpIf Mid(Express, i, 1) = "(" Then intPar = intPar + 1NextParNum = intParReDim Pa((intPar / 10 + 1) * 10)For i = 1 To intParIf intPar < 1 Then Exit ForFor j = 1 To lenExpIf Mid(Express, j, 1) = ")" Thenstr1 = Mid(Express, 1, j - 1)Exit ForEnd IfNextLenstr1 = Len(str1)For k = 1 To Lenstr1If Mid(str1, Lenstr1 + 1 - k, 1) = "(" ThenPa(i).s = Mid(str1, Lenstr1 + 2 - k)Exit ForEnd IfNextExpress = Mid(Express, 1, Lenstr1 - k) & Chr(128) & CStr(i) & Mid(Express, j + 1)lenExp = Len(Express)NextPa(0).s = Express'*/-----End'按加减号进一步分解:Begin-----/*Dim n As Integer, strLeft As StringFor i = 0 To ParNumk = 0For j = 1 To Len(Pa(i).s)str1 = Mid(Pa(i).s, j, 1)If str1 = "+" Or str1 = "-" Then k = k + 1NextIf k > OperNum Then OperNum = kNextReDim Ps(ParNum, OperNum)For i = 0 To ParNumstrLeft = Pa(i).s: n = 0: str2 = ""DoIf Len(strLeft) = 0 Then Exit DoFor j = 1 To Len(strLeft)str1 = Mid(strLeft, j, 1)If str1 = "+" Or str1 = "-" ThenPs(i, n).s = str2 & Mid(strLeft, 1, j - 1)n = n + 1str2 = IIf(str1 = "-", str1, "")strLeft = Mid(strLeft, j + 1)Exit ForEnd IfIf j = Len(strLeft) ThenPs(i, n).s = str2 & strLeft: j = 0Exit ForEnd IfNextLoop Until j = 0Next'*/-----End'计算最后分成的多个简单表达式的值的总和,即表达式 Express 的值Dim Total As Double, value As DoubleFor i = 1 To ParNum + 1If i = ParNum + 1 Then i = 0Total = 0For j = 0 To OperNumExpress = Ps(i, j).s: value = 0Dim lasti As Integer, operator As StringlenExp = Len(Express): lasti = 0: operator = ""For k = 1 To lenExpstr0 = Mid(Express, k, 1)If InStr("*/^", str0) > 0 Or k = lenExp ThenIf k = lenExp Then k = k + 1str1 = Mid(Express, lasti + 1, k - 1 - lasti)Dim sign As Integer, Valstr1 As DoubleIf Mid(str1, 1, 1) = "-" Thensign = -1str1 = Mid(str1, 2)Elsesign = 1End Ifn = InStr(1, "/sin" & Chr(128) & "/cos" & Chr(128) & "/tan" & Chr(128) & "/abs" & Chr(128) & "/atn" & Chr(128) & "/exp" & Chr(128) & "/int" & Chr(128) & "/fix" & Chr(128) & "/sgn" & Chr(128) & "/sqr" & Chr(128) & "/", "/" & Mid(str1, 1, 4) & "/")If n > 0 ThenValstr1 = Choose((n + 4) / 5, Sin(Pa(Val(Mid(str1, 5))).value), Cos(Pa(Val(Mid(str1, 5))).value), Tan(Pa(Val(Mid(str1, 5))).value), Abs(Pa(Val(Mid(str1, 5))).value), Atn(Pa(Val(Mid(str1, 5))).value), Exp(Pa(Val(Mid(str1, 5))).value), Int(Pa(Val(Mid(str1, 5))).value), Fix(Pa(Val(Mid(str1, 5))).value), Sgn(Pa(Val(Mid(str1, 5))).value), Sqr(Pa(Val(Mid(str1, 5))).value))Elsen = InStr(1, "/lg" & Chr(128) & "/ln" & Chr(128) & "/", Mid(str1, 1, 3))If n > 0 ThenValstr1 = Choose((n + 3) / 4, Log(Pa(Val(Mid(str1, 4))).value) / Log(10), Log(Pa(Val(Mid(str1, 4))).value))ElseIf Mid(str1, 1, 1) = Chr(128) ThenValstr1 = Pa(Val(Mid(str1, 2))).valueElseIf Right(str1, 1) = "!" ThenIf Val(str1) = 0 ThenValstr1 = 1ElseValstr1 = 1For n = 1 To Val(str1)Valstr1 = Valstr1 * nNextEnd IfElseValstr1 = Val(str1)End IfEnd IfEnd IfValstr1 = Valstr1 * signSelect Case operatorCase ""value = Valstr1Case "*"value = value * Valstr1Case "/"value = value / Valstr1Case "^"value = value ^ Valstr1End Selectlasti = k: operator = str0End IfNextPs(i, j).value = valueTotal = Total + Ps(i, j).valueNextPa(i).value = TotalIf i = 0 Then Exit ForNextValueOfExpression = Pa(0).valueEnd Function'使用例子:'在Text1中输入式子,然后:'Text2.Text=ValueOfExpression(Text1.Text)'该函数支持很多数学函数如sin、cos、tan等等,如:'Print ValueOfExpression("sin(1)")'注意后面要加括号'该函数支持运算符+-*/^!,还有括号,^为乘幂,其优先级与*/相同,这是使用该函数需要注意的地方;!为阶乘

忽然笑

on command1_click()计算之后text2.text = 计算结果 ...
打开App,查看更多内容
随时随地看视频慕课网APP