我想做一个text,在里面输入一些简单的计算,如何实现,先算乘除后算加减啊?
解决方案 »
- vb 从 access 中选一列最大值,为什么结果不对?平均数对。
- webbrowser 本地html模板,获取远程数据
- debug问题
- 如何判断VB webbrowser 提交后是否成功
- API中SendMessage的问题
- Inet执行完FTP命令之后怎么关闭?
- DataGrid 单独一列的内容颜色可以设置吗?请高手解决
- 如何判断一个文件存在
- 在vb中有没有很好的第三方控件做报表的?
- 关于集合及类及odbc安装的问题
- randomize怎么使用?
- 我用excel.application对象创建excel文件且用SetParent嵌入到窗体,但运行之后再用excel打开文件就会出现在任务栏上出现excel进程,我用
Set s = CreateObject("ScriptControl")
s.Language = "VBScript"
text2.Text = s.Eval(text1.Text)
Set s = Nothing
Private Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As Long
Dim i As Double
Public Function ExecuteLine(sCode As String, Optional fCheckOnly As Boolean) As Boolean
ExecuteLine = EbExecuteLine(StrPtr(sCode), 0&, 0&, Abs(fCheckOnly)) = 0
End Function
Private Sub Command1_Click()ExecuteLine "form1.i=" + Text1.Text MsgBox Text1.Text & "=" & i
End SubPrivate Sub Form_Load()
Text1.Text = "1+2*3-4/5"
End Sub2 USE EXCEL:'引用microsoft excel 10.0 object library( OR OTHER VERSION)
' add a textbox and a commandbutton to form1
Function result(ByVal x As String)
Dim myobj As Object
Set myobj = CreateObject("excel.sheet")
Set myobj = myobj.Application.ActiveWorkbook.ActiveSheet
myobj.Range("a1").Formula = "= " & x '
result = myobj.Range("a1").Value
If Err.Number > 0 Then MsgBox Err.Description
Set myobj = Nothing
End FunctionPrivate Sub Command1_Click()
MsgBox Text1.Text & "=" & result(Text1.Text)
End SubPrivate Sub Form_Load()
Text1.Text = "1+2*3-4/5"
End Sub
注意引用microsoft script control 1.0
★★可以计算任意复杂的表达式带括号★★
代码如下:
Private Sub GetMatchExp(ByVal Exp As String, LF As String, RF As String, Lp As Integer, Rp As Integer)
'从表达式中,查找匹配起始位置
Dim strT As String
Rp = InStr(Exp, RF)
If Rp = 0 Then
Lp = 0
Else
strT = Left(Exp, Rp - 1)
strT = StrReverse(strT)
Lp = Rp - InStr(strT, LF)
End If
End SubFunction CalCulNext(ByVal Exp As String) As String
'本函数可以计算一个完整的表达式
'计算表达式中最优先的一步
Dim Lp As Integer, Rp As Integer, Mp As Integer, FirstNum As String, SecondNum As String
Call GetMatchExp(Exp, "(", ")", Lp, Rp)
If Lp * Rp = 0 Then '没有括弧
Mp = FirstOperater(Exp) '可用的操作符位置
If Mp = 0 Then '没有运算符
CalCulNext = Exp
Else
FirstNum = GetOpNum(Exp, Mp, True)
SecondNum = GetOpNum(Exp, Mp, False)
CalCulNext = CalCulNext(Mid(Exp, 1, Mp - Len(FirstNum) - 1) _
& CStr(DirectCalValue(FirstNum, SecondNum, Mid(Exp, Mp, 1))) _
& Right(Exp, Len(Exp) - (Mp + Len(SecondNum))))
End If
Else '有括弧
FirstNum = Left(Exp, Lp - 1)
SecondNum = Right(Exp, Len(Exp) - Rp)
CalCulNext = CalCulNext(FirstNum & CalCulNext(Mid(Exp, Lp + 1, Rp - Lp - 1)) & SecondNum)
End If
End Function
Private Function GetOpNum(ByVal Exp As String, ByVal OperPosition As Integer, _
ByVal AtLeft As Boolean) As String
'从表达式中取出指定操作符左或右侧的操作数
Dim I As Integer, pStep As Integer, J As Integer, Ch As String
J = OperPosition
If AtLeft Then
I = 1
pStep = -1
Else
I = Len(Exp)
pStep = 1
J = J + 1
End If
For J = J + pStep To I Step pStep
Ch = Mid(Exp, J, 1)
If Not (Ch >= "0" And Ch <= "9" Or Ch = ".") Then '
I = J - pStep
Exit For
End If
Next J
If AtLeft Then
J = OperPosition
If I > 1 Then
If Mid(Exp, I - 1, 1) = "-" Then
If I = 2 Then '不是第一个字符,检查数字前有无负号
I = 1
ElseIf I > 2 Then
Ch = Mid(Exp, I - 2, 1)
If Not (Ch >= "0" And Ch <= "9" Or Ch = ".") Then
I = I - 1
End If
End If
End If
End If
Else
J = I + 1
I = OperPosition + 1
End If
GetOpNum = Mid(Exp, I, J - I)
End Function
Private Function DirectCalValue(FirstV As String, SecondV As String, Op As String)
'用操作符计算两个操作数
Dim t1 As Double, t2 As Double
t1 = Val(FirstV): t2 = Val(SecondV)
Select Case Op
Case "+"
t1 = t1 + t2
Case "-"
t1 = t1 - t2
Case "*"
t1 = t1 * t2
Case "/"
t1 = t1 / t2
Case Else
Exit Function
End Select
DirectCalValue = CStr(t1)
End Function
Private Function FirstOperater(ByVal Exp As String) As Integer
'返回指定表达式字符串中下一步可最先计算的操作符位置
Dim P1 As Integer, P2 As Integer
P1 = InStr(Exp, "*")
P2 = InStr(Exp, "/")
GoSub SubFun
'从子程序返回,则没有 * / 符号
P1 = InStr(Exp, "+")
P2 = InStr(2, Exp, "-")
'从子程序返回,则没有 + - 符号
GoSub SubFun
FirstOperater = 0
Exit Function
SubFun:
If P1 * P2 = 0 Then
If P1 + P2 <> 0 Then
FirstOperater = P1 + P2
Exit Function
End If
Else
If P1 > P2 Then P1 = P2
FirstOperater = P1
Exit Function
End If
Return
End Function
'------------------------------------------------------------------
'******************计算字符串形式表达式结束************************
'------------------------------------------------------------------
Private Sub Command1_Click()Text2.Text = CalCulNext(Replace(Text1.Text, " ", ""))End Sub
Dim cal As New ScriptControl
cal.Language = "VBScript"
MsgBox cal.Eval("1+2*3")
End Sub