-
Notifications
You must be signed in to change notification settings - Fork 0
/
MicroCalc.dpr
1546 lines (1395 loc) · 39 KB
/
MicroCalc.dpr
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
MICROCALC DEMONSTRATION PROGRAM Version 1.00A
This program is hereby donated to the public domain
for non-commercial use only. Dot commands are for
the program lister: LISTT.PAS (available with our
TURBO TUTOR): .PA, .CP20, etc...
INSTRUCTIONS
1. Compile this program using the TURBO.COM compiler.
If a memory overflow occurs, compile the program:
CALCMAIN.PAS which will include this program.
2. Exit the program by typing: /Q
Here is a note to the compiler: }
program MicroCalc;
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
{$R-,U-,V-,X-,C-}
{$APPTYPE CONSOLE}
{$IFnDEF FPC}
{$R *.res}
{$ENDIF}
{ Um das Projekt mit FlexCel-Support zu übersetzen, der Direktive
DEFINE ein Dollar-Zeichen voranstellen.
}
{$DEFINE FLEXCEL}
uses
{$IFnDEF FPC}
System.SysUtils,
Crt32_64 in 'Crt32_64.pas'
{$IFDEF FLEXCEL}
,
VCL.FlexCel.Core,
FlexCel.XlsAdapter,
FlexCel.Render
{$ENDIF}
;
{$ELSE}
SysUtils,
Crt
{$IFDEF FLEXCEL}
,
LCL.FlexCel.Core,
FlexCel.XlsAdapter,
FlexCel.Render
{$ENDIF}
;
{$ENDIF}
{Using it before implemented}
procedure Recalculate; Forward;
const
FXMax: Char = 'G'; { Maximum number of columns in spread sheet }
FYMax = 21; { Maximum number of lines in spread sheet }
type
Anystring = string[70];
SheetIndex = 'A'..'G';
Attributes = (Constant,Formula,Txt,OverWritten,Locked,Calculated);
{ The spreadsheet is made out of Cells every Cell is defined as }
{ the following record:}
CellRec = record
CellStatus: set of Attributes; { Status of cell (see type def.) }
Contents: String[70]; { Contains a formula or some text }
Value: Real; { Last calculated cell value }
DEC,FW: 0..20; { Decimals and Cell Whith }
end;
Cells = array[SheetIndex,1..FYMax] of CellRec;
const
XPOS: array[SheetIndex] of integer = (3,14,25,36,47,58,68);
var
Sheet: Cells; { Definition of the spread sheet }
FX: SheetIndex; { Culumn of current cell }
FY: Integer; { Line of current cell }
{ Ch: Char; } { Last read character }
Ch: AnsiChar; { Last read character }
MCFile: file of CellRec; { File to store sheets in }
AutoCalc: boolean; { Recalculate after each entry? }
{$IFDEF FLEXCEL}
XlsSupport: boolean; { Support Excel file format? }
{$ENDIF}
{ For easy reference the procedures and functions are grouped in mo-}
{ dules called MC-MOD00 through MC-MOD05. }
{.PA}
{*******************************************************************}
{* SOURCE CODE MODULE: MC-MOD00 *}
{* PURPOSE: Micellaneous utilities and commands. *}
{*******************************************************************}
procedure Msg(S: AnyString);
begin
GotoXY(1,24);
ClrEol;
Write(S);
end;
procedure Flash(X: integer; S: AnyString; Blink: boolean);
begin
HighVideo;
GotoXY(X,23);
Write(S);
if Blink then
begin
repeat
GotoXY(X,23);
Blink:=not Blink; if Blink then HighVideo else LowVideo;
Write(S);
Delay(175);
until KeyPressed;
end;
LowVideo;
end;
procedure IBMCh(var Ch: AnsiChar);
begin
case Ch of
'H': Ch:=^E;
'P': Ch:=^X;
'M': Ch:=^D;
'K': Ch:=^S;
'S': Ch:=#127;
'R': Ch:=^V;
'G',
'I',
'O',
'Q': Ch:=#00;
end;
end;
procedure Auto;
begin
AutoCalc:=not AutoCalc;
if AutoCalc then Flash(60,'AutoCalc: ON ',false)
else Flash(60,'AutoCalc: OFF',false);
end;
{$IFDEF FLEXCEL}
procedure XlsSupp;
begin
XlsSupport := not XlsSupport;
if XlsSupport then
Flash(74, 'Xls:On ', false)
else
Flash(74, 'Xls:Off', false)
end;
{$ENDIF}
{.PA}
{*******************************************************************}
{* SOURCE CODE MODULE: MC-MOD01 *}
{* PURPOSE: Display grid and initialize all cells *}
{* in the spread sheet. *}
{*******************************************************************}
procedure Grid;
var I: integer;
Count: Char;
begin
HighVideo;
For Count:='A' to FXMax do
begin
GotoXY(XPos[Count],1);
Write(Count);
end;
GotoXY(1,2);
for I:=1 to FYMax do writeln(I:2);
LowVideo;
if AutoCalc then Flash(60,'AutoCalc: ON' ,false)
else Flash(60,'AutoCalc: OFF',false);
{$IFDEF FLEXCEL}
if XlsSupport then
Flash(74, 'Xls:On ', false)
else
Flash(74, 'Xls:Off', false);
{$ENDIF}
Flash(33,' Type / for Commands',false);
end;
procedure Init;
var
I: SheetIndex;
J: Integer;
LastName: string[2];
begin
for I:='A' to FXMAX do
begin
for J:=1 to FYMAX do
begin
with Sheet[I,J] do
begin
CellStatus:=[Txt];
Contents:='';
Value:=0;
DEC:=2; { Default number of decimals }
FW:=10; { Default field width }
end;
end;
end;
AutoCalc:=True;
{$IFDEF FLEXCEL}
XlsSupport := False;
{$ENDIF}
FX:='A'; FY:=1; { First field in upper left corner }
end;
procedure Clear;
begin
HighVideo;
GotoXY(1,24); ClrEol;
Write('Clear this worksheet? (Y/N) ');
{ repeat Read(Kbd,Ch) until Upcase(Ch) in ['Y','N'];}
repeat Ch := readkey until Upcase(Ch) in ['Y','N'];
Write(Upcase(Ch));
if UpCase(Ch)='Y' then
begin
ClrScr;
Init;
Grid;
end;
end;
{.PA}
{*******************************************************************}
{* SOURCE CODE MODULE: MC-MOD02 *}
{* PURPOSE: Display values in cells and move between *}
{* cells in the spread sheet. *}
{*******************************************************************}
procedure FlashType;
begin
with Sheet[FX,FY] do
begin
GotoXY(1,23);
Write(FX,FY:2,' ');
if Formula in CellStatus then write('Formula:') else
if Constant in CellStatus then Write('Numeric ') else
if Txt in CellStatus then Write('Text ');
GotoXY(1,24); ClrEol;
if Formula in CellStatus then Write(Contents);
end;
end;
{ The following procedures move between the Cells on the calc sheet.}
{ Each Cell has an associated record containing its X,Y coordinates }
{ and data. See the type definition for "Cell". }
procedure GotoCell(GX: SheetIndex; GY: integer);
begin
with Sheet[GX,GY] do
begin
HighVideo;
GotoXY(XPos[GX],GY+1);
Write(' ');
GotoXY(XPos[GX],GY+1);
if Txt in CellStatus then Write(Contents)
else
begin
if DEC>=0 then Write(Value:FW:DEC)
else Write(Value:FW);
end;
FlashType;
GotoXY(XPos[GX],GY+1);
end;
LowVideo;
end;
{.CP20}
procedure LeaveCell(FX:SheetIndex;FY: integer);
begin
with Sheet[FX,FY] do
begin
GotoXY(XPos[FX],FY+1);
LowVideo;
if Txt in CellStatus then Write(Contents)
else
begin
if DEC>=0 then Write(Value:FW:DEC)
else Write(Value:FW);
end;
end;
end;
{.CP20}
procedure Update;
var
UFX: SheetIndex;
UFY: integer;
begin
ClrScr;
Grid;
for UFX:='A' to FXMax do for UFY:=1 to FYMax do
if Sheet[UFX,UFY].Contents<>'' then LeaveCell(UFX,UFY);
GotoCell(FX,FY);
end;
{.CP20}
procedure MoveDown;
var Start: integer;
begin
LeaveCell(FX,FY);
Start:=FY;
repeat
FY:=FY+1;
if FY>FYMax then FY:=1;
until (Sheet[FX,FY].CellStatus*[OverWritten,Locked]=[]) or (FY=Start);
if FY<>Start then GotoCell(FX,FY);
end;
{.CP20}
procedure MoveUp;
var Start: integer;
begin
LeaveCell(FX,FY);
Start:=FY;
repeat
FY:=FY-1;
if FY<1 then FY:=FYMax;
until (Sheet[FX,FY].CellStatus*[OverWritten,Locked]=[]) or (FY=Start);
if FY<>Start then GotoCell(FX,FY);
end;
{.CP20}
procedure MoveRight;
var Start: SheetIndex;
begin
LeaveCell(FX,FY);
Start:=FX;
repeat
FX:=Succ(FX);
if FX>FXMax then
begin
FX:='A';
FY:=FY+1;
if FY>FYMax then FY:=1;
end;
until (Sheet[FX,FY].CellStatus*[OverWritten,Locked]=[]) or (FX=Start);
if FX<>Start then GotoCell(FX,FY);
end;
{.CP20}
procedure MoveLeft;
var Start: SheetIndex;
begin
LeaveCell(FX,FY);
Start:=FX;
repeat
FX:=Pred(FX);
if FX<'A' then
begin
FX:=FXMax;
FY:=FY-1;
if FY<1 then FY:=FYMax;
end;
until (Sheet[FX,FY].CellStatus*[OverWritten,Locked]=[]) or (FX=Start);
if FX<>Start then GotoCell(FX,FY);
end;
{.PA}
{*******************************************************************}
{* SOURCE CODE MODULE: MC-MOD03 *}
{* PURPOSE: Read, Save and Print a spread sheet. *}
{* Display on-line manual. *}
{*******************************************************************}
type
String3 = string[3];
var
FileName: string[14];
Line: string[100];
function Exist(FileN: AnyString): boolean;
var F: file;
begin
{$I-}
assign(F,FileN);
reset(F);
{$I+}
if IOResult<>0 then Exist:=false
else
begin
Exist:=true;
close(F);
end;
end;
procedure GetFileName(var Line: AnyString; FileType:String3);
begin
Line:='';
repeat
{ Read(Kbd,Ch);}
Ch := readkey;
if Upcase(Ch) in ['A'..'Z',^M] then
begin
write(Upcase(Ch));
Line:=Line+Ch;
end;
until (Ch=^M) or (length(Line)=8);
if Ch=^M then Delete(Line,Length(Line),1);
if Line<>'' then Line:=Line+'.'+FileType;
end;
{.CP20}
procedure Save;
var I: SheetIndex;
J: integer;
{$IFDEF FLEXCEL}
xls: TXlsFile;
procedure SetCellValues(I: SheetIndex; J: integer);
var
attr: Attributes;
sheetIdx: Integer;
charIdx: Integer;
AZaehler: Integer;
begin
sheetIdx := Ord(I)-64;
for attr := Constant to Calculated do
begin
if attr in Sheet[I,J].CellStatus then
begin
case attr of
Constant:
begin
if Sheet[I,J].CellStatus = [Constant,Formula,Calculated] then {Ist das eine Formel?}
begin
charIdx := AnsiPos('>', Sheet[I,J].Contents); {Ist mmindestens ein Bereich von Zellen zu berechnen?}
if charIdx <> 0 then {Dann die Formel Excel-kompatibel umwandeln}
begin
for AZaehler := 1 to Length(Sheet[I,J].Contents) do
begin
if Sheet[I,J].Contents[AZaehler] = '>' then
Sheet[I,J].Contents[AZaehler] := ':';
end;
Sheet[I,J].Contents := 'Sum'+Sheet[I,J].Contents;
end;
{Formel endgültig schreiben}
xls.SetCellValue(J, sheetIdx, TFormula.Create('=' + Sheet[I,J].Contents))
end
else {Sonst Zahlenwert}
xls.SetCellValue(J, sheetIdx, Double(Sheet[I,J].Value));
break
end;
Txt:
begin
xls.SetCellFromString(J, sheetIdx, Sheet[I,J].Contents);
break
end;
end;
end;
end;
end;
{$ENDIF}
begin
HighVideo;
Msg('Save: Enter filename ');
GetFileName(Filename,'MCS');
if FileName<>'' then
begin
{$IFDEF FLEXCEL}
if XlsSupport = True then
begin
xls := TXlsFile.Create(1, TExcelFileFormat.v2019, True);
end
else
begin
Assign(MCFile,FileName);
Rewrite(MCFile);
end;
{$ELSE}
Assign(MCFile,FileName);
Rewrite(MCFile);
{$ENDIF}
for I:='A' to FXmax do
begin
for J:=1 to FYmax do
{$IFDEF FLEXCEL}
begin
if XlsSupport = True then
begin
SetCellValues(I, J);
end
else
write(MCFile, Sheet[I,J]);
end
{$ELSE}
write(MCfile,Sheet[I,J]);
{$ENDIF}
end;
Grid;
{$IFDEF FLEXCEL}
if XlsSupport = True then
begin
xls.Save(Filename+'.xlsx');
xls.Free;
end
else
begin
Close(MCFile);
end;
{$ELSE}
Close(MCFile);
{$ENDIF}
LowVideo;
GotoCell(FX,FY);
end;
end;
{.CP30}
procedure Load;
{$IFDEF FLEXCEL}
var
xls: TXlsFile;
ColTypes: TArray<TColumnImportType>;
CellValue: TCellValue;
AFormular: TFormula;
AString: string;
procedure GetCellValues(FX: SheetIndex; FY: integer);
var
sheetIdx: Integer;
charIdx: Integer;
AZaehler: Integer;
begin
sheetIdx := Ord(FX)-64;
CellValue := xls.GetCellValue(FY, sheetIdx);
if CellValue.IsNumber then
begin
Sheet[FX,FY].Value := CellValue.AsNumber;
Sheet[FX,FY].Contents := FloatToStr(Sheet[FX,FY].Value);
Sheet[FX,FY].CellStatus := [Constant];
end;
if CellValue.IsString then
begin
Sheet[FX,FY].Contents := CellValue.AsString.ToString;
Sheet[FX,FY].Value := 0;
Sheet[FX,FY].CellStatus := [Txt];
end;
if CellValue.IsFormula then
begin
AFormular := CellValue.AsFormula;
AString := AFormular.Text;
Delete(AString,1,1); {= von Formel entfernen}
charIdx := AnsiPos('(', Astring); {Wenn vor der Klammer ein Kommando steht generell entfernen, damit kann MicroCalc nichts anfangen}
if charIdx > 1 then
for AZaehler := 1 to charIdx-1 do
Delete(AString,1,1);
charIdx := AnsiPos(':', Astring); {Ist mmindestens ein Bereich von Zellen zu berechnen?}
if charIdx <> 0 then {Dann die Formel Excel-kompatibel umwandeln}
begin
for AZaehler := 1 to Length(AString) do
begin
if AString[AZaehler] = ':' then
AString[AZaehler] := '>';
end;
end;
Sheet[FX,FY].Contents := AString;
Sheet[FX,FY].CellStatus := [Constant, Formula];
end;
end;
{$ENDiF}
begin
HighVideo;
Msg('Load: Enter filename ');
GetFileName(Filename,'MCS');
if (Filename<>'') then
begin
{$IFDEF FLEXCEL}
if XlsSupport = True then
begin
if (not exist(FileName+'.xlsx')) then
repeat
Msg('File not Found: Enter another filename ');
GetFileName(Filename,'MCS');
until exist(FileName+'.xlsx') or (FileName='');
end
else
begin
{$ENDIF}
if (not exist(FileName)) then
repeat
Msg('File not Found: Enter another filename ');
GetFileName(Filename,'MCS');
until exist(FileName) or (FileName='');
{$IFDEF FLEXCEL}
end;
{$ENDIF}
end;
{$IFDEF FLEXCEL}
xls := TXlsFile.Create(1, TExcelFileFormat.v2019, False);
{$ENDIF}
if FileName<>'' then
begin
ClrScr;
Msg('Please Wait. Loading definition...');
{$IFDEF FLEXCEL}
if XlsSupport = True then
begin
xls.Open(FileName+'.xlsx',TFileFormats.Xlsx,' ',1,1,ColTypes);
end
else
begin
Assign(MCFile,FileName);
Reset(MCFile);
end;
{$ELSE}
Assign(MCFile,FileName);
Reset(MCFile);
{$ENDIF}
for FX:='A' to FXmax do
for FY:=1 to FYmax do
{$IFDEF FLEXCEL}
begin
if XlsSupport = True then
GetCellValues(FX, FY)
else
read(MCFile,Sheet[FX,FY]);
end;
{$ELSE}
read(MCFile,Sheet[FX,FY]);
{$ENDIF}
FX:='A'; FY:=1;
LowVideo;
UpDate;
end;
{$IFDEF FLEXCEL}
Recalculate;
xls.Free;
{$ENDIF}
GotoCell(FX,FY);
end;
{.PA}
procedure Print;
var
I: SheetIndex;
J,Count,
LeftMargin: Integer;
P: string[20];
MCFile: Text;
begin
HighVideo;
Msg('Print: Enter filename "P" for Printer> ');
GetFileName(Filename,'LST');
Msg('Left margin > '); Read(LeftMargin);
if FileName='P.LST' then FileName:='Printer';
Msg('Printing to: ' + FileName + '....');
Assign(MCFile,FileName);
Rewrite(MCFile);
For Count:=1 to 5 do Writeln(MCFile);
for J:=1 to FYmax do
begin
Line:='';
for I:='A' to FXmax do
begin
with Sheet[I,J] do
begin
while (Length(Line)<XPOS[I]-4) do Line:=Line+' ';
if (Constant in CellStatus) or (Formula in CellStatus) then
begin
if not (Locked in CellStatus) then
begin
if DEC>0 then Str(Value:FW:DEC,P) else Str(Value:FW,P);
Line:=Line+P;
end;
end else Line:=Line+Contents;
end; { With }
end; { One line }
For Count:=1 to LeftMargin do Write(MCFile,' ');
writeln(MCFile,Line);
end; { End Column }
Grid;
Close(MCFile);
LowVideo;
GotoCell(FX,FY);
end;
{.PA}
procedure Help;
var
H: text;
Line: string[80];
J: integer;
Bold: boolean;
begin
if Exist('CALC.HLP') then
begin
Assign(H,'CALC.HLP');
Reset(H);
while not Eof(H) do
begin
ClrScr; Bold:=false; LowVideo;
Readln(H,Line);
repeat
Write(' ');
For J:=1 to Length(Line) do
begin
if Line[J]=^B then
begin
Bold:=not Bold;
if Bold then HighVideo else LowVideo;
end else write(Line[J]);
end;
Writeln;
Readln(H,Line);
until Eof(H) or (Copy(Line,1,3)='.PA');
GotoXY(26,24); HighVideo;
write('<<< Please press any key to continue >>>');
LowVideo;
{ read(Kbd,Ch);}
Ch := readkey;
end;
GotoXY(20,24); HighVideo;
write('<<< Please press <RETURN> to start MicroCalc >>>');
LowVideo;
Readln(Ch);
UpDate;
end else { Help file did not exist }
begin
Msg('To get help the file CALC.HLP must be on your disk. Press <RETURN>');
{ repeat Read(kbd,Ch) until Ch=^M;}
repeat Ch := readkey until Ch=^M;
GotoCell(FX,FY);
end;
end;
{.PA}
{*******************************************************************}
{* SOURCE CODE MODULE: MC-MOD04 *}
{* PURPOSE: Evaluate formulas. *}
{* Recalculate spread sheet. *}
{* *}
{* NOTE: This module contains recursive procedures *}
{*******************************************************************}
var
Form: Boolean;
{$A-}
procedure Evaluate(var IsFormula: Boolean; { True if formula}
var Formula: AnyString; { Fomula to evaluate}
var Value: Real; { Result of formula }
var ErrPos: Integer);{ Position of error }
const
Numbers: set of Char = ['0'..'9'];
EofLine = ^M;
var
Pos: Integer; { Current position in formula }
Ch: AnsiChar; { Current character being scanned }
EXY: string[3]; { Intermidiate string for conversion }
{ Procedure NextCh returns the next character in the formula }
{ The variable Pos contains the position ann Ch the character }
procedure NextCh;
begin
repeat
Pos:=Pos+1;
if Pos<=Length(Formula) then
Ch:=Formula[Pos] else Ch:=eofline;
until Ch<>' ';
end { NextCh };
function Expression: Real;
var
E: Real;
Opr: Char;
function SimpleExpression: Real;
var
S: Real;
Opr: Char;
function Term: Real;
var
T: Real;
function SignedFactor: Real;
function Factor: Real;
type
StandardFunction = (fabs,fsqrt,fsqr,fsin,fcos,
farctan,fln,flog,fexp,ffact);
StandardFunctionList = array[StandardFunction] of string[6];
const
StandardFunctionNames: StandardFunctionList =('ABS','SQRT','SQR','SIN','COS',
'ARCTAN','LN','LOG','EXP','FACT');
var
E,EE,L: Integer; { intermidiate variables }
Found:Boolean;
F: Real;
Sf:StandardFunction;
OldEFY, { Current cell }
EFY,
SumFY,
Start:Integer;
OldEFX,
EFX,
SumFX:SheetIndex;
CellSum: Real;
function Fact(I: Integer): Real;
begin
if I > 0 then begin Fact:=I*Fact(I-1); end
else Fact:=1;
end { Fact };
{.PA}
begin { Function Factor }
if Ch in Numbers then
begin
Start:=Pos;
repeat NextCh until not (Ch in Numbers);
if Ch='.' then repeat NextCh until not (Ch in Numbers);
if Ch='E' then
begin
NextCh;
repeat NextCh until not (Ch in Numbers);
end;
Val(Copy(Formula,Start,Pos-Start),F,ErrPos);
end else
if Ch='(' then
begin
NextCh;
F:=Expression;
if Ch=')' then NextCh else ErrPos:=Pos;
end else
if Ch in ['A'..'G'] then { Maybe a cell reference }
begin
EFX:=Char(Ch);
NextCh;
if Ch in Numbers then
begin
F:=0;
EXY:=Ch; NextCh;
if Ch in Numbers then
begin
EXY:=EXY+Ch;
NextCh;
end;
Val(EXY,EFY,ErrPos);
IsFormula:=true;
if (Constant in Sheet[EFX,EFY].CellStatus) and
not (Calculated in Sheet[EFX,EFY].CellStatus) then
begin
Evaluate(Form,Sheet[EFX,EFY].contents,f,ErrPos);
Sheet[EFX,EFY].CellStatus:=Sheet[EFX,EFY].CellStatus+[Calculated]
end else if not (Txt in Sheet[EFX,EFY].CellStatus) then
F:=Sheet[EFX,EFY].Value;
if Ch='>' then
begin
OldEFX:=EFX; OldEFY:=EFY;
NextCh;
EFX:=Char(Ch);
NextCh;
if Ch in Numbers then
begin
EXY:=Ch;
NextCh;
if Ch in Numbers then
begin
EXY:=EXY+Ch;
NextCh;
end;
val(EXY,EFY,ErrPos);
Cellsum:=0;
for SumFY:=OldEFY to EFY do
begin
for SumFX:=OldEFX to EFX do
begin
F:=0;
if (Constant in Sheet[SumFX,SumFY].CellStatus) and
not (Calculated in Sheet[SumFX,SumFY].CellStatus) then
begin
Evaluate(Form,Sheet[SumFX,SumFY].contents,f,errPos);
Sheet[SumFX,SumFY].CellStatus:=
Sheet[SumFX,SumFY].CellStatus+[Calculated];
end else if not (Txt in Sheet[SumFX,SumFY].CellStatus) then
F:=Sheet[SumFX,SumFY].Value;
Cellsum:=Cellsum+f;
f:=Cellsum;
end;
end;
end;
end;
end;
end else
begin
found:=false;
for sf:=fabs to ffact do
if not found then
begin
l:=Length(StandardFunctionNames[sf]);
if copy(Formula,Pos,l)=StandardFunctionNames[sf] then
begin
Pos:=Pos+l-1; NextCh;
F:=Factor;
case sf of
fabs: f:=abs(f);
fsqrt: f:=sqrt(f);
fsqr: f:=sqr(f);
fsin: f:=sin(f);
fcos: f:=cos(f);
farctan: f:=arctan(f);
fln : f:=ln(f);
flog: f:=ln(f)/ln(10);
fexp: f:=exp(f);
ffact: f:=fact(trunc(f));
end;
Found:=true;
end;
end;
if not Found then ErrPos:=Pos;
end;
Factor:=F;
end { function Factor};
{.PA}
begin { SignedFactor }
if Ch='-' then
begin
NextCh; SignedFactor:=-Factor;
end else SignedFactor:=Factor;
end { SignedFactor };
begin { Term }
T:=SignedFactor;
while Ch='^' do
begin
NextCh; t:=exp(ln(t)*SignedFactor);
end;
Term:=t;
end { Term };
begin { SimpleExpression }
s:=term;
while Ch in ['*','/'] do
begin
Opr:=Char(Ch); NextCh;
case Opr of
'*': s:=s*term;
'/': s:=s/term;
end;
end;
SimpleExpression:=s;
end { SimpleExpression };
begin { Expression }
E:=SimpleExpression;
while Ch in ['+','-'] do