-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcamatheval.pas
652 lines (577 loc) · 19.7 KB
/
camatheval.pas
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
unit caMathEval;
{$INCLUDE ca.inc}
interface
uses
// Standard Delphi units
Classes,
SysUtils,
Math,
// ca units
caClasses,
caTypes,
caVector,
caUtils;
const
cComma = ',';
cDoubleComma = ',,';
type
EcaMathEvalException = class(Exception);
TcaOperandVariableEvent = procedure(Sender: TObject; const AOperand: String; var AValue: Extended) of object;
//---------------------------------------------------------------------------
// IcaMathTokens
//---------------------------------------------------------------------------
IcaMathTokens = interface
['{474F2872-F6BE-4BF6-84E2-94129C1111D5}']
// Property methods
function GetExpression: String;
function GetTokens: TStrings;
procedure SetExpression(const Value: String);
// Properties
property Expression: String read GetExpression write SetExpression;
property Tokens: TStrings read GetTokens;
end;
//---------------------------------------------------------------------------
// TcaMathTokens
//---------------------------------------------------------------------------
TcaMathTokens = class(TInterfacedObject, IcaMathTokens)
private
FExpression: String;
FTokens: TStrings;
// Property methods
function GetExpression: String;
function GetTokens: TStrings;
procedure SetExpression(const Value: String);
// Private methods
procedure UpdateTokens;
public
constructor Create;
destructor Destroy; override;
// Properties
property Expression: String read GetExpression write SetExpression;
property Tokens: TStrings read GetTokens;
end;
//---------------------------------------------------------------------------
// IcaInFixToPostfix
//---------------------------------------------------------------------------
IcaInFixToPostfix = interface
['{F84D2A15-9F76-415F-AE91-EAF176F80A95}']
// Property methods
function GetPostfixTokens: TStrings;
function GetInfixTokens: TStrings;
procedure SetInfixTokens(const Value: TStrings);
// Properties
property PostfixTokens: TStrings read GetPostfixTokens;
property InfixTokens: TStrings read GetInfixTokens write SetInfixTokens;
end;
//---------------------------------------------------------------------------
// TcaInfixToPostfix
//---------------------------------------------------------------------------
TcaInfixToPostfix = class(TInterfacedObject, IcaInFixToPostfix)
private
FPostfixTokens: TStrings;
FInfixTokens: TStrings;
// Property methods
function GetPostfixTokens: TStrings;
function GetInfixTokens: TStrings;
procedure SetInfixTokens(const Value: TStrings);
// Private methods
procedure UpdatePostfixTokens;
public
constructor Create;
destructor Destroy; override;
// Properties
property PostfixTokens: TStrings read GetPostfixTokens;
property InfixTokens: TStrings read GetInfixTokens write SetInfixTokens;
end;
//---------------------------------------------------------------------------
// IcaPostfixEvaluator
//---------------------------------------------------------------------------
IcaPostfixEvaluator = interface
['{44D75E82-81D5-457F-A82A-A46C9FCC0173}']
// Property methods
function GetPostfixTokens: TStrings;
function GetTrigUnits: TcaTrigUnits;
function GetValue: Extended;
procedure SetPostfixTokens(const Value: TStrings);
procedure SetTrigUnits(const Value: TcaTrigUnits);
// Event property methods
function GetOnGetOperandVariable: TcaOperandVariableEvent;
procedure SetOnGetOperandVariable(const Value: TcaOperandVariableEvent);
// Properties
property PostfixTokens: TStrings read GetPostfixTokens write SetPostfixTokens;
property TrigUnits: TcaTrigUnits read GetTrigUnits write SetTrigUnits;
property Value: Extended read GetValue;
// Event properties
property OnGetOperandVariable: TcaOperandVariableEvent read GetOnGetOperandVariable write SetOnGetOperandVariable;
end;
//---------------------------------------------------------------------------
// TcaPostfixEvaluator
//---------------------------------------------------------------------------
TcaPostfixEvaluator = class(TInterfacedObject, IcaPostfixEvaluator)
private
FPostfixTokens: TStrings;
FTrigUnits: TcaTrigUnits;
FValue: Extended;
FOnGetOperandVariable: TcaOperandVariableEvent;
// Property methods
function GetPostfixTokens: TStrings;
function GetTrigUnits: TcaTrigUnits;
function GetValue: Extended;
procedure SetPostfixTokens(const Value: TStrings);
procedure SetTrigUnits(const Value: TcaTrigUnits);
// Event property methods
function GetOnGetOperandVariable: TcaOperandVariableEvent;
procedure SetOnGetOperandVariable(const Value: TcaOperandVariableEvent);
// Private methods
function GetOperandValue(const AOperand: String): Extended;
function GetTrigOperand(AOperand: Extended): Extended;
procedure Evaluate;
protected
// Protected methods
procedure DoGetOperandVariable(const AOperand: String; var AValue: Extended); virtual;
public
constructor Create;
destructor Destroy; override;
// Properties
property PostfixTokens: TStrings read GetPostfixTokens write SetPostfixTokens;
property TrigUnits: TcaTrigUnits read GetTrigUnits write SetTrigUnits;
property Value: Extended read GetValue;
// Event properties
property OnGetOperandVariable: TcaOperandVariableEvent read GetOnGetOperandVariable write SetOnGetOperandVariable;
end;
//---------------------------------------------------------------------------
// IcaMathEvaluator
//---------------------------------------------------------------------------
IcaMathEvaluator = interface
['{BD670DED-CBF3-448C-81B6-C10568F7BA00}']
// Property methods
function GetExpression: String;
function GetTrigUnits: TcaTrigUnits;
function GetValue: Extended;
function GetValueAsString: String;
procedure SetExpression(const Value: String);
procedure SetTrigUnits(const Value: TcaTrigUnits);
// Event property methods
function GetOnGetOperandVariable: TcaOperandVariableEvent;
procedure SetOnGetOperandVariable(const Value: TcaOperandVariableEvent);
// Properties
property Expression: String read GetExpression write SetExpression;
property TrigUnits: TcaTrigUnits read GetTrigUnits write SetTrigUnits;
property Value: Extended read GetValue;
property ValueAsString: String read GetValueAsString;
// Event properties
property OnGetOperandVariable: TcaOperandVariableEvent read GetOnGetOperandVariable write SetOnGetOperandVariable;
end;
//---------------------------------------------------------------------------
// TcaMathEvaluator
//---------------------------------------------------------------------------
TcaMathEvaluator = class(TInterfacedObject, IcaMathEvaluator)
private
FExpression: String;
FInfixToPostfix: IcaInFixToPostfix;
FMathTokens: IcaMathTokens;
FOnGetOperandVariable: TcaOperandVariableEvent;
FPostfixEvaluator: IcaPostfixEvaluator;
FValue: Extended;
// Property methods
function GetExpression: String;
function GetTrigUnits: TcaTrigUnits;
function GetValue: Extended;
function GetValueAsString: String;
procedure SetExpression(const Value: String);
procedure SetTrigUnits(const Value: TcaTrigUnits);
// Event property methods
function GetOnGetOperandVariable: TcaOperandVariableEvent;
procedure SetOnGetOperandVariable(const Value: TcaOperandVariableEvent);
// Private methods
procedure UpdateValue;
// Event handler
procedure OperandVariableEvent(Sender: TObject; const AOperand: String; var AValue: Extended);
protected
// Protected methods
procedure DoGetOperandVariable(const AOperand: String; var AValue: Extended); virtual;
public
constructor Create;
property Expression: String read GetExpression write SetExpression;
property TrigUnits: TcaTrigUnits read GetTrigUnits write SetTrigUnits;
property Value: Extended read GetValue;
property ValueAsString: String read GetValueAsString;
// Event properties
property OnGetOperandVariable: TcaOperandVariableEvent read GetOnGetOperandVariable write SetOnGetOperandVariable;
end;
implementation
//---------------------------------------------------------------------------
// TcaMathTokens
//---------------------------------------------------------------------------
constructor TcaMathTokens.Create;
begin
inherited;
FTokens := TStringList.Create;
end;
destructor TcaMathTokens.Destroy;
begin
FTokens.Free;
inherited;
end;
function TcaMathTokens.GetExpression: String;
begin
Result := FExpression;
end;
function TcaMathTokens.GetTokens: TStrings;
begin
Result := FTokens;
end;
procedure TcaMathTokens.SetExpression(const Value: String);
begin
FExpression := Value;
UpdateTokens;
end;
procedure TcaMathTokens.UpdateTokens;
var
Index: Integer;
Ch: Char;
ChPrev: Char;
TokensStr: String;
AExpression: String;
PreDelim: String;
PostDelim: String;
ZeroInserts: IcaIntegerVector;
begin
AExpression := LowerCase(FExpression);
Utils.StripChar(#32, AExpression);
// Check for any '-' operators used for negation or '+' used as a prefix
ZeroInserts := TcaIntegerVector.Create;
for Index := 1 to Length(AExpression) do
begin
Ch := AExpression[Index];
if (Ch = '-') or (Ch = '+') then
begin
// If the '-' is the first token or if it is not preceeded
// by an operand, then insert a '0' operand before the '-'
if Index = 1 then
ZeroInserts.Add(Index)
else
begin
ChPrev := AExpression[Index - 1];
if Utils.IsOperator(ChPrev) or Utils.IsBracket(ChPrev) then
ZeroInserts.Add(Index);
end;
end;
end;
for Index := ZeroInserts.Count - 1 downto 0 do
Insert('0', AExpression, ZeroInserts[Index]);
// Break expression into comma separated tokens
TokensStr := '';
for Index := 1 to Length(AExpression) do
begin
PreDelim := '';
PostDelim := '';
Ch := AExpression[Index];
if Utils.IsOperator(Ch) or Utils.IsBracket(Ch) then
begin
PreDelim := cComma;
PostDelim := cComma;
end;
TokensStr := TokensStr + PreDelim + Ch + PostDelim;
end;
if TokensStr[1] = cComma then
Utils.DeleteFromStart(TokensStr, 1);
Utils.Replace(TokensStr, cDoubleComma, cComma);
FTokens.CommaText := TokensStr;
end;
//---------------------------------------------------------------------------
// TcaInfixToPostfix
//---------------------------------------------------------------------------
constructor TcaInfixToPostfix.Create;
begin
inherited;
FPostfixTokens := TStringList.Create;
FInfixTokens := TStringList.Create;
end;
destructor TcaInfixToPostfix.Destroy;
begin
FPostfixTokens.Free;
FInfixTokens.Free;
inherited;
end;
function TcaInfixToPostfix.GetPostfixTokens: TStrings;
begin
Result := FPostfixTokens;
end;
function TcaInfixToPostfix.GetInfixTokens: TStrings;
begin
Result := FInfixTokens;
end;
procedure TcaInfixToPostfix.SetInfixTokens(const Value: TStrings);
begin
FInfixTokens.Assign(Value);
UpdatePostfixTokens;
end;
procedure TcaInfixToPostfix.UpdatePostfixTokens;
var
Index: Integer;
OperatorStack: IcaStringStack;
Token: String;
begin
FPostfixTokens.Clear;
OperatorStack := TcaStringList.Create;
for Index := 0 to FInfixTokens.Count - 1 do
begin
Token := FInfixTokens[Index];
// Add operand to output
if Utils.IsOperand(Token) then
begin
// Check for special constants
if Token = 'pi' then Token := FloatToStr(Pi);
if Token = 'e' then Token := FloatToStr(Exp(1));
FPostfixTokens.Add(Token);
Continue;
end;
// Push open bracket on to stack
if Token = '(' then
begin
OperatorStack.Push(Token);
Continue;
end;
// Add pending operators to output
if Token = ')' then
begin
// Pop operators from stack until an open bracket is found
while OperatorStack.Peek <> '(' do
begin
if OperatorStack.IsEmpty then
raise EcaMathEvalException.Create('Mismatched bracket error');
FPostfixTokens.Add(OperatorStack.Pop);
end;
// Discard the open bracket
OperatorStack.Pop;
Continue;
end;
// Push function onto the stack
if Utils.IsFunction(Token) then
begin
OperatorStack.Push(Token);
Continue;
end;
// Add pending operators to output
if Utils.IsOperator(Token) then
begin
while
(not OperatorStack.IsEmpty) and
(not (Utils.GetOperatorPrecedence(OperatorStack.Peek, Token) = opLower)) and
(not (Utils.GetOperatorPrecedence(OperatorStack.Peek, Token) = opSameRightAssoc)) do
FPostfixTokens.Add(OperatorStack.Pop);
OperatorStack.Push(Token);
Continue;
end;
end;
// Add remaining operators to output
while not OperatorStack.IsEmpty do
begin
if OperatorStack.Peek = '(' then
raise EcaMathEvalException.Create('Mismatched bracket error');
FPostfixTokens.Add(OperatorStack.Pop);
end;
OperatorStack := nil;
end;
//---------------------------------------------------------------------------
// TcaPostfixEvaluator
//---------------------------------------------------------------------------
constructor TcaPostfixEvaluator.Create;
begin
inherited;
FPostfixTokens := TStringList.Create;
end;
destructor TcaPostfixEvaluator.Destroy;
begin
FPostfixTokens.Free;
inherited;
end;
// Protected methods
procedure TcaPostfixEvaluator.DoGetOperandVariable(const AOperand: String; var AValue: Extended);
begin
if Assigned(FOnGetOperandVariable) then
FOnGetOperandVariable(Self, AOperand, AValue);
end;
// Private methods
procedure TcaPostfixEvaluator.Evaluate;
var
EvalResult: Extended;
Index: Integer;
LeftOperand: Extended;
Operand: Extended;
OperandStack: IcaStringStack;
RightOperand: Extended;
Token: String;
MathUtils: IcaMathUtils;
begin
MathUtils := Utils as IcaMathUtils;
OperandStack := TcaStringList.Create;
for Index := 0 to FPostfixTokens.Count - 1 do
begin
Token := FPostfixTokens[Index];
if Utils.IsOperand(Token) then
OperandStack.Push(Token)
else
begin
EvalResult := 0;
if Utils.IsOperator(Token) then
begin
RightOperand := GetOperandValue(OperandStack.Pop);
LeftOperand := GetOperandValue(OperandStack.Pop);
if Token = '+' then EvalResult := LeftOperand + RightOperand else
if Token = '-' then EvalResult := LeftOperand - RightOperand else
if Token = '*' then EvalResult := LeftOperand * RightOperand else
if Token = '/' then EvalResult := LeftOperand / RightOperand else
if Token = '^' then EvalResult := Power(LeftOperand, RightOperand);
end;
if Utils.IsFunction(Token) then
begin
Operand := GetOperandValue(OperandStack.Pop);
if Token = 'cos' then EvalResult := Cos(GetTrigOperand(Operand)) else
if Token = 'exp' then EvalResult := Exp(Operand) else
if Token = 'int' then EvalResult := Int(Operand) else
if Token = 'sin' then EvalResult := Sin(GetTrigOperand(Operand)) else
if Token = 'frac' then EvalResult := Frac(Operand) else
if Token = 'round' then EvalResult := Round(Operand) else
if Token = 'trunc' then EvalResult := MathUtils.Trunc(Operand) else
if Token = 'tan' then EvalResult := Tan(GetTrigOperand(Operand)) else
if Token = 'ln' then EvalResult := Ln(Operand) else
if Token = 'log10' then EvalResult := Log10(Operand) else
if Token = 'sqrt' then EvalResult := Sqrt(Operand);
end;
OperandStack.Push(FloatToStr(EvalResult));
end;
end;
if (OperandStack as IcaStringList).Count <> 1 then
raise EcaMathEvalException.Create('Postfix evaluator stack count <> 1');
// FValue := StrToFloat(OperandStack.Pop);
FValue := GetOperandValue(OperandStack.Pop);
end;
function TcaPostfixEvaluator.GetTrigOperand(AOperand: Extended): Extended;
begin
if FTrigUnits = tuDegrees then
Result := DegToRad(AOperand)
else
Result := AOperand;
end;
function TcaPostfixEvaluator.GetOperandValue(const AOperand: String): Extended;
var
Handled: Boolean;
OperandChar: Char;
begin
Result := 0;
Handled := False;
if AOperand <> '' then
begin
OperandChar := AOperand[1];
if OperandChar in ['a'..'z', 'A'..'Z'] then
begin
DoGetOperandVariable(AOperand, Result);
Handled := True;
end;
end;
if not Handled then
Result := Utils.Str2Float(AOperand, 0);
end;
function TcaPostfixEvaluator.GetPostfixTokens: TStrings;
begin
Result := FPostfixTokens;
end;
function TcaPostfixEvaluator.GetTrigUnits: TcaTrigUnits;
begin
Result := FTrigUnits;
end;
function TcaPostfixEvaluator.GetValue: Extended;
begin
Result := FValue;
end;
procedure TcaPostfixEvaluator.SetPostfixTokens(const Value: TStrings);
begin
FPostfixTokens.Assign(Value);
Evaluate;
end;
procedure TcaPostfixEvaluator.SetTrigUnits(const Value: TcaTrigUnits);
begin
FTrigUnits := Value;
end;
// Event property methods
function TcaPostfixEvaluator.GetOnGetOperandVariable: TcaOperandVariableEvent;
begin
Result := FOnGetOperandVariable;
end;
procedure TcaPostfixEvaluator.SetOnGetOperandVariable(const Value: TcaOperandVariableEvent);
begin
FOnGetOperandVariable := Value;
end;
//---------------------------------------------------------------------------
// TcaMathEvaluator
//---------------------------------------------------------------------------
constructor TcaMathEvaluator.Create;
begin
inherited;
FMathTokens := TcaMathTokens.Create;
FInfixToPostfix := TcaInFixToPostfix.Create;
FPostfixEvaluator := TcaPostfixEvaluator.Create;
FPostfixEvaluator.OnGetOperandVariable := OperandVariableEvent;
SetTrigUnits(tuDegrees);
end;
// Protected methods
procedure TcaMathEvaluator.DoGetOperandVariable(const AOperand: String; var AValue: Extended);
begin
if Assigned(FOnGetOperandVariable) then
FOnGetOperandVariable(Self, AOperand, AValue);
end;
// Event handler
procedure TcaMathEvaluator.OperandVariableEvent(Sender: TObject; const AOperand: String; var AValue: Extended);
begin
DoGetOperandVariable(AOperand, AValue);
end;
// Property methods
function TcaMathEvaluator.GetExpression: String;
begin
Result := FExpression;
end;
function TcaMathEvaluator.GetTrigUnits: TcaTrigUnits;
begin
Result := FPostfixEvaluator.TrigUnits;
end;
procedure TcaMathEvaluator.SetExpression(const Value: String);
begin
FExpression := Value;
UpdateValue;
end;
function TcaMathEvaluator.GetValue: Extended;
begin
Result := FValue;
end;
function TcaMathEvaluator.GetValueAsString: String;
begin
Result := FloatToStr(FValue);
end;
procedure TcaMathEvaluator.SetTrigUnits(const Value: TcaTrigUnits);
begin
FPostfixEvaluator.TrigUnits := Value;
end;
procedure TcaMathEvaluator.UpdateValue;
begin
if FExpression <> '' then
begin
FMathTokens.Expression := FExpression;
FInfixToPostfix.InfixTokens := FMathTokens.Tokens;
FPostfixEvaluator.PostfixTokens := FInfixToPostfix.PostfixTokens;
FValue := FPostfixEvaluator.Value;
end
else
FValue := 0;
end;
// Event property methods
function TcaMathEvaluator.GetOnGetOperandVariable: TcaOperandVariableEvent;
begin
Result := FOnGetOperandVariable;
end;
procedure TcaMathEvaluator.SetOnGetOperandVariable(const Value: TcaOperandVariableEvent);
begin
FOnGetOperandVariable := Value;
end;
end.