这个抽象算法早已为人所知,只不过在VB的公开文档中鲜见示例代码。于是,为了提高自己的程序设计水平,锻炼自己的能力,我写了如下代码。
【VB代码版权所有,允许转载修改用作学习目的,转载必须注明来源】
【求大家看完后留言啊!】
堆栈类的实现:
1 Private Stack() As String 2 Private itemCount As Long 3 Private Sub Class_Initialize() 4 ReDim Stack(0) 5 Stack(0) = "#" 6 End Sub 7 Public Sub Push(ByVal inString As String) 8 ReDim Preserve Stack(itemCount + 1) 9 Stack(itemCount + 1) = inString10 itemCount = itemCount + 111 End Sub12 13 Public Function Pop() As String14 If itemCount >= 1 Then15 Pop = Stack(itemCount)16 ReDim Preserve Stack(itemCount - 1)17 itemCount = itemCount - 118 Else19 Pop = ""20 End If21 End Function22 Public Function Peek() As String23 If itemCount = 0 Then24 Peek = ""25 Exit Function26 End If27 Peek = Stack(itemCount)28 End Function29 30 Sub Clear()31 itemCount = 032 ReDim Stack(itemCount)33 Stack(itemCount) = "#"34 End Sub35 36 Public Function Count()37 Count = itemCount38 End Function39 Public Function ViewStack() As String40 Dim kOut As String41 Dim i As Long42 If itemCount = 0 Then ViewStack = "": Exit Function43 For i = 1 To itemCount44 kOut = kOut & Format(i, "00") & " " & Stack(i) & vbCrLf45 Next i46 ViewStack = kOut47 End Function
计算模块中的代码:
1 Public opNum As New StackClass 2 Public opChar As New StackClass 3 Public Function CalcString(ByVal strIn As String) As String 4 Dim sTxt As String 5 Dim strNumFix As String 6 Dim curChar As String 7 Dim i As Long 8 Dim signCount As Long 9 Dim ops1 As String, ops2 As String, opC As String 10 '初始化堆栈 11 opNum.Clear 12 opChar.Clear 13 '堆栈初始化结束 14 sTxt = strIn 15 For i = 1 To Len(sTxt) 16 curChar = Mid(sTxt, i, 1) 17 If IsSymbol(curChar) = True Then 18 '看看数字预备区有没有 19 If strNumFix <> "" Then 20 opNum.Push strNumFix 21 strNumFix = "" 22 End If 23 redo: 24 If IsHigh(curChar, opChar.Peek) = 1 Then 'if new come char is higher then push it to stack 25 opChar.Push curChar '如果等级高的控制符,则进入 26 signCount = signCount + 1 27 ElseIf IsHigh(curChar, opChar.Peek) = 0 Then 28 If curChar = "#" And opChar.Peek = "#" Then 29 opChar.Pop 30 CalcString = opNum.Pop 31 Exit Function 32 End If 33 ElseIf IsHigh(curChar, opChar.Peek) = -1 Then 'if low then ready to calculate 34 '判断是不是第一个符号 35 If signCount = 1 Then '这个符号是刚刚输入#后的那个,无论如何入栈 36 opChar.Push curChar 37 signCount = signCount + 1 38 GoTo nextone 39 End If 40 ops2 = opNum.Pop 41 ops1 = opNum.Pop 42 opC = opChar.Pop 43 opNum.Push CStr(Calc(ops1, ops2, opC)) 44 If curChar = ")" And opChar.Peek = "(" Then 45 opChar.Pop '如果操作数是),就把(弹出来 46 GoTo moveon 47 End If 48 GoTo redo 49 moveon: 50 End If 51 Else '非符号 52 strNumFix = strNumFix & curChar 53 End If 54 nextone: 55 Next i 56 End Function 57 58 Public Function Calc(ByVal op1 As String, ByVal op2 As String, ByVal options As String) As Double 59 On Error Resume Next 60 Calc = 0 61 Select Case options 62 Case "+" 63 Calc = CDbl(op1) + CDbl(op2) 64 Case "-" 65 Calc = CDbl(op1) - CDbl(op2) 66 Case "*" 67 Calc = CDbl(op1) * CDbl(op2) 68 Case "/" 69 Calc = CDbl(op1) / CDbl(op2) 70 End Select 71 End Function 72 73 Public Function IsHigh(ByVal sNew As String, ByVal sOld As String) As Integer 74 '1大于,-1小于,0等于 75 Select Case sNew 76 Case "+" 77 Select Case sOld 78 Case "(" 79 IsHigh = 1 80 Exit Function 81 Case "#" 82 IsHigh = 1 83 Exit Function 84 Case Else 85 IsHigh = -1 86 Exit Function 87 End Select 88 Case "-" 89 Select Case sOld 90 Case "(" 91 IsHigh = 1 92 Exit Function 93 Case "#" 94 IsHigh = 1 95 Exit Function 96 Case Else 97 IsHigh = -1 98 Exit Function 99 End Select100 Case "*"101 Select Case sOld102 Case "("103 IsHigh = 1104 Exit Function105 Case "#"106 IsHigh = 1107 Exit Function108 Case "+"109 IsHigh = 1110 Exit Function111 Case "-"112 IsHigh = 1113 Exit Function114 Case Else115 IsHigh = -1116 Exit Function117 End Select118 Case "/"119 Select Case sOld120 Case "("121 IsHigh = 1122 Exit Function123 Case "#"124 IsHigh = 1125 Exit Function126 Case "+"127 IsHigh = 1128 Exit Function129 Case "-"130 IsHigh = 1131 Exit Function132 Case Else133 IsHigh = -1134 Exit Function135 End Select136 Case "("137 Select Case sOld138 Case "+"139 IsHigh = 1140 Exit Function141 Case "-"142 IsHigh = 1143 Exit Function144 Case "*"145 IsHigh = 1146 Exit Function147 Case "/"148 IsHigh = 1149 Exit Function150 Case "("151 IsHigh = 1152 Exit Function153 Case Else154 IsHigh = -1155 Exit Function156 End Select157 Case ")"158 IsHigh = -1159 Exit Function160 Case ""161 IsHigh = -1162 Exit Function163 Case "#"164 Select Case sOld165 Case "#"166 IsHigh = 0167 Exit Function168 Case ""169 IsHigh = 1170 Exit Function171 Case "+"172 IsHigh = -1173 Exit Function174 Case "-"175 IsHigh = -1176 Exit Function177 Case "*"178 IsHigh = -1179 Exit Function180 Case "/"181 IsHigh = -1182 Exit Function183 Case ")"184 IsHigh = -1185 Exit Function186 End Select187 End Select188 End Function189 190 Public Function IsSymbol(ByVal strS As String) As Boolean191 IsSymbol = True192 Select Case strS193 Case "+"194 Case "-"195 Case "*"196 Case "/"197 Case "("198 Case ")"199 Case "#"200 Case Else201 IsSymbol = False202 End Select203 End Function