我用弄成二个文本框,一个显示答案,一个做计算
只有一个计算按钮,没有加减乘除按钮,然后在文本框1输入例如:
2389+2+8*10/2-200点击计算,就可以在文本框2输出结果
如何做到呢(效果图如下)麻烦各位大虾了,一定要是这样的计算方式哦
只有一个计算按钮,没有加减乘除按钮,然后在文本框1输入例如:
2389+2+8*10/2-200点击计算,就可以在文本框2输出结果
如何做到呢(效果图如下)麻烦各位大虾了,一定要是这样的计算方式哦
解决方案 »
- VB与485通讯问题
- 如何限制SSTAB控件的某页不响应用用户操作
- 急问:如何用sql实现两个查询结果的并集?
- 高手低手都进来回答!!
- 请教:这个sql查询怎么做???
- 关于VB的绘图闪烁
- ***请问怎样得到打开“控制面板--显示”之后得到的 “显示 属性”窗口(就是在桌面上点右键,再选“属性”打开的那个窗口)的句柄?
- 请问如何在vb的Collection对象中加入自定义的Type变量?
- 除了用媒体播放器控件回放mpeg格式的文件,还有没有其它的?
- 请问,一个TEXT框如果要做成输入IP地址装,即有"."在固定位置应该怎么做?
- 一个网上到处都是的vb作业 请教。
- 如何判断RichTextBox中是否出现了滚动条?请进
你要复杂点的话,就自己写个解析过程
Dim theI As Long'[]==========[]
'[]pop
'[]在stack中弹出字符串,stack栈深度固定为4
'[]==========[]
Private Function Pop(ByRef Stack() As String) As String
Dim i As Long
For i = 4 To 1 Step -1
If Stack(i) <> "" Then
Pop = Stack(i)
Stack(i) = ""
Exit For
End If
Next i
End Function
'[]==========[]
'[]look
'[]察看stack中栈顶字符串,stack栈深度固定为4
'[]==========[]
Private Function Look(ByRef Stack() As String) As String
Dim i As Long
For i = 4 To 1 Step -1
If Stack(i) <> "" Then
Look = Stack(i)
Exit For
End If
Next i
End Function
'[]==========[]
'[]push
'[]向stack中压入字符串,stack栈深度固定为4
'[]==========[]
Private Sub Push(ByRef Stack() As String, ByVal strs As String)
Dim i As Long
For i = 1 To 4
If Stack(i) = "" Then
Stack(i) = strs
Exit For
End If
Next i
End Sub
'[]==========[]
'[]get0
'[]表达式分析,递归调用
'[]先建立两个栈,符号栈和数据栈
'[]向符号栈内先压入一个“#”号
'[]如果遇到数字,压入数字栈
'[]如果遇到“+”、“-”号,用Look函数察看符号栈顶是什么符号
'[] 如果是“#”号,把这个“+”或者“-”压入符号栈,如果是“+”或者“-”
'[] 就把前一步的运算先算了。并且把结果压栈,新的“+”、“-”号也压栈
'[]如果遇到“*”、“/”号,从数字栈中弹出一个数,从表达式中预取一个数,进行
'[] 运算,并且把结果压栈
'[]如果遇到“(”那么就递归调用了
'[]==========[]
Private Function get0() As String
Dim p0 As String
Dim p1 As String
Dim P(1 To 4) As String
Dim d(1 To 4) As String
Dim operator As String
Dim a As String
Dim b As String
Push P, "#"
Do
start:
theI = theI + 1
If IsNumeric(Decom(theI)) Then
Push d, Decom(theI)
Else
p0 = Look(P)
p1 = Decom(theI)
Select Case p1
Case "+"
Select Case p0
Case "#"
Push P, p1
Case "+"
operator = Pop(P)
b = Pop(d)
a = Pop(d)
Push d, Str(Val(a) + Val(b))
Push P, "+"
Case "-"
operator = Pop(P)
b = Pop(d)
a = Pop(d)
Push d, Str(Val(a) - Val(b))
Push P, "+"
Case Else
End Select
Case "-"
Select Case p0
Case "#"
Push P, p1
Case "+"
operator = Pop(P)
b = Pop(d)
a = Pop(d)
Push d, Str(Val(a) + Val(b))
Push P, "-"
Case "-"
operator = Pop(P)
b = Pop(d)
a = Pop(d)
Push d, Str(Val(a) - Val(b))
Push P, "-"
Case Else
End Select
Case "("
Select Case p0
Case "+"
Push d, get0()
GoTo start
Case "-"
Push d, get0()
GoTo start
Case "#"
Push d, get0()
GoTo start
Case Else
End Select
Case ")"
operator = Pop(P)
b = Pop(d)
a = Pop(d)
If operator = "#" Or a = "" Or b = "" Then
Push P, "#"
Push d, a
Push d, b
Else
Select Case operator
Case "+"
Push d, Str(Val(a) + Val(b))
Case "-"
Push d, Str(Val(a) - Val(b))
Case "*"
Push d, Str(Val(a) * Val(b))
Case "/"
Push d, Str(Val(a) / Val(b))
Case Else
End Select
End If
Case ""
operator = Pop(P)
b = Pop(d)
a = Pop(d)
If operator = "#" Or a = "" Or b = "" Then
Push P, "#"
Push d, a
Push d, b
Else
Select Case operator
Case "+"
Push d, Str(Val(a) + Val(b))
Case "-"
Push d, Str(Val(a) - Val(b))
Case "*"
Push d, Str(Val(a) * Val(b))
Case "/"
Push d, Str(Val(a) / Val(b))
Case Else
End Select
End If
Case "*"
If IsNumeric(Decom(theI + 1)) Then
theI = theI + 1
b = Decom(theI)
a = Pop(d)
Push d, Str(Val(a) * Val(b))
End If
If Decom(theI + 1) = "(" Then
a = Pop(d)
theI = theI + 1
Push d, Str(Val(a) * Val(get0()))
GoTo start
End If
Case "/"
If IsNumeric(Decom(theI + 1)) Then
theI = theI + 1
b = Decom(theI)
a = Pop(d)
Push d, Str(Val(a) / Val(b))
End If
If Decom(theI + 1) = "(" Then
a = Pop(d)
theI = theI + 1
Push d, Str(Val(a) / Val(get0()))
GoTo start
End If
Case ","
operator = Pop(P)
b = Pop(d)
a = Pop(d)
If operator = "#" Or a = "" Or b = "" Then
Push P, "#"
Push d, a
Push d, b
Else
Select Case operator
Case "+"
Push d, Str(Val(a) + Val(b))
Case "-"
Push d, Str(Val(a) - Val(b))
Case "*"
Push d, Str(Val(a) * Val(b))
Case "/"
Push d, Str(Val(a) / Val(b))
Case Else
End Select
End If
Exit Do
Case Else
End Select
End If
Loop Until Decom(theI) = ")" Or Decom(theI) = ""
get0 = Pop(d)
End Function
'[]==========[]
'[]getexp
'[]表达式求值
'[]==========[]
Private Function GetExp(ByVal strCodeLine As String) As String
Dim s As String
Dim i As Long
Seperate strCodeLine
i = 0
Do
i = i + 1
If Decom(i) = "(" Then
theI = i - 1
Decom(i) = get0
Exit Do
End If
Loop While Decom(i) <> ""
i = i + 1
Do
Decom(i) = ""
i = i + 1
Loop While Decom(i) <> ""
i = 0
Do
i = i + 1
s = s & Decom(i) & " "
Loop While Decom(i) <> ""
GetExp = s
End Function'[]==========[]
'[]seperate
'[]分离一个字符串,存入数组decom中
'[]==========[]
Public Sub Seperate(ByVal strCodeLine As String)
Dim i As Long
Dim leng As Long
Dim c As String
Dim j As Long
Dim n As Long
leng = Len(strCodeLine)
n = 0
For i = 1 To 256
Decom(i) = ""
Next i
i = 0
Do
i = i + 1
c = Mid(strCodeLine, i, 1)
If c = " " Then
Do
i = i + 1
c = Mid(strCodeLine, i, 1)
Loop While c = " " And i <= leng
i = i - 1
ElseIf c = Chr(vbKeyTab) Then
Do
i = i + 1
c = Mid(strCodeLine, i, 1)
Loop While c = Chr(vbKeyTab) And i <= leng
i = i - 1
ElseIf c = "'" Then
j = i
Do
i = i + 1
c = Mid(strCodeLine, i, 1)
Loop While c <> "'" And i <= leng
n = n + 1
Decom(n) = Trim(Mid(strCodeLine, j, i - j + 1))
ElseIf c = "<" Then
n = n + 1
Decom(n) = "<"
ElseIf c = ">" Then
n = n + 1
Decom(n) = ">"
ElseIf c = "[" Then
n = n + 1
Decom(n) = "["
ElseIf c = "]" Then
n = n + 1
Decom(n) = "]"
ElseIf c = "{" Then
n = n + 1
Decom(n) = "{"
ElseIf c = "}" Then
n = n + 1
Decom(n) = "}"
ElseIf c = "(" Then
n = n + 1
Decom(n) = "("
ElseIf c = ")" Then
n = n + 1
Decom(n) = ")"
ElseIf c = "+" Then
n = n + 1
Decom(n) = "+"
ElseIf c = "-" Then
n = n + 1
Decom(n) = "-"
ElseIf c = "*" Then
n = n + 1
Decom(n) = "*"
ElseIf c = "/" Then
n = n + 1
Decom(n) = "/"
ElseIf c = "=" Then
n = n + 1
Decom(n) = "="
ElseIf c = "," Then
n = n + 1
Decom(n) = ","
ElseIf c = ":" Then
n = n + 1
Decom(n) = ":"
Else
j = i
Do
i = i + 1
c = Mid(strCodeLine, i, 1)
Loop While c <> " " And c <> Chr(vbKeyTab) And c <> "'" And c <> "{" And c <> "}" _
And c <> "[" And c <> "]" And c <> "<" And c <> ">" _
And c <> "(" And c <> ")" And c <> "+" And c <> "-" _
And c <> "*" And c <> "/" And c <> "=" And c <> "," _
And c <> ":" And i <= leng
n = n + 1
Decom(n) = Trim(Mid(strCodeLine, j, i - j))
i = i - 1
End If
Loop While i <= leng
End SubPrivate Sub Command1_Click()
Dim s As String
s = "(" & Text1.Text & ")"
Text2.Text = GetExp(s)
End Sub测试通过。
If KeyAscii = 13 Then Command1_Click
End SubPrivate Sub Command1_Click()
Set sc = CreateObject("ScriptControl")
sc.Language = "VBScript"
Text2.Text = sc.Eval(Text1.Text)
Set sc = Nothing
End Sub
Text1.Text = "2389+2+8*10/2-200"
End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then Command1_Click
End SubPrivate Sub Command1_Click()
Set sc = CreateObject("ScriptControl")
sc.Language = "VBScript"
Text2.Text = "结果为:" & sc.Eval(Text1.Text)
Set sc = Nothing
End Sub
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 result
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then Command1_Click
End SubPrivate Sub Command1_Click()
Call calc(Text1)
End SubSub calc(ByVal textstr As String)
ExecuteLine "Dim X As Long, Y As Long"
ExecuteLine "textstr= " & textstr
ExecuteLine "clipboard.settext textstr"
result = Clipboard.GetText
Text2.Text = "结果为:" & result
Set result = Nothing
End SubPublic Function ExecuteLine(sCode As String, Optional fCheckOnly As Boolean) As Boolean
ExecuteLine = EbExecuteLine(StrPtr(sCode), 0&, 0&, Abs(fCheckOnly)) = 0
End Function
http://topic.csdn.net/u/20070828/02/48a980b0-2a22-4eb5-b34a-f0ba55bb2250.html我没有调用任何部件,用栈的数据结构即可完成。写成的成品在这里,vb绿色软件,有兴趣可以去下个,没毒的,在华军和硅谷动力都有:
http://download.enet.com.cn/html/033432007101201.html
Option ExplicitPrivate Sub Command1_Click()
Text2.Text = ScriptControl1.Eval(Trim(Text1.Text))
End SubPrivate Sub Form_Load()
ScriptControl1.AddCode _
"Function Max(v1, v2)" & vbCrLf & _
" If v1>=v2 Then" & vbCrLf & _
" Max = V1" & vbCrLf & _
" Else" & vbCrLf & _
" Max = V2" & vbCrLf & _
" End If" & vbCrLf & _
"End Function"
End Sub