-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCalcFunc.bas
More file actions
204 lines (201 loc) · 5.17 KB
/
CalcFunc.bas
File metadata and controls
204 lines (201 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
Attribute VB_Name = "CalcFunc"
Public opNum As New StackClass
Public opChar As New StackClass
Public Function CalcString(ByVal strIn As String) As String
Dim sTxt As String
Dim strNumFix As String
Dim curChar As String
Dim i As Long
Dim signCount As Long
Dim ops1 As String, ops2 As String, opC As String
'初始化堆栈
opNum.Clear
opChar.Clear
'堆栈初始化结束
sTxt = strIn
For i = 1 To Len(sTxt)
curChar = Mid(sTxt, i, 1)
If IsSymbol(curChar) = True Then
'看看数字预备区有没有
If strNumFix <> "" Then
opNum.Push strNumFix
strNumFix = ""
End If
redo:
If IsHigh(curChar, opChar.Peek) = 1 Then 'if new come char is higher then push it to stack
opChar.Push curChar '如果等级高的控制符,则进入
signCount = signCount + 1
ElseIf IsHigh(curChar, opChar.Peek) = 0 Then
If curChar = "#" And opChar.Peek = "#" Then
opChar.Pop
CalcString = opNum.Pop
Exit Function
End If
ElseIf IsHigh(curChar, opChar.Peek) = -1 Then 'if low then ready to calculate
'判断是不是第一个符号
If signCount = 1 Then '这个符号是刚刚输入#后的那个,无论如何入栈
opChar.Push curChar
signCount = signCount + 1
GoTo nextone
End If
ops2 = opNum.Pop
ops1 = opNum.Pop
opC = opChar.Pop
opNum.Push CStr(Calc(ops1, ops2, opC))
If curChar = ")" And opChar.Peek = "(" Then
opChar.Pop '如果操作数是),就把(弹出来
GoTo moveon
End If
GoTo redo
moveon:
End If
Else '非符号
strNumFix = strNumFix & curChar
End If
nextone:
Next i
End Function
Public Function Calc(ByVal op1 As String, ByVal op2 As String, ByVal options As String) As Double
On Error Resume Next
Calc = 0
Select Case options
Case "+"
Calc = CDbl(op1) + CDbl(op2)
Case "-"
Calc = CDbl(op1) - CDbl(op2)
Case "*"
Calc = CDbl(op1) * CDbl(op2)
Case "/"
Calc = CDbl(op1) / CDbl(op2)
End Select
End Function
Public Function IsHigh(ByVal sNew As String, ByVal sOld As String) As Integer
'1大于,-1小于,0等于
Select Case sNew
Case "+"
Select Case sOld
Case "("
IsHigh = 1
Exit Function
Case "#"
IsHigh = 1
Exit Function
Case Else
IsHigh = -1
Exit Function
End Select
Case "-"
Select Case sOld
Case "("
IsHigh = 1
Exit Function
Case "#"
IsHigh = 1
Exit Function
Case Else
IsHigh = -1
Exit Function
End Select
Case "*"
Select Case sOld
Case "("
IsHigh = 1
Exit Function
Case "#"
IsHigh = 1
Exit Function
Case "+"
IsHigh = 1
Exit Function
Case "-"
IsHigh = 1
Exit Function
Case Else
IsHigh = -1
Exit Function
End Select
Case "/"
Select Case sOld
Case "("
IsHigh = 1
Exit Function
Case "#"
IsHigh = 1
Exit Function
Case "+"
IsHigh = 1
Exit Function
Case "-"
IsHigh = 1
Exit Function
Case Else
IsHigh = -1
Exit Function
End Select
Case "("
Select Case sOld
Case "+"
IsHigh = 1
Exit Function
Case "-"
IsHigh = 1
Exit Function
Case "*"
IsHigh = 1
Exit Function
Case "/"
IsHigh = 1
Exit Function
Case "("
IsHigh = 1
Exit Function
Case Else
IsHigh = -1
Exit Function
End Select
Case ")"
IsHigh = -1
Exit Function
Case ""
IsHigh = -1
Exit Function
Case "#"
Select Case sOld
Case "#"
IsHigh = 0
Exit Function
Case ""
IsHigh = 1
Exit Function
Case "+"
IsHigh = -1
Exit Function
Case "-"
IsHigh = -1
Exit Function
Case "*"
IsHigh = -1
Exit Function
Case "/"
IsHigh = -1
Exit Function
Case ")"
IsHigh = -1
Exit Function
End Select
End Select
End Function
Public Function IsSymbol(ByVal strS As String) As Boolean
IsSymbol = True
Select Case strS
Case "+"
Case "-"
Case "*"
Case "/"
Case "("
Case ")"
Case "#"
Case Else
IsSymbol = False
End Select
End Function