This repository has been archived by the owner on Jan 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mdlMath.bas
351 lines (338 loc) · 14.9 KB
/
mdlMath.bas
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
Attribute VB_Name = "mdlMath"
Option Explicit
Option Compare Text
Dim n As Long, MathSkipErrors As Boolean, blnError As Boolean
Public Const Pi = 3.14159265358979
Const Plus = "+"
Const Minus = "-"
Const Mult = "*"
Const Div = "/"
Const Extent = "^"
Const iSin = "sin("
Const iCos = "cos("
Const iTan = "tan("
Const iCtn = "ctn("
Const iSqr = "sqr("
Const iRnd = "rnd"
Const iAbs = "abs("
Const iPi = "pi"
' Ëîãè÷åñêèå îïåðàöèè
Public Const loOR = "OR", loAND = "AND", loNOT = "NOT", loEQV = "=", loNOTEQV = "!="
Public Const GetLeft = 1, GetRight = 2
Function Calculate(ByVal strExpression As String, Optional SkipErrors As Boolean) As Variant ', VarsNamesArr() As String, VarsValsArr() As String) As Variant '----------------------- 1 --------------------------
Dim s As String, BracketStart As Long, BracketEnd As Long, strTmpFunc As String, _
Result As String, CustomFunction As String, blnExtent As Boolean, blnSaveBracket As Boolean, OriginalExpression As String
MathSkipErrors = SkipErrors Or Settings.bSkipErrors
If MathSkipErrors Then On Error GoTo lSkip Else On Error GoTo ErrHandler
AddStage "Èíòåðïðåòàöèÿ ìàò. âûðàæåíèÿ - ãëàâíûé ìîäóëü"
OriginalExpression = strExpression
'ConvertXFactor strExpression
UpdateExpression strExpression
If InStr(1, strExpression, "(") > 0 Then
If GetCount(ReplaceInQuotesSTR(strExpression, "(", ""), "(") <> GetCount(ReplaceInQuotesSTR(strExpression, ")", ""), ")") Then Err.Raise ErrNum, , "Èìåþòñÿ íåçàêðûòûå ñêîáêè"
End If
If InStr(1, strExpression, "|") Then ConvertModule strExpression
If blnError Then GoTo lEnd
Do While IsNumeric(strExpression) = False Or InStr(1, strExpression, "(") > 0
AddStage "Èíòåðïðåòàöèÿ ìàò. âûðàæåíèÿ - ãëàâíûé ìîäóëü"
blnExtent = False
blnSaveBracket = False
BracketStart = InStrRev(strExpression, "(") ' <-------------------------
If BracketStart = 0 Then 'ïîñëåäíåå äåéñòâèå
strExpression = LiteCalculate(strExpression)
Exit Do
End If
BracketEnd = InStr(BracketStart, strExpression, ")") ' ---------------------------->
strTmpFunc = Mid$(strExpression, BracketStart + 1, BracketEnd - (BracketStart + 1))
If Mid$(strExpression, BracketEnd + 1, 1) = Extent Then
If IsNumeric(strTmpFunc) Then
BracketEnd = BracketEnd + Len(GetNum(strExpression, BracketEnd + 1, GetRight))
strTmpFunc = Mid$(strExpression, BracketStart, BracketEnd - BracketStart + 2)
blnExtent = True
Else
blnSaveBracket = True
End If
End If
Result = LiteCalculate(strTmpFunc)
If blnError Then Exit Do
If BracketStart > 3 Then CustomFunction = Mid$(strExpression, BracketStart - 3, 4) Else CustomFunction = ""
Select Case CustomFunction
Case iSin
Result = Any2Str(sIn(Result))
ReplaceFunc strExpression, BracketStart, BracketEnd, Result
Case iCos
Result = Any2Str(Cos(Result))
ReplaceFunc strExpression, BracketStart, BracketEnd, Result
Case iTan
Result = Any2Str(Tan(Result))
ReplaceFunc strExpression, BracketStart, BracketEnd, Result
Case iSqr
Result = Any2Str(Sqr(Result))
ReplaceFunc strExpression, BracketStart, BracketEnd, Result
Case iAbs
Result = Any2Str(Abs(Result))
ReplaceFunc strExpression, BracketStart, BracketEnd, Result
Case Else
If blnSaveBracket Then
strExpression = Replace(strExpression, Mid$(strExpression, BracketStart, BracketEnd - BracketStart + 1), "(" & Result & ")")
ElseIf blnExtent Then
strExpression = Replace(strExpression, Mid$(strExpression, BracketStart, BracketEnd - BracketStart + 2), Result)
Else
strExpression = Replace(strExpression, Mid$(strExpression, BracketStart, BracketEnd - BracketStart + 1), Result)
End If
End Select
DoEvents
If blnError Or (Not blnExecuting) Then Exit Do
UpdateExpression strExpression
Loop
lEnd:
If Not IsNumeric(strExpression) And MathSkipErrors Then strExpression = OriginalExpression
Calculate = strExpression
blnError = False
Exit Function
lSkip:
ADL "íåâåðíîå ìàò. âûðàæåíèå"
Calculate = OriginalExpression
blnError = False
Exit Function
ErrHandler:
blnError = False
GlobalError.Description = "Îøèáêà ïðè ïîïûòêå íàéòè çíà÷åíèå ìàò. âûðàæåíèÿ"
GlobalError.Expression = strExpression
Call GlobalErrorHandler
End Function
'Sub ConvertXFactor(strExpression As String) 'ìíîæèòåëü èêñà.. 2õ, 3õ, 100õ è òä. Factor - ìíîæèòåëü
' Dim n As Long, Factor As Variant, StartPos As Long
' n = InStr(1, strExpression, "x")
' If n < 2 Then Exit Sub
' Do While IsNumeric(Mid$(strExpression, n - 1, 1))
' GetFirstNum Factor, StartPos, strExpression, n
' strExpression = Replace(strExpression, Mid$(strExpression, StartPos, (n - StartPos) + 1), Any2Str(Factor) & "*x")
' n = InStr(n + 1, strExpression, "x")
' Loop
'End Sub
Sub ConvertModule(strExpression As String)
Dim ModuleStart As Long, ModuleEnd As Long, i As Long
If MathSkipErrors Then
On Error GoTo lSkip
Else
On Error GoTo ErrHandler
End If
'AddStage "Êîíâåðòàöèÿ ìîäóëÿ â ïðèåìëèìûé âèä"
Do While InStr(ModuleEnd + 1, strExpression, "|")
ModuleStart = InStr(ModuleEnd + 1, strExpression, "|")
ModuleEnd = InStr(ModuleStart + 1, strExpression, "|")
strExpression = Mid$(strExpression, 1, ModuleStart - 1) & Replace(strExpression, "|", iAbs, ModuleStart, 1)
strExpression = Mid$(strExpression, 1, ModuleStart + 3 + (ModuleEnd - ModuleStart) - 1) & Replace(strExpression, "|", ")", 4 + ModuleEnd - 1, 1)
Loop
Exit Sub
lSkip:
ADL "íåâîçìîæíî êîíâåðòèðîâàòü ìîäóëè"
blnError = True
Exit Sub
ErrHandler:
blnError = True
GlobalError.Description = "Îøèáêà ïðè ïîïûòêå ïðîèçâåñòè êîíâåðòàöèþ ìîäóëÿ ÷èñëà"
GlobalError.Expression = strExpression
Call GlobalErrorHandler
End Sub
Sub ReplaceFunc(ByRef strExpression As String, ByVal BracketStart As Long, ByVal BracketEnd As Long, ByVal Result As String)
If MathSkipErrors Then
On Error GoTo lSkip
Else
On Error GoTo ErrHandler
End If
strExpression = Replace(strExpression, Mid$(strExpression, BracketStart - 3, BracketEnd - BracketStart + 1 + 3), Result)
Exit Sub
lSkip:
ADL "íåâîçìîæíî çàìåíèòü ôóíêöèþ ÷èñëîì"
blnError = True
Exit Sub
ErrHandler:
blnError = True
GlobalError.Description = "Îøèáêà ïðè ïîïûòêå çàìåíèòü ôóíêöèþ ÷èñëîì"
GlobalError.Expression = strExpression
Call GlobalErrorHandler
End Sub
Function LiteCalculate(strExpression As String) As String '--------------------- 2 -------------------
Dim s As String, n As Long, Pos1 As Long, Pos2 As Long, Num1 As Double, Num2 As Double, Result As String, Operation As String, i As Long
If MathSkipErrors Then
On Error GoTo lSkip
Else
On Error GoTo ErrHandler
End If
AddStage "Èíòåðïðåòàöèÿ ìàò. âûðàæåíèÿ - èñïîëíèòåëüíûé ìîäóëü"
If IsNumeric(strExpression) Then GoTo iEnd
n = 2
Do While n > 1
'level 0 - ^
Operation = Extent
n = InStr(1, strExpression, Operation) ' ^
If n > 1 Then GoTo Calculate
'level 1 - ïîèñê * è /
For n = 2 To Len(strExpression)
s = Mid$(strExpression, n, 1)
Operation = Mult
If s = Operation Then GoTo Calculate
Operation = Div
If s = Operation Then GoTo Calculate
Next n
'level 2 - ïîèñê + è -
For n = 2 To Len(strExpression)
s = Mid$(strExpression, n, 1)
If LCase(Mid$(strExpression, n - 1, 1)) <> "e" And IsNumeric(Mid(strExpression, n - 1, 1)) Then 'ïðîâåðêà íà äëèííîå ÷èñëî è íà ñêîáêó
Operation = Plus
If s = Operation Then GoTo Calculate
Operation = Minus
If s = Operation Then GoTo Calculate
End If
Next n
Exit Do
Calculate:
AddStage "Èíòåðïðåòàöèÿ ìàò. âûðàæåíèÿ - èñïîëíèòåëüíûé ìîäóëü"
If n > 1 Then
Num1 = GetNum(strExpression, n, 1, Operation, Pos1) ' ïîëó÷àåì ÷èñëî è ïîçèöèþ íà÷àëà ÷èñëà
Num2 = GetNum(strExpression, n, 2, Operation, Pos2) ' ïîëó÷àåì ÷èñëî è ïîçèöèþ âòîðîãî ÷èñëà
Select Case Operation
Case Extent ' ^
Result = Num1 ^ Num2
Case Mult ' *
Result = Num1 * Num2
Case Div ' /
Result = Num1 / Num2
Case Plus ' +
Result = Num1 + Num2
Case Minus ' -
Result = Num1 - Num2
End Select
Result = Any2Str(Result)
If Sgn(Result) = 1 And Pos1 > 1 Then
strExpression = AccurateReplace(strExpression, "+" & Result, Pos1, Pos2) 'Replace(strExpression, Mid$(strExpression, Pos1, Pos2 - Pos1), "+" & Result) '+
Else
strExpression = AccurateReplace(strExpression, Result, Pos1, Pos2) 'Replace(strExpression, Mid$(strExpression, Pos1, Pos2 - Pos1), Result) '-
End If
End If
UpdateExpression strExpression
If IsNumeric(strExpression) Then Exit Do
Loop
iEnd:
LiteCalculate = Any2Str(strExpression)
Exit Function
lSkip:
ADL "âû÷èñëåíèÿ âåðíóëè îøèáêó"
blnError = True
Exit Function
ErrHandler:
blnError = True
GlobalError.Description = "Îøèáêà ïðè ïîïûòêå ïðîèçâåñòè âû÷èñëåíèå ìàò. ôóíêöèè"
GlobalError.Expression = strExpression
Call GlobalErrorHandler
End Function
Sub ConvertBracket(strExpression As String)
If InStr(1, strExpression, "(") = 0 Or Len(strExpression) < 3 Then Exit Sub
If IsNumeric(Mid(strExpression, 2, Len(strExpression) - 2)) Then strExpression = Mid$(strExpression, 2, Len(strExpression) - 2) 'èçáàâëÿåìñÿ îò ñêîáîê, êîòîðûå áûëè äàíû âíà÷àëå
End Sub
Sub UpdateExpression(ByRef strExpression As String)
strExpression = LCase(strExpression)
strExpression = Replace(strExpression, " ", "")
strExpression = Replace(strExpression, ".", ",")
strExpression = Replace(strExpression, "\", Div)
strExpression = Replace(strExpression, ":", Div)
strExpression = Replace(strExpression, "Ìîäóëü(", iAbs)
strExpression = Replace(strExpression, "Êîñèíóñ(", iCos)
strExpression = Replace(strExpression, "Ñèí(", iSin)
strExpression = Replace(strExpression, "Ñèíóñ(", iSin)
strExpression = Replace(strExpression, "Êîñ(", iCos)
strExpression = Replace(strExpression, "Tg(", iTan)
strExpression = Replace(strExpression, "Òàí(", iTan)
strExpression = Replace(strExpression, "Òàíãåíñ(", iTan)
strExpression = Replace(strExpression, "CTg(", iCtn)
strExpression = Replace(strExpression, "CTan(", iCtn)
strExpression = Replace(strExpression, "CoTg(", iCtn)
strExpression = Replace(strExpression, "CoTan(", iCtn)
strExpression = Replace(strExpression, "Êîòàíãåíñ(", iCtn)
strExpression = Replace(strExpression, "Êîðåíü(", iSqr)
strExpression = Replace(strExpression, "Sqrt(", iSqr)
strExpression = Replace(strExpression, "Ïè", iPi)
strExpression = Replace(strExpression, iPi, Pi)
If InStr(1, strExpression, "Rnd") Then strExpression = Replace(strExpression, "Rnd", Rnd) 'Random
strExpression = Replace(strExpression, "++", "+")
strExpression = Replace(strExpression, "+-", "-")
strExpression = Replace(strExpression, "-+", "-")
strExpression = Replace(strExpression, "--", "+")
strExpression = Replace(strExpression, "/+", "/")
strExpression = Replace(strExpression, "+/", "/")
strExpression = Replace(strExpression, "+:", "/")
strExpression = Replace(strExpression, ":+", "/")
strExpression = Replace(strExpression, "*+", "*")
strExpression = Replace(strExpression, "+*", "*")
strExpression = Replace(strExpression, "+^", "^")
strExpression = Replace(strExpression, "^+", "^")
End Sub
Function GetNum(line As String, OperationPos As Long, Side As Long, Optional Operation As String, Optional ByRef GetPosTo As Long) As Variant
Dim tmp As String, RightSideStart As Long, NextOperationMet As Boolean, LeftSideEnd As Long, IsExtent As Boolean
If MathSkipErrors Then
On Error GoTo lSkip
Else
On Error GoTo ErrHandler
End If
AddStage "Îïðåäåëåíèå îïåðàíäîâ"
If Side = GetLeft Then
' ëåâàÿ ñòîðîíà
LeftSideEnd = OperationPos '- 1
For n = 1 To Len(line)
GetPosTo = LeftSideEnd - n '+ 1
If LeftSideEnd - n <= 1 Then
tmp = Mid(line, 1, LeftSideEnd - 1)
GoTo ReturnVal
End If
tmp = Mid$(line, LeftSideEnd - n, n)
NextOperationMet = CBool(Not IsNumeric(Mid$(line, LeftSideEnd - n - 1, 1)) _
And Mid$(line, LeftSideEnd - n - 1, 1) <> "e" _
And Left$(tmp, 1) <> "e" _
And Mid$(line, LeftSideEnd - n - 1, 1) <> "," _
And Left$(tmp, 1) <> "," _
And Mid$(line, LeftSideEnd - n - 1, 1) <> ")" _
And Mid$(line, LeftSideEnd - n - 1, 1) <> "-" _
Or (Left$(tmp, 1) = "-" And Mid$(line, LeftSideEnd - n - 1, 1) <> "e"))
If NextOperationMet Or IsExtent Then
ReturnVal:
If Right$(tmp, 1) = ")" And Operation = Extent Then
tmp = Mid(tmp, 1, Len(tmp) - 1)
GetPosTo = GetPosTo - 1 '÷òîáû íà÷àëüíàÿ ñêîáêà òîæå çàðåïëåéñèëàñü
ElseIf tmp < 0 And Operation = Extent Then
GetPosTo = GetPosTo + 1 '÷òîáû îñòàâèòü ìèíóñ
tmp = Abs(tmp)
End If
GetNum = tmp
Exit Function ' åñëè ÷èñëî îêîí÷åíî - âûéòè èç ôóíêöèè
End If
Next n
Else
' ïðàâàÿ ñòîðîíà
RightSideStart = OperationPos + 1
For n = 1 To Len(line)
GetPosTo = RightSideStart + n - 1
tmp = Mid$(line, RightSideStart, n)
NextOperationMet = CBool(Not IsNumeric(Mid$(line, RightSideStart + n, 1)) _
And Mid$(line, RightSideStart + n - 1, 1) <> "e" _
And Mid$(line, RightSideStart + n, 1) <> "e" _
And Mid$(line, RightSideStart + n, 1) <> "," _
And Mid$(line, RightSideStart + n, 1) <> ".")
If (RightSideStart + n - 1 = Len(line) Or NextOperationMet) And IsNumeric(tmp) Then
GetNum = tmp
Exit Function ' åñëè ÷èñëî îêîí÷åíî - âûéòè èç ôóíêöèè
End If
Next n
End If
Exit Function
lSkip:
ADL "íåâîçìîæíî íàéòè îïåðàíä"
blnError = True
Exit Function
ErrHandler:
blnError = True
GlobalError.Description = "Îøèáêà ïðè ïîïûòêå íàéòè çíà÷åíèå " & IIf(Side = GetLeft, "ëåâîãî", "ïðàâîãî") & " îïåðàíäà"
GlobalError.Expression = line
Call GlobalErrorHandler
End Function