- 积分
- 4924
- 回帖
- 0
- 西莫币
-
- 贡献
-
- 威望
-
- 存款
-
- 阅读权限
- 120
- 最后登录
- 1970-1-1
该用户从未签到
|
发表于 2012-3-27 21:13
|
显示全部楼层
来自: 中国四川成都
用vb实现表达式求值的算符优先算法
这是我做的用vb实现表达式求值的算符优先算法的程序,很简单,帖在这,仅供vb新手学习,高手见笑。
'用vb实现表达式求值的算符优先算法
Option Explicit
Dim op(6) As String 'op为运算符集合
Dim optr(20) As String 'optr是运算符栈,我用的数组实现的
Dim opnd(20) As Integer
'opnd是运算数栈,其实本来用的动态数组,后来我嫌麻烦就用的静态的
'运算数和运算符都不能超过20个,反正主要是实现算法
Public optrlen As Integer
Public opndlen As Integer
Public Function inop(c As String, op) As Boolean '判断输入的字符是否为运算符的函数
Dim i As Integer
inop = False
For i = 0 To 6
If op(i) = c Then
inop = True
Exit For
Exit Function
End If
Next i
End Function
Public Sub pushopnd(c As String) '入运算数栈
opnd(opndlen) = c
opndlen = opndlen + 1
End Sub
Public Sub pushoptr(c As String) '入运算符栈
optr(optrlen) = c
optrlen = optrlen + 1
End Sub
Public Function popoptr() As String '出运算符栈
popoptr = optr(optrlen - 1)
optrlen = optrlen - 1
End Function
Public Function popopnd() As String '出运算数栈
popopnd = opnd(opndlen - 1)
opndlen = opndlen - 1
End Function
Public Function gettopoptr() As String '取运算符的栈顶元素
gettopoptr = optr(optrlen - 1)
End Function
Public Function operate(a As Integer, theta As String, b As Integer) '运算函数
If theta = "+" Then
operate = b + a
ElseIf theta = "*" Then
operate = b * a
ElseIf theta = "-" Then
operate = a - b
ElseIf theta = "/" Then
operate = a / b
End If
End Function
Public Function precede(optrtop As String, c As String) As String '比较优先级的函数,相当于查咱数据结构书上的那个优先级表
Select Case optrtop
Case "+"
Select Case c
Case "+", "-", ")", "#"
precede = ">"
Case "*", "/", "("
precede = "<"
Case Else
MsgBox ("新读入的字符不是运算符!")
End Select
Case "-"
Select Case c
Case "+", "-", ")", "#"
precede = ">"
Case "*", "/", "("
precede = "<"
Case Else
MsgBox ("新读入的字符不是运算符!")
End Select
Case "*"
Select Case c
Case "+", "-", ")", "#", "*", "/"
precede = ">"
Case "("
precede = "<"
Case Else
MsgBox ("新读入的字符不是运算符!")
End Select
Case "/"
Select Case c
Case "+", "-", ")", "#", "*", "/"
precede = ">"
Case "("
precede = "<"
Case Else
MsgBox ("新读入的字符不是运算符!")
End Select
Case "("
Select Case c
Case "+", "-", "(", "*", "/"
precede = "<"
Case ")"
precede = "="
Case Else
MsgBox ("新读入的字符不是运算符!")
End Select
Case ")"
Select Case c
Case "+", "-", ")", "#", "*", "/"
precede = ">"
Case "("
MsgBox ("不允许出现‘)’,‘(’的情况!")
Case Else
MsgBox ("新读入的字符不是运算符!")
End Select
Case "#"
Select Case c
Case "+", "-", "(", "*", "/"
precede = "<"
Case "#"
precede = "="
Case ")"
MsgBox ("不允许出现‘#’,‘)’的情况!")
Case Else
MsgBox ("新读入的字符不是运算符!")
End Select
Case Else
MsgBox ("运算符栈的栈顶字符不是运算符!")
End Select
End Function
Private Sub Command1_Click()
Dim getchar As String
Dim popoptrchar As String
Dim popopndchara As Integer
Dim popopndcharb As Integer
Dim operatechar As Integer
Dim jieguo As String
Do While gettopoptr <> "#" '运算,直到运算符栈是“#”时
popopndcharb = popopnd()
popopndchara = popopnd()
popoptrchar = popoptr
operatechar = operate(popopndchara, popoptrchar, popopndcharb)
pushopnd (operatechar)
Loop
jieguo = popopnd()
Text2.Text = jieguo '显示结果
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Load()
optrlen = 1
opndlen = 0
op(0) = "+" '初始化运算符集合
op(1) = "-"
op(2) = "*"
op(3) = "/"
op(4) = "("
op(5) = ")"
op(6) = "#"
optr(0) = "#"
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
'输入的字符相当与c=getchar
'keypress事件正好是输入一个字符执行一次,就省着做循环了,这是vb的优点也正是我用vb的初衷
Dim getchar As String
Dim popoptrchar As String
Dim popopndchara As Integer
Dim popopndcharb As Integer
Dim operatechar As Integer
getchar = Chr(KeyAscii)
If (Not inop(getchar, op)) Then
pushopnd (getchar)
Else
Select Case precede(gettopoptr(), getchar)
Case "<"
pushoptr (getchar)
Case "="
popoptrchar = popoptr()
Case ">"
popopndcharb = popopnd()
popopndchara = popopnd()
popoptrchar = popoptr
operatechar = operate(popopndchara, popoptrchar, popopndcharb)
pushopnd (operatechar)
If getchar = ")" Then
popoptrchar = popoptr
End If
If getchar <> "#" And getchar <> ")" Then pushoptr (getchar)
Case Else
MsgBox ("嘿嘿,对不起,出现了我意想不到的错误。")
End Select
End If
End Sub
注:此程序在win98,vb6下调试通过。 |
|