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
|
unit matheunit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Gmp, Math, lowlevelunit, protokollunit;
type
tCallBackGetValue = function(name: string): extended of object;
tExprToFloat = function(syntaxtest: boolean; name: string): extended of object;
tKnownValue = record
name: string;
value: extended;
end;
tKnownValues = class
private
chef: tKnownValues;
knechte: array of tKnownValues;
kvs: array of tKnownValue;
function finde(nam: string): longint; inline;
public
constructor create(chf: tKnownValues);
constructor createFromOriginal(original: tKnownValues);
destructor destroy; override;
procedure add(val: tKnownValue); inline; overload;
procedure add(nam: string; val: extended); overload;
function rem(nam: string): boolean;
function extract(nam: string; out val: extended): boolean; inline;
procedure dump(prot: tProtokollant; prefix: string);
procedure neuerKnecht(knecht: tKnownValues);
procedure entferneKnecht(knecht: tKnownValues);
end;
function plus(a,b: tExtPoint): tExtPoint;
function durch(a: tExtPoint; b: extended): tExtPoint;
function myFrac(x: extended): extended;
function mpfToStr(f: mpf_t): string;
function signSqr(x: extended): extended; inline;
function mpfMyRoot(rad: mpf_t; wzlExp: int64): extended;
function myTimeToStr(t: extended): string;
function cmpStr(s1,s2: string): longint;
function mitte(s1,s2: string): string;
function myFloatToStr(x: extended): string;
function myStrToFloat(s: string): extended;
function intToFixpoint(x,stellen: longint): string;
function floatToFixpoint(x: extended; vk,nk: longint): string;
function istGanzZahl(s: string): boolean;
procedure copyArray(i: tExtPointArray; out o: tExtPointArray); overload;
procedure copyArray(i: tLongintArray; out o: tLongintArray); overload;
procedure copyArray(i: tExtendedArray; out o: tExtendedArray); overload;
function nullFunktion(x: extended): extended;
function exprToFloat(st: boolean; s: string; kvs: tKnownValues; cbgv: tCallBackGetValue): extended;
function exprToBool(st: boolean; s: string; kvs: tKnownValues; cbgv: tCallBackGetValue): boolean;
function formelnAuswerten(s: string; kvs: tKnownValues; cbgv: tCallBackGetValue): string;
function knownValue(nam: string; val: extended): tKnownValue;
function berechneEinheitsZelle(invarianz,modulus: tInt64Point): tInt64Point;
function ggT(a,b: int64): int64;
function ermittleAnstieg(dat: string; xSpalte,ySpalte: longestOrdinal): extended;
implementation
// tKnownValueArray ************************************************************
constructor tKnownValues.create(chf: tKnownValues);
begin
inherited create;
fillchar(kvs,sizeOf(kvs),#0);
fillchar(knechte,sizeOf(knechte),#0);
add('π',pi);
chef:=chf;
if assigned(chef) then
chef.neuerKnecht(self);
setLength(knechte,0);
end;
constructor tKnownValues.createFromOriginal(original: tKnownValues);
var
i: longint;
begin
inherited create;
fillchar(kvs,sizeOf(kvs),#0);
fillchar(knechte,sizeOf(knechte),#0);
for i:=0 to length(original.kvs)-1 do
add(original.kvs[i]);
chef:=original.chef;
if assigned(chef) then
chef.neuerKnecht(self);
end;
destructor tKnownValues.destroy;
var
i: longint;
begin
setLength(kvs,0);
for i:=0 to length(knechte)-1 do begin
knechte[i].chef:=chef;
if assigned(chef) then
chef.neuerKnecht(knechte[i]);
end;
setLength(knechte,0);
if assigned(chef) then begin
chef.entferneKnecht(self);
chef:=nil;
end;
inherited destroy;
end;
function tKnownValues.finde(nam: string): longint;
var
i: longint;
begin
result:=-1;
for i:=0 to length(kvs)-1 do
if kvs[i].name=nam then begin
result:=i;
exit;
end;
end;
procedure tKnownValues.add(val: tKnownValue);
begin
add(val.name,val.value);
end;
procedure tKnownValues.add(nam: string; val: extended);
var
i: longint;
begin
i:=finde(nam);
if i<0 then begin
i:=length(kvs);
setLength(kvs,length(kvs)+1);
kvs[i].name:=nam;
end;
kvs[i].value:=val;
end;
function tKnownValues.rem(nam: string): boolean;
var
i: longint;
begin
i:=finde(nam);
result:=i>=0;
if result then begin
while i<length(kvs)-1 do begin
kvs[i].name:=kvs[i+1].name;
kvs[i].value:=kvs[i+1].value;
inc(i);
end;
setLength(kvs,length(kvs)-1);
end;
end;
function tKnownValues.extract(nam: string; out val: extended): boolean;
var
i: longint;
begin
i:=finde(nam);
result:=i>=0;
if result then
val:=kvs[i].value
else
if assigned(chef) then
result:=chef.extract(nam,val);
end;
procedure tKnownValues.dump(prot: tProtokollant; prefix: string);
var
i: longint;
begin
for i:=0 to length(kvs)-1 do
prot.schreibe(prefix+kvs[i].name+' = '+floattostr(kvs[i].value));
if assigned(chef) then
chef.dump(prot,prefix+' chef: ');
end;
procedure tKnownValues.neuerKnecht(knecht: tKnownValues);
var
i: longint;
begin
for i:=0 to length(knechte)-1 do
if knechte[i]=knecht then
fehler('tKnownValues soll einen Knecht hinzufügen, den es schon hat!');
setLength(knechte,length(knechte)+1);
knechte[length(knechte)-1]:=knecht;
end;
procedure tKnownValues.entferneKnecht(knecht: tKnownValues);
var
i: longint;
begin
i:=0;
while i<length(knechte) do begin
if knechte[i]=knecht then begin
while i<length(knechte)-1 do begin
knechte[i]:=knechte[i+1];
inc(i);
end;
setLength(knechte,length(knechte)-1);
exit;
end;
inc(i);
end;
fehler('tKnownValues soll einen Knecht entfernen, den es nicht hat!');
end;
// allgemeine Funktionen *******************************************************
function plus(a,b: tExtPoint): tExtPoint;
var
c: char;
begin
for c:='x' to 'y' do
result[c]:=a[c]+b[c];
end;
function durch(a: tExtPoint; b: extended): tExtPoint;
var
c: char;
begin
for c:='x' to 'y' do
result[c]:=a[c]/b;
end;
function myFrac(x: extended): extended;
begin
result:=frac(x);
while result<0 do
result:=result+1;
end;
function mpfToStr(f: mpf_t): string;
var
ex: mp_exp_t;
off: byte;
begin
result:=mpf_get_str(nil,ex,10,0,f);
off:=1+byte(pos('-',result)=1);
if result='' then
result:='0'
else if ex=1 then
result:=copy(result,1,off)+','+copy(result,off+1,length(result)-off)
else
result:=copy(result,1,off)+','+copy(result,off+1,length(result)-off)+' * 10^'+intToStr(ex-1);
end;
function signSqr(x: extended): extended;
begin
result:=sign(x)*sqr(x);
end;
function mpfMyRoot(rad: mpf_t; wzlExp: int64): extended;
var
ex: mp_exp_t;
begin
result:=power(mpf_get_d_2exp(ex,rad),1/wzlExp);
result:=result*power(2,ex/wzlExp);
end;
function myTimeToStr(t: extended): string;
var
tim: int64;
begin
tim:=floor(t*24*60*60);
result:=intToStr(tim mod 10)+'s';
tim:=tim div 10;
if tim=0 then exit;
result:=intToStr(tim mod 6)+result;
tim:=tim div 6;
if tim=0 then exit;
result:=intToStr(tim mod 10)+'min '+result;
tim:=tim div 10;
if tim=0 then exit;
result:=intToStr(tim mod 6)+result;
tim:=tim div 6;
if tim=0 then exit;
result:=intToStr(tim mod 24)+'h '+result;
tim:=tim div 24;
if tim=0 then exit;
result:=' '+result;
if (tim mod 7)<>1 then
result:='e'+result;
result:=intToStr(tim mod 7)+'Tag'+result;
tim:=tim div 7;
if tim=0 then exit;
result:=' '+result;
if tim<>1 then
result:='n'+result;
result:=intToStr(tim)+'Woche'+result;
end;
function cmpStr(s1,s2: string): longint;
var
i: longint;
begin
for i:=1 to min(length(s1),length(s2)) do
if s1[i]<>s2[i] then begin
result:=2*byte(s1[i]>s2[i])-1;
exit;
end;
if length(s1)<>length(s2) then begin
result:=2*byte(length(s1)>length(s2))-1;
exit;
end;
result:=0;
end;
function mitte(s1,s2: string): string;
var
i: longint;
w,nw: word;
begin
setLength(result,max(length(s1),length(s2)));
w:=0;
for i:=length(result) downto 1 do begin // result:= "s1+s2";
if i<=length(s1) then
w:=w+byte(s1[i]);
if i<=length(s2) then
w:=w+byte(s2[i]);
result[i]:=char(w and $ff);
w:=w shr 8;
end;
result:=char(w)+result;
w:=0;
for i:=1 to length(result) do begin
nw:=byte(odd(byte(result[i])+w));
result[i]:=char((byte(result[i])+w) div 2);
w:=nw shl 8;
end;
if w<>0 then
result:=result+char(w div 2);
if result[1]<>#0 then begin
writeln('Fehler bei der Mittenfindeung!');
halt;
end;
delete(result,1,1);
end;
function myFloatToStr(x: extended): string;
var
i,e: longint;
begin
e:=0;
if x<0 then begin
result:='-';
x:=-x;
end
else
result:='';
if x=0 then begin
result:='0';
exit;
end;
while x<1 do begin
dec(e);
x:=x*10;
end;
while x>=10 do begin
inc(e);
x:=x/10;
end;
result:=result+char(ord('0')+floor(x))+'.';
for i:=0 to 20 do begin
x:=(x-floor(x))*10;
result:=result+char(ord('0')+floor(x));
end;
if e<>0 then
result:=result+'E'+intToStr(e);
end;
function myStrToFloat(s: string): extended;
var
i,e: longint;
neg: boolean;
begin
if pos('E',s)>0 then begin
e:=strToInt(rightStr(s,length(s)-pos('E',s)));
delete(s,pos('E',s),length(s));
end
else e:=0;
if pos('.',s)=0 then begin
result:=strToInt(s)*power(10,e);
exit;
end;
neg:=leftStr(s,1)='-';
if neg then
delete(s,1,1);
if pos('.',s)=2 then begin
result:=0;
for i:=length(s) downto 3 do
result:=result/10 + ord(s[i])-ord('0');
result:=result/10 + ord(s[1])-ord('0');
end
else result:=strToFloat(s);
result:=result*power(10,e);
if neg then
result:=-result;
end;
function intToFixpoint(x,stellen: longint): string;
var
neg: boolean;
begin
neg:=x<0;
if neg then
x:=-x;
result:=intToStr(x);
while length(result)+byte(neg)<stellen do
result:='0'+result;
if neg then
result:='-'+result;
end;
function floatToFixpoint(x: extended; vk,nk: longint): string;
begin
result:=intToFixpoint(round(x*power(10,nk)),vk+nk);
result:=leftStr(result,length(result)-nk)+'.'+rightStr(result,nk);
end;
function istGanzZahl(s: string): boolean;
var
i: longint;
begin
result:=length(s)>0;
result:=result and (length(s) > byte(s[1] in ['-','+']));
if result then
for i:=byte(s[1] in ['-','+'])+1 to length(s) do
result:=result and (s[i] in ['0'..'9']);
end;
procedure copyArray(i: tExtPointArray; out o: tExtPointArray);
var
j: longint;
c: char;
begin
setLength(o,length(i));
for j:=0 to length(o)-1 do
for c:='x' to 'y' do
o[j,c]:=i[j,c];
end;
procedure copyArray(i: tLongintArray; out o: tLongintArray);
var
j: longint;
begin
setLength(o,length(i));
for j:=0 to length(o)-1 do
o[j]:=i[j];
end;
procedure copyArray(i: tExtendedArray; out o: tExtendedArray);
var
j: longint;
begin
setLength(o,length(i));
for j:=0 to length(o)-1 do
o[j]:=i[j];
end;
function nullFunktion(x: extended): extended;
begin
result:=0*x;
end;
function exprToFloat(st: boolean; s: string; kvs: tKnownValues; cbgv: tCallBackGetValue): extended;
var
i,j,k,l,m: longint;
inv,neg,cbv: boolean;
val1,val2: extended;
const
fkt1: array[0..9] of string = ('exp','sin','cos','tan','sqr','sqrt','ln','round','floor','ceil');
fkt2: array[0..1] of string = ('min','max');
begin
s:=trimAll(s);
for i:=0 to length(fkt1)-1 do
while fktPos(fkt1[i],s)>0 do begin
j:=fktPos(fkt1[i],s)+length(fkt1[i]);
while (j<=length(s)) and (s[j]<>'(') do
inc(j);
m:=j+1; // erstes Zeichen innerhalb der Klammern
k:=1;
while (j<length(s)) and (k>0) do begin
inc(j);
case s[j] of
'(': inc(k);
')': dec(k);
end;
end;
k:=fktPos(fkt1[i],s); // erstes Zeichen des Funktionsnamens
val1:=exprToFloat(st,copy(s,m,j-m),kvs,cbgv);
case i of
0: val1:=exp(val1);
1: val1:=sin(val1);
2: val1:=cos(val1);
3: val1:=tan(val1);
4: val1:=sqr(val1);
5: val1:=sqrt(val1);
6: val1:=ln(val1);
7: val1:=round(val1);
8: val1:=floor(val1);
9: val1:=ceil(val1);
end{of case};
s:=copy(s,1,k-1) + floattostr(val1) + copy(s,j+1,length(s));
end;
for i:=0 to length(fkt2)-1 do
while fktPos(fkt2[i],s)>0 do begin
j:=fktPos(fkt2[i],s)+length(fkt2[i]);
while (j<=length(s)) and (s[j]<>'(') do
inc(j);
m:=j+1; // erstes Zeichen innerhalb der Klammern
k:=1;
l:=-1;
while (j<length(s)) and (k>0) do begin
if (k=1) and (s[j] in [',',';']) then
l:=j; // das Komma/Semikolon
inc(j);
case s[j] of
'(': inc(k);
')': dec(k);
end;
end;
k:=fktPos(fkt1[i],s); // erstes Zeichen des Funktionsnamens
val1:=exprToFloat(st,copy(s,m,l-m),kvs,cbgv);
val2:=exprToFloat(st,copy(s,l+1,j-l-1),kvs,cbgv);
case i of
0: val1:=min(val1,val2);
1: val1:=max(val1,val2);
end{of case};
s:=copy(s,1,k-1) + floattostr(val1) + copy(s,j+1,length(s));
end;
while pos('(',s)>0 do begin
i:=pos('(',s);
j:=1;
while j>0 do begin
inc(i);
case s[i] of
'(': inc(j);
')': dec(j);
end;
end;
s:=copy(s,1,pos('(',s)-1)+
floattostr(exprToFloat(st,copy(s,pos('(',s)+1,i-pos('(',s)-1),kvs,cbgv))+
copy(s,i+1,length(s)-i);
end;
if assigned(cbgv) then
while pos('[',s)>0 do begin
i:=pos('[',s)-1; // i wird Index des letzten Zeichens vor "Werte[bla].blubb" o. ä.
while (i>0) and not (s[i] in ['+','-','*','/','^']) do
dec(i);
j:=pos(']',s)+1; // j wird Index des ersten Zeichens nach "Werte[bla].blubb" o. ä.
while (j<=length(s)) and not (s[j] in ['+','-','*','/','^']) do
inc(j);
s:=copy(s,1,i)+ // vorher
floattostr(cbgv(copy(s,i+1,j-i-1)))+ // zwischen
copy(s,j,length(s)-j+1); // nachher
end;
if (binOpPos('+',s)>0) or (binOpPos('-',s)>0) then begin
result:=0;
inv:=false;
repeat
i:=min(binOpPos('+',s),binOpPos('-',s));
if i=0 then i:=max(binOpPos('+',s),binOpPos('-',s));
if i=0 then i:=length(s)+1;
if inv then result:=result-exprToFloat(st,copy(s,1,i-1),kvs,cbgv)
else result:=result+exprToFloat(st,copy(s,1,i-1),kvs,cbgv);
inv:=s[i-byte(i>length(s))]='-';
delete(s,1,i);
until s='';
exit;
end;
if (binOpPos('*',s)>0) or (binOpPos('/',s)>0) then begin
result:=1;
inv:=false;
repeat
i:=min(binOpPos('*',s),binOpPos('/',s));
if i=0 then i:=max(binOpPos('*',s),binOpPos('/',s));
if i=0 then i:=length(s)+1;
if inv then result:=result/exprToFloat(st,copy(s,1,i-1),kvs,cbgv)
else result:=result*exprToFloat(st,copy(s,1,i-1),kvs,cbgv);
inv:=s[i-byte(i>length(s))]='/';
delete(s,1,i);
until s='';
exit;
end;
if binOpPos('^',s)>0 then begin
i:=binOpPos('^',s);
result:=power(exprToFloat(st,copy(s,1,i-1),kvs,cbgv),
exprToFloat(st,copy(s,i+1,length(s)-i),kvs,cbgv));
exit
end;
neg:=startetMit('-',s);
cbv:=false;
for i:=1 to length(s) do
cbv:=cbv or not (s[i] in ['-','0'..'9','.',',','e','E']);
if not cbv then result:=strToFloat(s)
else if not (assigned(kvs) and kvs.extract(s,result)) then begin
if st then result:=1
else if assigned(cbgv) then result:=cbgv(s)
else begin
result:=nan;
fehler(''''+s+''' kann ich nicht auflösen!');
end;
end;
result:=result*(2*byte(not neg)-1);
end;
function simpleExprToBool(st: boolean; s: string; kvs: tKnownValues; cbgv: tCallBackGetValue): boolean;
var
i: longint;
t: string;
const
binOps: array[0..12] of string =
('<=','>=','<>','≤','≥','=','≠','<','>',' in ','∈',' notIn ','∉');
begin
s:=trim(s);
while (leftStr(s,1)='(') and (rightStr(s,1)=')') do
s:=trim(copy(s,2,length(s)-2));
if uppercase(s)='TRUE' then begin
result:=true;
exit;
end;
if uppercase(s)='FALSE' then begin
result:=false;
exit;
end;
for i:=0 to length(binOps)-1 do
if pos(binOps[i],s)>0 then begin
t:=erstesArgument(s,binOps[i]);
case binOps[i] of
'≤','<=': result:=exprToFloat(st,t,kvs,cbgv)<=exprToFloat(st,s,kvs,cbgv);
'≥','>=': result:=exprToFloat(st,t,kvs,cbgv)>=exprToFloat(st,s,kvs,cbgv);
'=':
try
result:=exprToFloat(st,t,kvs,cbgv)=exprToFloat(st,s,kvs,cbgv);
except
result:=t=s;
end;
'≠','<>':
try
result:=exprToFloat(st,t,kvs,cbgv)<>exprToFloat(st,s,kvs,cbgv);
except
result:=t<>s;
end;
'<': result:=exprToFloat(st,t,kvs,cbgv)<exprToFloat(st,s,kvs,cbgv);
'>': result:=exprToFloat(st,t,kvs,cbgv)>exprToFloat(st,s,kvs,cbgv);
' in ','∈': begin
result:=false;
while (s<>'') and not result do
result:=erstesArgument(s)=t;
end;
' notIn ','∉': begin
result:=true;
while (s<>'') and result do
result:=erstesArgument(s)<>t;
end;
else
fehler('Operator '''+binOps[i]+''' ist nicht implementiert!');
end{of case};
exit;
end;
fehler(''''+s+''' ist kein gültiger logischer Ausdruck!');
end;
function exprToBool(st: boolean; s: string; kvs: tKnownValues; cbgv: tCallBackGetValue): boolean;
var
i,j,anz: longint;
operatoren,
ausdruecke: tIntPointArray;
werte: tBooleanArray;
klammernZahl: tLongintArray;
const
logOpsNamen: array[0..6] of string =
('not ',' and ','&&',' or ','||',' xor ',' equiv ');
logOpsAktionen: array[0..6,boolean,boolean] of boolean =
(((true,false),(true,false)), // not (1. Argument ignoriert!)
((false,false),(false,true)), // and
((false,false),(false,true)), // &&
((false,true),(true,true)), // or
((false,true),(true,true)), // ||
((false,true),(true,false)), // xor
((true,false),(false,true))); // equiv
begin
i:=1;
setLength(operatoren,0);
while i<=length(s) do begin
for j:=0 to length(logOpsNamen)-1 do
if copy(s,i,length(logOpsNamen[j]))=logOpsNamen[j] then begin
setLength(operatoren,length(operatoren)+1);
operatoren[length(operatoren)-1,'x']:=j;
operatoren[length(operatoren)-1,'y']:=i;
i:=i+length(logOpsNamen[j]);
break;
end;
inc(i);
end;
setLength(ausdruecke,length(operatoren)+1);
setLength(klammernZahl,length(operatoren)+1);
for i:=0 to length(ausdruecke)-1 do begin
if i=0 then
ausdruecke[i,'x']:=1
else
ausdruecke[i,'x']:=operatoren[i-1,'y']+length(logOpsNamen[operatoren[i-1,'x']]);
if i=length(operatoren) then
ausdruecke[i,'y']:=length(s)
else
ausdruecke[i,'y']:=operatoren[i,'y']-1;
anz:=0;
for j:=ausdruecke[i,'x'] to ausdruecke[i,'y'] do
if s[j]='(' then
inc(anz)
else if s[j]=')' then
dec(anz);
klammernZahl[i]:=anz;
while (anz<0) and (ausdruecke[i,'y']>=ausdruecke[i,'x']) do begin // zu viele schließende Klammern (am rechten Rand)
if s[ausdruecke[i,'y']]=')' then
inc(anz);
dec(ausdruecke[i,'y']);
end;
while (anz>0) and (ausdruecke[i,'y']>=ausdruecke[i,'x']) do begin // zu viele öffnende Klammern (am linken Rand)
if s[ausdruecke[i,'x']]='(' then
dec(anz);
inc(ausdruecke[i,'x']);
end;
if (ausdruecke[i,'y']<ausdruecke[i,'x']) and ((i=length(operatoren)) or (operatoren[i,'x']<>0)) then // das linke Argument von "not" darf leer sein
fehler('Klammerfehler in '''+s+''' ('+intToStr(ausdruecke[i,'y'])+'<'+intToStr(ausdruecke[i,'x'])+')!');
end;
anz:=0;
for i:=0 to length(klammernZahl)-1 do begin
anz:=anz+klammernZahl[i];
if anz<0 then
fehler('zu viele oder zu zeitige schließende Klammern in '''+s+'''!');
end;
if anz<>0 then
fehler('Klammern in '''+s+''' sind nicht ausgeglichen!');
setLength(werte,length(ausdruecke));
for i:=0 to length(werte)-1 do
if (i=length(werte)-1) or (operatoren[i,'x']<>0) then // kein linkes Argument von "not"
werte[i]:=simpleExprToBool(st,copy(s,ausdruecke[i,'x'],ausdruecke[i,'y']-ausdruecke[i,'x']+1),kvs,cbgv)
else
werte[i]:=false;
setLength(ausdruecke,0);
i:=length(werte)-2;
while i>=0 do
if ((klammernZahl[i]>0) or (i=0)) and (klammernZahl[i+1]<=0) then begin
result:=true;
werte[i]:=logOpsAktionen[operatoren[i,'x'],werte[i],werte[i+1]];
klammernZahl[i]:=klammernZahl[i]+klammernZahl[i+1];
for j:=i+1 to length(operatoren)-1 do
operatoren[j-1]:=operatoren[j];
for j:=i+2 to length(werte)-1 do begin
werte[j-1]:=werte[j];
klammernZahl[j-1]:=klammernZahl[j];
end;
setLength(operatoren,length(operatoren)-1);
setLength(werte,length(werte)-1);
setLength(klammernZahl,length(klammernZahl)-1);
if i>length(werte)-2 then
dec(i);
end
else
dec(i);
if length(werte)<>1 then
fehler('Kann kein Paar von öffnenden und schließenden Klammern in '''+s+''' mehr finden - das sollte eigentlich nicht passieren können!');
result:=werte[0];
end;
function formelnAuswerten(s: string; kvs: tKnownValues; cbgv: tCallBackGetValue): string;
var
i,start,mitte,
vk,nk: longint;
t: string;
ok: boolean;
wert: extended;
begin
ok:=false;
i:=1;
mitte:=0;
start:=0;
while i<=length(s) do begin
if copy(s,i,2)='$[' then begin
ok:=true;
i:=i+2;
start:=i;
continue;
end;
if ok then
case s[i] of
';':
mitte:=i;
']': begin
if mitte<=start then
mitte:=i;
t:=trim(copy(s,start,mitte-start));
try
wert:=exprToFloat(false,t,kvs,cbgv);
except
ok:=false;
inc(i);
continue;
end;
if mitte<>i then begin
t:=trim(copy(s,mitte+1,i-mitte-1));
if pos('.',t)=0 then
t:=t+'.0';
try
vk:=strToInt(erstesArgument(t,'.'));
nk:=strToInt(t);
except
ok:=false;
inc(i);
continue;
end;
if nk=0 then
t:=intToFixpoint(round(wert),vk)
else
t:=floatToFixpoint(wert,vk,nk);
end
else // keine Formatierung => generisch als float umwandeln
t:=floattostr(wert);
s:=
leftStr(s,start-3) + t + rightStr(s,length(s)-i);
i:=start-3 + length(t);
continue;
end;
'$': ok:=false;
end{of case};
inc(i);
end;
result:=s;
end;
function knownValue(nam: string; val: extended): tKnownValue;
begin
result.name:=nam;
result.value:=val;
end;
function berechneEinheitsZelle(invarianz,modulus: tInt64Point): tInt64Point;
var
c: char;
fak: tInt64Point;
verh: tExtPoint;
begin
for c:='x' to 'y' do begin
invarianz[c]:=invarianz[c] mod modulus[c];
result[c]:=ggT(invarianz[c],modulus[c]);
invarianz[c]:=invarianz[c] div result[c];
modulus[c]:=modulus[c] div result[c];
end;
// x*y = p (konstant)
// für x>y wollen wir x/y maximieren (lang und schmal)
// => x^2 ist zu maximieren, falls x>y
fak['x']:=ggT(modulus['y']*invarianz['x'],modulus['x']);
fak['y']:=ggT(modulus['x']*invarianz['y'],modulus['y']);
for c:='x' to 'y' do begin
verh[c]:=result[c]*fak[c]/result[char(ord('y')-byte(c='y'))];
if verh[c]<1 then
verh[c]:=1/verh[c];
end;
if verh['x']<verh['y'] then
result['y']:=result['y']*fak['y']
else
result['x']:=result['x']*fak['x'];
end;
function ggT(a,b: int64): int64;
var
tmp: int64;
begin // a <= b
if a>b then begin
tmp:=abs(a);
a:=abs(b);
b:=tmp;
end
else begin
a:=abs(a);
b:=abs(b);
end;
while a>0 do begin
tmp:=b mod a;
if tmp>a then
b:=tmp
else begin
b:=a;
a:=tmp;
end;
end;
result:=b;
end;
function ermittleAnstieg(dat: string; xSpalte,ySpalte: longestOrdinal): extended;
var
f: textFile;
s: string;
xSum,xQdrSum,ySum,xySum: extended;
wert: tExtPoint;
i,anz: longestOrdinal;
begin
if xSpalte=ySpalte then begin
result:=1;
exit;
end;
anz:=0;
xSum:=0;
ySum:=0;
xySum:=0;
xQdrSum:=0;
assignFile(f,dat);
reset(f);
while not eof(f) do begin
readln(f,s);
for i:=0 to min(xSpalte,ySpalte)-1 do
erstesArgument(s);
wert[char(ord('x')+byte(ySpalte<xSpalte))]:=
strToFloat(erstesArgument(s));
for i:=min(xSpalte,ySpalte)+1 to max(xSpalte,ySpalte)-1 do
erstesArgument(s);
wert[char(ord('x')+byte(xSpalte<ySpalte))]:=
strToFloat(erstesArgument(s));
inc(anz);
xSum:=xSum+wert['x'];
ySum:=ySum+wert['y'];
xySum:=xySum+wert['x']*wert['y'];
xQdrSum:=xQdrSum+sqr(wert['x']);
end;
closeFile(f);
if anz=0 then
fehler('Keine Werte in Anstiegs-Datei'''+dat+'''!');
result:=(anz*xySum-xSum*ySum)/(anz*xQdrSum-sqr(xSum))
end;
end.
|