summaryrefslogtreecommitdiff
path: root/unit2.pas
diff options
context:
space:
mode:
Diffstat (limited to 'unit2.pas')
-rw-r--r--unit2.pas250
1 files changed, 204 insertions, 46 deletions
diff --git a/unit2.pas b/unit2.pas
index e75d847..edd6b70 100644
--- a/unit2.pas
+++ b/unit2.pas
@@ -24,6 +24,7 @@ type
function count: longint;
procedure add(x: extended; cnt: longint); overload;
procedure add(x: string; cnt: longint); overload;
+ function toStr(llNum: longint): string;
procedure dump(sl: tStrings);
property inhalt[i: longint]: extended
read rInhalt; default;
@@ -59,10 +60,14 @@ type
write wFrequenz;
function ordnungNichtDirektEinstellen: boolean; inline;
procedure anzeigen(sl: tStrings);
+ procedure uebersicht(sl: tStrings);
+ procedure sortenreinAnzeigen(rsl,csl: tStrings);
function dump: string;
function weite: longint;
function cWaehlen(idx: longint; var nutzen: extended): boolean; // Erfolg?
- function rsWaehlen(idx: longint): boolean; dynamic; abstract; // Erfolg?
+ function rsWaehlen(idx: longint): boolean; dynamic; abstract; // Erfolg?
+ procedure allesZurueckLegen;
+ function findeRsZuAnderenCs(_cs,_rs: tStrings): boolean; // Erfolg?
end;
tHochpass = class(tFilter)
function rsWaehlen(idx: longint): boolean; override;
@@ -86,6 +91,7 @@ type
function ordnungsSchritt: boolean;
function auswahlSchritt(minNutzen: extended; callBack: tNotifyEvent): boolean;
procedure anzeigen(sl: tStrings);
+ procedure filterUebersicht(sl: tStrings);
function dump: string;
function dumpWeite: string;
end;
@@ -101,6 +107,8 @@ type
function minNutzen(cnt: longint): extended;
end;
+function strToScientific(s: string): extended; inline;
+function scientificToStr(x: extended): string; inline;
function liesExtendedArray(sl: tStrings): tMyExtendedArray; overload;
function liesExtendedArray(sl: tStrings; suf: string): tMyExtendedArray; overload;
procedure liesExtendedArray(sl: tStrings; suf: string; var arr: tMyExtendedArray); overload;
@@ -201,25 +209,17 @@ begin
end;
procedure tMyExtendedArray.add(x: string; cnt: longint);
-const
- edg: array[0..13] of string = ('k','M','G','T','P','E','m','µ','n','p','f','a','y','z');
- fak: array[0..13] of extended = (1e3,1e6,1e9,1e12,1e15,1e18,1e-3,1e-6,1e-9,1e-12,1e-15,1e-18,1e-21,1e-24);
-var
- i: longint;
- c: char;
begin
- if decimalseparator='.' then
- c:=','
- else
- c:='.';
- while pos(c,x)>0 do
- x[pos(c,x)]:=decimalseparator;
- for i:=0 to length(edg)-1 do
- if pos(edg[i],x)>0 then begin
- add(strtofloat(trim(leftStr(x,pos(edg[i],x)-1)))*fak[i],cnt);
- exit;
- end;
- add(strtofloat(trim(x)),cnt);
+ add(strToScientific(x),cnt);
+end;
+
+function tMyExtendedArray.toStr(llNum: longint): string;
+begin
+ if (llNum<0) or (llNum>=length(zahlen)) then begin
+ result:='nan ('+inttostr(llNum)+') ';
+ exit;
+ end;
+ result:=scientificToStr(zahlen[llNum])
end;
procedure tMyExtendedArray.dump(sl: tStrings);
@@ -335,16 +335,7 @@ var
i: longint;
begin
if _ordnung<>o then begin
- for i:=0 to length(Rs)-1 do
- if Rs[i].i>=0 then begin
- inc(ars.anzahlen[Rs[i].i]);
- Rs[i].i:=-1;
- end;
- for i:=0 to length(Cs)-1 do
- if Cs[i]>=0 then begin
- inc(acs.anzahlen[Cs[i]]);
- Cs[i]:=-1;
- end;
+ allesZurueckLegen;
_ordnung:=o;
if assigned(gegenstueck) then
@@ -375,28 +366,39 @@ end;
procedure tFilter.anzeigen(sl: tStrings);
var
- s: string;
i: longint;
begin
+ uebersicht(sl);
+ for i:=0 to ordnung-1 do
+ sl.add(
+ ' R'+inttostr(i+1)+' = '+scientificToStr(Rs[i].x)+'Ω von '+ars.toStr(Rs[i].i)+'Ω,'+
+ ' C'+inttostr(i+1)+' = '+acs.toStr(Cs[i])+'F'
+ );
+end;
+
+procedure tFilter.uebersicht(sl: tStrings);
+var
+ s: string;
+begin
if self is tHochpass then s:='Hochpass'
else s:='Tiefpass';
if assigned(gegenstueck) then s:=s+'''';
- sl.add(s+' '+inttostr(ordnung)+'. Ordnung ('+floattostr(frequenz)+' Hz)');
- for i:=0 to ordnung-1 do begin
- s:=' R'+inttostr(i+1)+' = '+floattostr(Rs[i].x)+' Ω von ';
- if (Rs[i].i>=0) and
- (Rs[i].i<length(ars.zahlen)) then
- s:=s+floattostr(ars.zahlen[Rs[i].i])
- else
- s:=s+'nan('+inttostr(Rs[i].i)+')';
- s:=s+' Ω, C'+inttostr(i+1)+' = ';
- if (Cs[i]>=0) and
- (Cs[i]<length(acs.zahlen)) then
- s:=s+floattostr(acs.zahlen[Cs[i]])
- else
- s:=s+'nan('+inttostr(Cs[i])+')';
- s:=s+' F';
- sl.add(s);
+ sl.add(s+' '+inttostr(ordnung)+'. Ordnung ('+scientificToStr(frequenz)+'Hz)');
+end;
+
+procedure tFilter.sortenreinAnzeigen(rsl,csl: tStrings);
+var
+ i: longint;
+begin
+ if assigned(rsl) then begin
+ rsl.clear;
+ for i:=0 to ordnung-1 do
+ rsl.add(scientificToStr(Rs[i].x)+'Ω');
+ end;
+ if assigned(csl) then begin
+ csl.clear;
+ for i:=0 to ordnung-1 do
+ csl.add(acs.toStr(Cs[i])+'F');
end;
end;
@@ -462,6 +464,101 @@ begin
result:=true;
end;
+procedure tFilter.allesZurueckLegen;
+var
+ i: longint;
+begin
+ for i:=0 to length(Rs)-1 do
+ if Rs[i].i>=0 then begin
+ inc(ars.anzahlen[Rs[i].i]);
+ Rs[i].i:=-1;
+ end;
+ for i:=0 to length(Cs)-1 do
+ if Cs[i]>=0 then begin
+ inc(acs.anzahlen[Cs[i]]);
+ Cs[i]:=-1;
+ end;
+end;
+
+function tFilter.findeRsZuAnderenCs(_cs,_rs: tStrings): boolean;
+var
+ tmpArs,tmpAcs: tMyExtendedArray;
+ tmpRs: tExtendedLongintArray;
+ tmpCs: tLongintArray;
+ tmpVals: array of extended;
+ i: longint;
+begin
+ result:=true;
+
+ tmpArs:=ars; // kleiner Hack
+ tmpAcs:=acs;
+
+ setlength(tmpRs,length(Rs));
+ for i:=0 to length(tmpRs)-1 do
+ tmpRs[i]:=Rs[i];
+ setlength(tmpCs,length(Cs));
+ for i:=0 to length(tmpCs)-1 do
+ tmpCs[i]:=Cs[i];
+
+ ars:=tMyExtendedArray.create;
+ for i:=0 to length(Rs)-1 do // Widerstände nach Soll generieren
+ if Rs[i].i>=0 then begin
+ ars.add(tmpArs.zahlen[Rs[i].i],1);
+ Rs[i].i:=-1;
+ end;
+
+ if ars.count<>ordnung then begin
+ _rs.add('Ich habe '+inttostr(ars.count)+' Widerstände zur Verfügung statt erwarteter '+inttostr(ordnung)+'.');
+ result:=false;
+ end;
+
+ setlength(tmpVals,0);
+ acs:=tMyExtendedArray.create;
+ for i:=0 to _cs.count-1 do // Kondensatoren aus Tabelle auslesen
+ if rightStr(trim(_cs[i]),1)='F' then begin
+ if length(tmpVals)>=ordnung then begin
+ _rs.add('zu viele Kondensatoren!');
+ result:=false;
+ break;
+ end;
+ setlength(tmpVals,length(tmpVals)+1);
+ tmpVals[length(tmpVals)-1]:=strToScientific(leftStr(_cs[i],length(_cs[i])-1));
+ acs.add(tmpVals[length(tmpVals)-1],1);
+ end;
+ if length(tmpVals)<ordnung then begin
+ _rs.add('zu wenige Kondensatoren!');
+ result:=false;
+ end;
+
+ for i:=0 to length(tmpVals)-1 do
+ cs[i]:=acs.findeWenigstens(tmpVals[i]);
+
+ for i:=0 to (ordnung div 2) -1 do
+ if not rsWaehlen(i) then begin
+ _rs.add('Widerstandspaar ('+inttostr(2*i+1)+';'+inttostr(2*i+2)+') nicht wählbar!');
+ result:=false;
+ break;
+ end;
+
+ if result then begin
+ self.sortenreinAnzeigen(_rs,nil);
+ _rs.add('alles ok ('+inttostr(random(1000))+')!');
+ end;
+
+ ars.free;
+ acs.free;
+
+ ars:=tmpArs; // kleinen Hack rückgängig machen
+ acs:=tmpAcs;
+ setlength(Rs,length(tmpRs));
+ setlength(Rs,length(tmpRs));
+ for i:=0 to length(Rs)-1 do
+ Rs[i]:=tmpRs[i];
+ setlength(Cs,length(tmpCs));
+ for i:=0 to length(Cs)-1 do
+ Cs[i]:=tmpCs[i];
+end;
+
// tHochpass *******************************************************************
function tHochpass.rsWaehlen(idx: longint): boolean;
@@ -705,6 +802,15 @@ begin
filter[i].anzeigen(sl);
end;
+procedure tLoesung.filterUebersicht(sl: tStrings);
+var
+ i: longint;
+begin
+ sl.clear;
+ for i:=0 to length(filter)-1 do
+ filter[i].uebersicht(sl);
+end;
+
function tLoesung.dump: string;
var
i: longint;
@@ -806,6 +912,58 @@ end;
// allgemeine Funktionen *******************************************************
+const
+ edg: array[0..13] of string = ('k','M','G','T','P','E','m','µ','n','p','f','a','y','z');
+ fak: array[0..13] of extended = (1e3,1e6,1e9,1e12,1e15,1e18,1e-3,1e-6,1e-9,1e-12,1e-15,1e-18,1e-21,1e-24);
+
+function strToScientific(s: string): extended;
+var
+ i: longint;
+ c: char;
+begin
+ if decimalseparator='.' then
+ c:=','
+ else
+ c:='.';
+ while pos(c,s)>0 do
+ s[pos(c,s)]:=decimalseparator;
+ for i:=0 to length(edg)-1 do
+ if pos(edg[i],s)>0 then begin
+ result:=strtofloat(trim(leftStr(s,pos(edg[i],s)-1)))*fak[i];
+ exit;
+ end;
+ result:=strtofloat(trim(s));
+end;
+
+function scientificToStr(x: extended): string;
+var
+ f: extended;
+ i: longint;
+begin
+ f:=1;
+ while abs(x)>=0.99e3 do begin
+ x:=x*1e-3;
+ f:=f*1e3;
+ end;
+ if x<>0 then
+ while abs(x)<0.99 do begin
+ x:=x*1e3;
+ f:=f*1e-3;
+ end;
+ for i:=0 to length(fak)-1 do
+ if abs(f/fak[i]-1)<0.1 then begin
+ result:=floattostr(x);
+ if length(result)-pos(decimalSeparator,result+decimalSeparator)>3 then
+ delete(result,pos(decimalSeparator,result+decimalSeparator)+4,length(result));
+ result:=result+' '+edg[i];
+ exit;
+ end;
+ result:=floattostr(x*f);
+ if length(result)-pos(decimalSeparator,result+decimalSeparator)>3 then
+ delete(result,pos(decimalSeparator,result+decimalSeparator)+4,length(result));
+ result:=result+' ';
+end;
+
function liesExtendedArray(sl: tStrings): tMyExtendedArray;
begin
result:=liesExtendedArray(sl,'');