-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOOP.PAS
executable file
·3486 lines (3279 loc) · 92.1 KB
/
OOP.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
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
unit OOP;
{$F+}
interface
uses Dos,Variable;
const
{ Node Types }
Stop = 1;
Assignment = 2;
Decision = 3;
Input = 4;
Output = 5;
Control = 6;
Start = 7;
ControlOut = 8;
{ Variable Types }
StrVar = 1;
NumVar = 2;
TabVar = 3;
NLVar = 4;
type
{ Variables }
PVariable = ^FVariable;
FVariable = object { Abstract object }
Name : string[13];
VarType : 0..4;
Next : PVariable;
Last : PVariable;
constructor Init;
procedure SetVar(VarName : string; LastVar : PVariable);
procedure List(ListType : byte; X,Y : byte);
function Find(VarName : string; FindType : byte) : PVariable;
procedure SaveAll(var OutFile : text); virtual;
procedure Save(var OutFile : text); virtual;
procedure Show; virtual;
function LoadBase(var InFile : text) : boolean;
function Load(var InFile : text) : boolean; virtual;
destructor Done; virtual;
end; { of object FVariable }
PNumVar = ^FNumVar;
FNumVar = object(FVariable)
Value : real;
constructor Init;
procedure SetVar(VarName : string; LastVar : PVariable; N : real);
procedure Show; virtual;
procedure Save(var OutFile : text); virtual;
function Load(var InFile : text) : boolean; virtual;
end; { of object FNumVar }
PStringVar = ^FStringVar;
FStringVar = object(FVariable)
value : string;
constructor Init;
procedure SetVar(VarName : string; LastVar : PVariable; S : string);
procedure Save(var OutFile : text); virtual;
procedure Show; virtual;
function Load(var InFile : text) : boolean; virtual;
end; { of object FStringVar }
PTabVar = ^FTabVar;
FTabVar = object(FVariable)
{ Used only as a constant }
TabWidth : integer;
constructor Init;
procedure SetVar(VarName : string; LastVar : PVariable; N : integer);
procedure Save(var OutFile : text); virtual;
function Load(var InFile : text) : boolean; virtual;
procedure Show; virtual;
end; { of object FTabVar }
PNLVar = ^FNLVar;
FNLVar = object(FVariable)
{ Used only as a constant }
constructor Init;
procedure Show; virtual;
end; { of object FNLVar }
{ Equations }
PTerm = ^FTerm;
FTerm = object
{ Linked list of variables or constants used in equations }
Next : PTerm;
Last : PTerm;
Operator : char;
Term : PVariable;
constructor Init;
procedure SetTerm(TermType : byte);
procedure GetStringTerm;
procedure GetNumTerm;
procedure GetVarTerm(VarType : byte);
procedure GetTab;
procedure GetNewLine;
procedure GetOperator;
procedure Show;
procedure CalcTerms(var TempTerm : PTerm);
procedure CalcPowers;
procedure CalcMultDiv;
procedure CalcAddSub;
function GetNumValue : real;
function GetStrValue : string;
procedure Save(var OutFile : text);
function Load(Var InFile : text) : boolean;
destructor Delete;
destructor Done;
end; { of object FTerm }
{ Nodes }
PNode = ^FNode;
FNode = object { Abstract object }
NodeType : 0..ControlOut;
NodePage : byte;
XPos, YPos : byte;
Comment : string;
Next : PNode;
CNext : PNode;
{ Temporary fields used in loading }
CNextX,CNextY,CNextPage : byte;
constructor Init;
function SetNode(X,Y : word) : boolean; virtual;
function ChangeNode(X,Y : word) : boolean; virtual;
procedure GetComment;
function Find(X,Y : word) : PNode;
function FindLast(X,Y : word) : PNode;
function FindCLast(X,Y : word) : PNode;
function FindFLast(X,Y : word) : PNode; virtual;
procedure Add(Node : PNode);
procedure ConnectTo(Node : PNode; side: Boolean); virtual;
procedure Copy(Node : PNode);
procedure NewPos(X,Y : word);
procedure Show; virtual;
procedure DrawAll; virtual;
procedure Draw; virtual;
procedure DrawLine(Side : boolean); virtual;
procedure run; virtual;
procedure SaveAll(var OutFile : text);
procedure Save(var OutFile : text); virtual;
function LoadBase(var InFile : text) : boolean; virtual;
function Load(var InFile : text) : boolean; virtual;
function ConnectAll : boolean; virtual;
destructor Delete; virtual;
destructor Done; virtual;
end; { of object FNode }
PEquationNode = ^FEquationNode;
FEquationNode = object(FNode) { Abstract object }
Value : PTerm;
Variable : PVariable;
Symbol : string[2];
constructor Init;
function GetVariable : boolean;
function GetNumValue : boolean;
function GetStrValue : boolean;
procedure Copy(Node : PEquationNode);
procedure Save(var OutFile : text); virtual;
function LoadBase(var InFile : text) : boolean; virtual;
function Load(var InFile : text) : boolean; virtual;
destructor Delete; virtual;
destructor Done; virtual;
end; { of object FEquationNode }
PStopNode = ^FStopNode;
FStopNode = object(FNode)
constructor Init;
function ChangeNode(X,Y : word) : boolean; virtual;
procedure Show; virtual;
procedure Run; virtual;
end; { of object FStopNode }
PAssignmentNode = ^FAssignmentnode;
FAssignmentNode = object(FEquationNode)
constructor Init;
function SetNode(X,Y : word) : boolean; virtual;
procedure Show; virtual;
procedure Run; virtual;
end; { of object FAssignmentNode }
PDecisionNode = ^FDecisionNode;
FDecisionNode = object(FEquationNode)
FNext : PNode;
{ Temporary fields used in loading }
FNextX,FNextY,FNextPage : byte;
constructor Init;
function SetNode(X,Y : word) : boolean; virtual;
function GetVariable : boolean;
procedure GetSymbol;
procedure Copy(Node : PDecisionNode);
procedure ConnectTo(Node : PNode; side: Boolean); virtual;
function FindFLast(X,Y : word) : PNode; virtual;
procedure Show; virtual;
procedure DrawAll; virtual;
procedure DrawLine(Side : boolean); virtual;
procedure run; virtual;
function RunNum : boolean;
function RunStr : boolean;
procedure Save(var OutFile : text); virtual;
function Load(var InFile : text) : boolean; virtual;
function ConnectAll : boolean; virtual;
end; { of object FDecisionNode }
PInputNode = ^FInputNode;
FInputNode = object(FNode)
Variable : PVariable;
constructor Init;
function SetNode(X,Y : word) : boolean; virtual;
function GetVariable : boolean;
procedure Copy(Node : PInputNode);
procedure Show; virtual;
procedure run; virtual;
procedure Save(var OutFile : text); virtual;
function Load(var InFile : text) : boolean; virtual;
end; { of object FInputNode }
POutputNode = ^FOutputNode;
FOutputNode = object(FEquationNode)
XCursor : shortint;
YCursor : shortint;
BackColour : shortint;
ForeColour : shortint;
constructor Init;
function SetNode(X,Y : word) : boolean; virtual;
procedure Getposition;
procedure GetColours;
procedure GetXCursor;
procedure GetYCursor;
procedure Copy(Node : POutputNode);
procedure Show; virtual;
procedure run; virtual;
procedure Save(var OutFile : text); virtual;
function Load(var InFile : text) : boolean; virtual;
end; { of object FOutputNode }
PControlOutNode = ^FControlOutNode;
PControlNode = ^FControlNode;
FControlNode = object(FNode)
OutNode : PControlOutNode;
Identifier : word;
{ Temporary fields used in loading and SetNode }
OutNodeX,OutNodeY,OutNodePage : byte;
constructor Init;
function SetNode(X,Y : word) : boolean; virtual;
function ChangeNode(X,Y : word) : boolean; virtual;
function GetOutLoc : boolean;
function GetOutPage : boolean;
function GetIdentifier : boolean;
procedure Copy(Node : PControlNode);
procedure Show; virtual;
procedure Draw; virtual;
procedure Run; virtual;
procedure Save(var OutFile : text); virtual;
function Load(var InFile : text) : boolean; virtual;
function ConnectAll : boolean; virtual;
destructor Delete; virtual;
end; { of object FControlNode }
PStartNode = ^FStartNode;
FStartNode = object(FNode)
constructor Init;
function ChangeNode(X,Y : word) : boolean; virtual;
procedure Show; virtual;
end; { of object FStartNode }
FControlOutNode = object(FNode)
InNode : PControlNode;
constructor Init;
function ChangeNode(X,Y : word) : boolean; virtual;
procedure Copy(Node : PControlOutNode);
procedure Show; virtual;
procedure Draw; virtual;
destructor Delete; virtual;
end; { of object FControlOutNode }
var
FirstVar : PVariable;
FirstNode : array[1..200] of PNode;
implementation
uses
Crt,Graph,Box,Convert,Mouse,MouseRS2,Util,Init,Misc,Menu;
{ Variables }
constructor FVariable.Init;
begin
Name := #0;
VarType := 0;
Next := nil;
Last := nil;
end; { of constructor FVariable.Init }
procedure FVariable.SetVar(VarName : string; LastVar : PVariable);
begin
Name := VarName;
if LastVar <> nil then begin { Insert into list }
Next := LastVar^.Next;
Last := LastVar;
LastVar^.Next := @Self;
end; { of if statement }
end; { of procedure FVariable.SetVar }
function FVariable.Find(VarName : string; FindType : byte) : PVariable;
var
TempVar : PVariable;
begin
if ((Name = VarName) or (VarName = #0)) { Name matches or is blank }
and ((VarType = FindType) or (FindType = 0)) then { Type matches or }
TempVar := @Self { no type necessary }
else
if Next <> nil then
TempVar := Next^.Find(VarName,FindType) { Keep searching list }
else
TempVar := nil; { Variable not found }
Find := TempVar;
end; { of function FVariable.Find }
procedure FVariable.List(ListType : byte; X,Y : byte);
begin
if (VarType = ListType) or (ListType = 0) then begin { Type matched or }
outtextXY(X,Y,Name); { no type necessary }
Y := Y + 14;
end; { of if statement }
if Next <> nil then Next^.List(ListType,X,Y); { Write remainder of list }
end; { of procedure FVariable.List }
procedure FVariable.Show;
var
tempbackground : pointer;
begin
if Name <> #0 then begin
if getx+(length(name)*8) > 631 then begin { If at edge of screen, }
if gety >= 118 then begin { enlarge box }
getmem(tempbackground,imagesize(0,topy,639,139));
getimage(0,topy,639,139,tempbackground^);
putimage(0,topy,outputsbackground^,normalput);
freemem(outputsbackground,imagesize(0,topy,639,139));
dec(topy,14);
getmem(outputsbackground,imagesize(0,topy,639,139));
getimage(0,topy,639,139,outputsbackground^);
boxit(0,topy,639,139,white,'');
putimage(0,topy,tempbackground^,normalput);
freemem(tempbackground,imagesize(0,topy+14,639,139));
clear(3,124,637,125,lightblue);
moveto(9,gety-14);
end; { of if statement }
moveto(9,gety+14);
end; { of if statement }
outtext(Name+' '); { Write name }
end; { of if statement }
end; { of procedure FVariable.Show }
procedure FVariable.SaveAll(var OutFile : text);
{ Save entire list of variables }
begin
Save(OutFile); { Save one variable }
writeln(OutFile);
if Next <> nil then Next^.SaveAll(OutFile); { Save remainder of list }
end; { of procedure FVariable.SaveAll }
procedure FVariable.Save(var OutFile : text);
{ Save one variable; each variable type redefines this procedure }
var
i : integer;
S : string;
begin
write(OutFile,VarType,' ');
S := leftjust(Name,12);
write(OutFile,S);
end; { of procedure FVariable.Save }
function FVariable.LoadBase(var InFile : text) : boolean;
{ Load portion of variable which never changes }
var
C : char;
Nm : string[12];
begin
LoadBase := False;
if Eoln(InFile) then exit; { Error occurred }
read(InFile,C);
if (C <> ' ') or Eoln(InFile) then exit; { Error occured }
read(InFile,Nm);
Name := NoRSpace(Nm);
LoadBase := True;
end; { of function FVariable.LoadBase }
function FVariable.Load(var InFile : text) : boolean;
{ Load one variable; each descendant will redefine }
var
LoadOK : boolean;
begin
Load := False;
LoadOK := LoadBase(InFile);
if not(LoadOK) or not(Eoln(InFile)) then exit; { Error occurred }
readln(InFile);
Load := True;
end; { of function FVariable.Load }
destructor FVariable.Done;
{ Dispose of entire list }
begin
if Next <> nil then dispose(Next,Done);
end; { of procedure FVariable.Done }
constructor FNumVar.Init;
begin
FVariable.Init;
VarType := NumVar;
Value := 0;
end; { of constructor FNumVar.Init }
procedure FNumVar.SetVar(VarName : string; LastVar : PVariable; N : real);
begin
FVariable.SetVar(VarName,LastVar);
Value := N;
end; { of procedure FNumVar.SetVar }
procedure FNumVar.Show;
var
tempbackground : pointer;
begin
FVariable.Show; { if variable has name, write it }
if Name = #0 then begin
if getx+(length(RtoS(Value,10,10)+' ')*8) > 631 then begin
if gety >= 118 then begin { if at edge of screen, enlarge box }
getmem(tempbackground,imagesize(0,topy,639,139));
getimage(0,topy,639,139,tempbackground^);
putimage(0,topy,outputsbackground^,normalput);
freemem(outputsbackground,imagesize(0,topy,639,139));
dec(topy,14);
getmem(outputsbackground,imagesize(0,topy,639,139));
getimage(0,topy,639,139,outputsbackground^);
boxit(0,topy,639,139,white,'');
putimage(0,topy,tempbackground^,normalput);
freemem(tempbackground,imagesize(0,topy+14,639,139));
clear(3,124,637,125,lightblue);
moveto(9,gety-14);
end; { of if statement }
moveto(9,gety+14);
end; { of if statement }
outtext(RtoS(Value,10,10)+' ');
end; { of if statement }
end; { of procedure FNumVar.Show }
procedure FNumVar.Save(var OutFile : text);
{ Save one variable; each variable type redefines this procedure }
begin
FVariable.Save(OutFile);
write(OutFile,Value);
end; { of procedure FNumVar.Save }
function FNumVar.Load(var InFile : text) : boolean;
{ Load one variable; each descendant will redefine }
var
LoadOK : boolean;
begin
Load := False;
LoadOK := LoadBase(InFile);
if not(LoadOK) or Eoln(InFile) then exit; { Error occurred }
read(InFile,Value);
if (IOResult <> 0) or not(Eoln(InFile)) then exit; { Error occurred }
readln(InFile);
Load := True;
end; { of function FNumVar.Load }
constructor FStringVar.Init;
begin
FVariable.Init;
Vartype := StrVar;
Value := #0;
end; { of constructor FStringVar.Init }
procedure FStringVar.SetVar(VarName : string; LastVar : PVariable;
S : string);
begin
FVariable.SetVar(VarName,LastVar);
Value := S;
end; { of procedure FStringVar.SetVar }
procedure FStringVar.Show;
var
tempbackground : pointer;
begin
FVariable.Show;
if Name = #0 then begin { if variable has name, write it }
if getx+(length('"'+Value+'" ')*8) > 631 then begin
if gety >= 118 then begin { if at edge of screen, enlarge box }
getmem(tempbackground,imagesize(0,topy,639,139));
getimage(0,topy,639,139,tempbackground^);
putimage(0,topy,outputsbackground^,normalput);
freemem(outputsbackground,imagesize(0,topy,639,139));
dec(topy,14);
getmem(outputsbackground,imagesize(0,topy,639,139));
getimage(0,topy,639,139,outputsbackground^);
boxit(0,topy,639,139,white,'');
putimage(0,topy,tempbackground^,normalput);
freemem(tempbackground,imagesize(0,topy+14,639,139));
clear(3,124,637,125,lightblue);
moveto(9,gety-14);
end; { of if statement }
moveto(9,gety+14);
end; { of if statement }
outtext('"'+Value+'" ');
end; { of if statement }
end; { of procedure FStringVar.Show }
procedure FStringVar.Save(var OutFile : text);
{ Save one variable; each variable type redefines this procedure }
begin
FVariable.Save(OutFile);
write(OutFile,Value);
end; { of procedure FStringVar.Save }
function FStringVar.Load(var InFile : text) : boolean;
{ Load one variable; each descendant will redefine }
var
LoadOK : boolean;
begin
Load := False;
LoadOK := LoadBase(InFile);
if not(LoadOK) or Eoln(InFile) then exit; { Error occurred }
read(InFile,Value);
if not(Eoln(InFile)) then exit; { Error occurred }
readln(InFile);
Load := True;
end; { of function FStringVar.Load }
constructor FTabVar.Init;
begin
FVariable.Init;
TabWidth := 0;
vartype := TabVar;
end; { of constructor FTabVar.Init }
procedure FTabVar.SetVar(VarName : string; LastVar : PVariable; N : integer);
begin
FVariable.SetVar(VarName,LastVar);
TabWidth := N;
end; { of procedure FTabVar.SetVar }
procedure FTabVar.Show;
var
tempbackground : pointer;
begin
FVariable.Show;
if Name = #0 then begin { if variable has name, write it }
if getx+(length('Tab('+ItoS(tabwidth)+') ')*8) > 631 then begin
if gety >= 118 then begin { if at edge of screen, enlarge box }
getmem(tempbackground,imagesize(0,topy,639,139));
getimage(0,topy,639,139,tempbackground^);
putimage(0,topy,outputsbackground^,normalput);
freemem(outputsbackground,imagesize(0,topy,639,139));
dec(topy,14);
getmem(outputsbackground,imagesize(0,topy,639,139));
getimage(0,topy,639,139,outputsbackground^);
boxit(0,topy,639,139,white,'');
putimage(0,topy,tempbackground^,normalput);
freemem(tempbackground,imagesize(0,topy+14,639,139));
clear(3,124,637,125,lightblue);
moveto(9,gety-14);
end; { of if statement }
moveto(9,gety+14);
end; { of if statement }
outtext('Tab('+ItoS(TabWidth)+') ');
end; { of if statement }
end; { of procedure FTabVar.Show }
procedure FTabVar.Save(var OutFile : text);
{ Save one variable; each variable type redefines this procedure }
begin
FVariable.Save(OutFile);
write(OutFile,TabWidth);
end; { of procedure FTabVar.Save }
function FTabVar.Load(var InFile : text) : boolean;
{ Load one variable; each descendant will redefine }
var
LoadOK : boolean;
begin
Load := False;
LoadOk := LoadBase(InFile);
if not(LoadOK) or Eoln(InFile) then exit; { Error occurred }
read(InFile,TabWidth);
if (IOResult <> 0) or not(Eoln(InFile)) then exit; { Error occurred }
readln(InFile);
Load := True;
end; { of function FTabVar.Load }
constructor FNLVar.Init;
begin
FVariable.Init;
VarType := NLVar;
end; { of constructor FNLVar.Init }
procedure FNLVar.Show;
var
tempbackground : pointer;
begin
FVariable.Show;
if Name = #0 then begin { if variable has name, write it }
if getx+(length('NL ')*8) > 631 then begin
if gety >= 118 then begin { if at edge of screen, enlarge box }
getmem(tempbackground,imagesize(0,topy,639,139));
getimage(0,topy,639,139,tempbackground^);
putimage(0,topy,outputsbackground^,normalput);
freemem(outputsbackground,imagesize(0,topy,639,139));
dec(topy,14);
getmem(outputsbackground,imagesize(0,topy,639,139));
getimage(0,topy,639,139,outputsbackground^);
boxit(0,topy,639,139,white,'');
putimage(0,topy,tempbackground^,normalput);
freemem(tempbackground,imagesize(0,topy+14,639,139));
clear(3,124,637,125,lightblue);
moveto(9,gety-14);
end; { of if statement }
moveto(9,gety+14);
end; { of if statement }
outtext('NL ');
end; { of if statement }
end; { of procedure FNLVar.Show }
{ Equations }
constructor FTerm.Init;
begin
Next := nil;
Last := nil;
Operator := #0;
Term := nil;
end;
procedure FTerm.SetTerm(TermType : byte);
{ TermType : 1 - String Constant,
2 - Numeric Constant,
3 - String Variable,
4 - Numeric Variable,
5 - Untyped Variable,
6 - Tab,
7 - New Line }
var
Cancel : boolean;
begin
case TermType of
1 : GetStringTerm;
2 : GetNumTerm;
3 : GetVarTerm(StrVar);
4 : GetVarTerm(NumVar);
5 : GetVarTerm(0);
6 : GetTab;
7 : GetNewLine;
end;
end;
procedure FTerm.GetStringTerm;
const
Temp : PStringVar = nil;
S : string = #0;
Background : pointer = nil;
CancelBox : array[1..1,1..4] of integer = ((8,25,197,210));
begin
Button := NewButton;
GetMem(Background,ImageSize(0,197,639,244)); { save window background }
GetImage(0,197,639,244,Background^);
boxit(8,197,631,244,white,'String : '); { open window }
SetColor(lightgray);
InputIt(17,223,s,75,@CancelBox); { read string from kbd }
PutImage(0,197,Background^,NormalPut); { close window }
Freemem(Background,ImageSize(0,197,639,244));
if s = '' then s := #27; { check for cancel }
if s <> #27 then begin { if not cancel then }
Temp := new(PStringVar,Init); { create variable }
Temp^.SetVar(#0,nil,S);
Term := Temp;
end;
end;
procedure Fterm.GetTab;
var
Background : pointer;
temp : string;
go : boolean;
tempterm : Ptabvar;
Column,
Code : integer;
const
CancelBox : array[1..1,1..4] of integer = ((249,262,155,172));
begin
Button := NewButton;
GetMem(Background,ImageSize(249,155,391,195)); { save window background }
GetImage(249,155,391,195,Background^);
temp := '';
repeat
go := false;
boxit(249,155,391,195,white,'Column'); { open window }
PrintXY(258,174,'New column > ',yellow);
SetColor(lightgray);
InputIt(361,174,temp,2,@CancelBox); { read tab width from kbd }
If Mon then MouseCursorOff(Mx,My);
if temp = '' then temp := #27; { check for cancel }
if temp <> #27 then begin
val(temp,column,code);
if (column < 1) or (column > 80) or (code <> 0) then
MouseMsg(200,200,'Invalid column!') { check if within bounds }
else
go := True;
end;
until go or (temp = #27); { repeat until valid response or cancel }
PutImage(249,155,Background^,NormalPut); { close window }
Freemem(Background,ImageSize(249,155,391,195));
if temp <> #27 then begin { if not cancel then }
Tempterm := new(PtabVar,Init); { create new variable }
Tempterm^.SetVar(#0,nil,RtoI(GetVal(temp)));
term := Tempterm;
end;
end;
procedure FTerm.GetNewLine;
begin
Term := new(PNLVar,Init);
end;
procedure FTerm.GetNumTerm;
var
TempNumVar : PNumVar;
temp : string;
N : real;
go : boolean;
Code : integer;
background : pointer;
const
CancelBox : array[1..1,1..4] of integer = ((201,214,155,172));
begin
Button := NewButton;
MouseCursorOff(Mx,My);
GetMem(Background,ImageSize(201,155,439,195)); { save window background }
GetImage(201,155,439,195,Background^);
temp := '';
repeat
go := false;
boxit(201,155,439,195,white,'Number'); { open window }
PrintXY(210,174,'Enter a number > ',yellow);
SetColor(lightgray);
InputIt(348,174,temp,10,@CancelBox); { read number from keyboard }
if temp = '' then temp := #27; { check for cancel }
if temp <> #27 then begin
val(temp,N,Code);
if Code <> 0 then { check for valid response }
MouseMsg(200,200,'Invalid number!')
else
go := true;
end;
until go or (temp = #27);
MouseCursorOff(Mx,My);
PutImage(201,155,Background^,NormalPut); { close window }
Freemem(Background,ImageSize(201,155,439,195));
MouseCursorOn(Mx,My,Arrow);
if temp <> #27 then begin { if not cancel then }
TempNumVar := new(PNumVar,Init); { create new variable }
TempNumVar^.SetVar(#0,nil,N);
Term := TempNumVar;
end;
end;
procedure FTerm.GetVarTerm(VarType : byte);
var
VarName : string;
Cancel : boolean;
begin
repeat
GetFileType := 1; { Pick variable from list }
VarName := MGetFile(ItoS(VarType),'Variable Name',Cancel);
GetFileType := 0;
term := FirstVar^.Find(VarName,VarType); { Get pointer to variable }
if (term = nil) and not(Cancel) then
mousemsg(200,200,'Invalid Variable');
until (term <> nil) or Cancel;
end;
procedure FTerm.GetOperator;
var
temp : string[1];
const
opset : array [1..6] of string = ('+','-','*','/','^',#0);
begin
Temp := OpSet[MouseQuestion(5,1,'Operator:',@Opset)];
Operator := Temp[1];
end;
procedure FTerm.Show;
var tempbackground : pointer;
begin
if Term <> nil then Term^.Show; { show variable or constant }
if Operator <> #0 then begin
if getx+(length(operator+' ')*8) > 631 then begin
if gety >= 118 then begin { if at edge of screen then expand box }
getmem(tempbackground,imagesize(0,topy,639,139));
getimage(0,topy,639,139,tempbackground^);
putimage(0,topy,outputsbackground^,normalput);
freemem(outputsbackground,imagesize(0,topy,639,139));
dec(topy,14);
getmem(outputsbackground,imagesize(0,topy,639,139));
getimage(0,topy,639,139,outputsbackground^);
boxit(0,topy,639,139,white,'');
putimage(0,topy,tempbackground^,normalput);
freemem(tempbackground,imagesize(0,topy+14,639,139));
clear(3,124,637,125,lightblue);
moveto(9,gety-14);
end;
moveto(9,gety+14);
end;
outtext(Operator+' '); { show operator }
if next <> nil then Next^.Show; { show rest of terms }
end;
end;
procedure FTerm.CalcTerms(var TempTerm : PTerm);
{ replace actual list of terms with temporary list, replacing all variable
references with constants having the same value }
var
Temp : PVariable;
begin
TempTerm^.Operator := Operator; { copy operator to new list }
TempTerm^.Term := new(PNumVar,Init); { create constant in new list }
Temp := TempTerm^.Term; { copy value of constant or var }
PNumVar(Temp)^.Value := PNumVar(Term)^.Value; { to new list }
if Next = nil then
TempTerm^.Next := nil { end new list }
else begin
TempTerm^.Next := new(PTerm,Init); { create new term in new list }
Next^.CalcTerms(TempTerm^.Next);
end;
end;
procedure FTerm.CalcPowers;
{ calculate all powers in list of terms, replacing each pair of terms with
a single term containing the result of the operation }
var
Finished : boolean;
Temp : PVariable;
TempNext : PTerm;
begin
Finished := False;
repeat
case Operator of
'^' : begin { calculate result and put it in replacement term }
Temp := Next^.Term;
PNumVar(Term)^.Value :=
Pwr( PNumVar(Term)^.Value, RtoI( PNumVar(Temp)^.Value ) );
Operator := Next^.Operator;
TempNext := Next^.Next;
dispose(Next,Delete);
Next := TempNext;
if Next <> nil then
Next^.Last := @Self;
end;
#0 : exit; { end of list }
else Finished := True; { next term is not a power }
end;
until Finished; { repeat until term that isn't power is reached }
Next^.CalcPowers; { continue until another sequence of powers is hit }
end;
procedure FTerm.CalcMultDiv;
{ calculate all mult. and div. in list of terms, replacing each pair of terms
with a single term containing the result of the operation }
var
Finished : boolean;
Temp : PVariable;
TempNext : PTerm;
begin
Finished := False;
repeat
case Operator of
'*' : begin { calculate result and put it in replacement term }
Temp := Next^.Term;
PNumVar(Term)^.Value := PNumVar(Term)^.Value *
PNumVar(Temp)^.Value;
Operator := Next^.Operator;
TempNext := Next^.Next;
dispose(Next,Delete);
Next := TempNext;
if Next <> nil then
Next^.Last := @Self;
end;
'/' : begin { calculate result and put it in replacement term }
Temp := Next^.Term;
if PNumVar(Temp)^.Value = 0 then begin
writeln('Error : Division by Zero');
halt(1);
end
else begin
PNumVar(Term)^.Value := PNumVar(Term)^.Value /
PNumVar(Temp)^.Value;
Operator := Next^.Operator;
TempNext := Next^.Next;
dispose(Next,Delete);
Next := TempNext;
if Next <> nil then
Next^.Last := @Self;
end;
end;
#0 : exit; { end of list }
else Finished := True;
end;
until Finished; { repeat until term that isn't mult or div is reached }
Next^.CalcMultDiv; { continue until next mult or div term is reached }
end;
procedure FTerm.CalcAddSub;
{ calculate all add. and sub. in list of terms, replacing each pair of terms
with a single term containing the result of the operation }
var
Finished : boolean;
TempNext : PTerm;
Temp : PVariable;
begin
Finished := False;
repeat
case Operator of
'+' : begin { calculate result and put it in replacement term }
Temp := Next^.Term;
PNumVar(Term)^.Value := PNumVar(Term)^.Value +
PNumVar(Temp)^.Value;
Operator := Next^.Operator;
TempNext := Next^.Next;
dispose(Next,Delete);
Next := TempNext;
if Next <> nil then
Next^.Last := @Self;
end;
'-' : begin { calculate result and put it in replacement term }
Temp := Next^.Term;
PNumVar(Term)^.Value := PNumVar(Term)^.Value -
PNumVar(Temp)^.Value;
Operator := Next^.Operator;
TempNext := Next^.Next;
dispose(Next,Delete);
Next := TempNext;
if Next <> nil then
Next^.Last := @Self;
end;
#0 : exit; { end of list }
else Finished := True;
end;
until Finished; { repeat until term that isn't add or subt is reached }
Next^.CalcPowers; { continue until next add or subt is reached }
end;
function FTerm.GetNumValue : real;
{ find result of numeric equation, using standard order of operations }
var
TempTerm : PTerm;
TempVal : PVariable;
begin
TempTerm := new(PTerm,Init); { create temporary list }
CalcTerms(TempTerm); { copy terms into temporary list }
TempTerm^.CalcPowers; { take all powers out of list }
TempTerm^.CalcMultDiv; { take all mult. and div. out of list }
TempTerm^.CalcAddSub; { take all add. and subt. out of list }
TempVal := TempTerm^.Term; { remaining term is value }
GetNumValue := PNumVar(TempVal)^.Value;
dispose(TempTerm,Done); { dispose of temporary list }
end;
function FTerm.GetStrValue : string;
{ find result of string equation }
var
Temp : string;
begin
Temp := '';
with Term^ do
case VarType of { add current term to string }
StrVar : Temp := Temp + PStringVar(Term)^.Value;
NumVar : Temp := Temp + RtoS(PNumVar(Term)^.Value,10,10);
3 : Temp := Temp + StringOf(PTabVar(Term)^.TabWidth,' ');
4 : Temp := Temp + #10 + #13;
end;
if Operator <> #0 then Temp := Temp + Next^.GetStrValue;
GetStrValue := Temp; { add result of rest of terms to string }
end;
procedure FTerm.Save(var OutFile : text);
begin
if Term^.Name <> #0 then begin { if variable, write -1 and name }
write(OutFile,-1,' ');
write(OutFile,LeftJust(Term^.Name,12));
end
else begin { otherwise save entire variable }
Term^.Save(OutFile);
writeln(OutFile);
end;