Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Variablen sortieren (https://www.delphipraxis.net/71554-variablen-sortieren.html)

the_source 16. Jun 2006 22:12


Variablen sortieren
 
Hallo,

bei meinem derzeitigen Programm handelt es sich um ein Kryptoanalyse-tool. Seine Hauptfunktion besteht darin, die Häufigkeit von Buchstaben in einem Text zu zählen und eine Statistik zu erstellen.
In einer Schleife wird bei jeden Buchstaben ein entsprechender Zähler erhöht, bis das Textende erreicht ist. Die die Werte der Zähler (countA, countB....) werden dann ausgegeben, sodass über jeden Buchstaben eine Häufigkeitsinformation vorliegt.
Um ein Diaramm und eine ausführliche Statistik zu erstellen, brauche ich nun die Variable, die den höchsten Wert hat.
Dazu meine Frage: Wie kann ich die höchste Variable bestimmen?


MfG
the_source

Muetze1 16. Jun 2006 22:22

Re: Variablen sortieren
 
Hast du für jeden Buchstaben eine eigene Zählvariable? Dann hast du doch haufenweise Variablen und ein paar zu viele IF Bedingungen - oder nicht? Warum nutzt du kein Array?

Nun noch zu deiner Frage: Schau dir doch einfach mal die Funktion Max() an oder mach es logisch selber: Mit einer IF Bedingung um dem grösser/kleiner Vergleich (> <) kannst du es leicht selber machen...

the_source 16. Jun 2006 22:24

Re: Variablen sortieren
 
Hmm, ja, dann werd ich wohl doch nochmal alles ummodeln. Ich bin zu spät auf die Idee gekommen, ein array zu machen.

Muetze1 16. Jun 2006 22:27

Re: Variablen sortieren
 
Ist aber wirklich ein guter Schritt der sich lohnt, weil sonst schreibst du den Code wirklich für jeden Buchstaben einzelnd. Ein Array macht das ganze recht komformtabel.

Dax 16. Jun 2006 22:41

Re: Variablen sortieren
 
Nein, ein Array macht das ganze erst brauchbar ;)

the_source 17. Jun 2006 10:32

Re: Variablen sortieren
 
hmm, also ich hab mir das jetzt mal zurechtgelget, aber es ergibt sich ein neues Problem.
ich hatte ja vorher ne schleife, in der für jeden Buchstaben eine if-anweisung befand

Delphi-Quellcode:
   for counter:= 1 to laenge do
    begin
     if UpperCase(s[counter]) = 'A' then
      begin
       countA:= countA + 1;
       countDEF:= countDEF + 1;
      end;
     if UpperCase(s[counter]) = 'B' then
      begin
       countB:= countB + 1;
       countDEF:= countDEF + 1;
      end;
und so weite..

jetzt habe ich diese count-Variablen rausgeschmissen und ein Array von 1 bis 26 erstellt.
jetzt habe ich aber das Problem, dass ich ja ne schleife mit möglichst nur einer if-Anweisung möchte.


Delphi-Quellcode:
   for counter:= 1 to laenge do
    begin
     if UpperCase(s[counter]) = 'A' then
      begin
       countArray[1]:= countArray[1] + 1;
       countDEF:= countDEF + 1;
      end;
     if UpperCase(s[counter]) = 'B' then
      begin
       countArray[2]:= countArray[2] + 1;
       countDEF:= countDEF + 1;
      end;
wie bekomm ichs jetzt hin, dass ich, wenn ich für das countArray einen counter einsetze, auch die Buchstaben in eine Art Liste zusammenfasse. Diese müssen ja sonst immer einzeln angegeben werden.

bin grad, während ich das schreibe, auf die Idee gekommen, das mit Ascii-codes zu machen.
Aber es gibt dann noch ein kleines Problem.

Am Ende werden die einzelnen Einträge Labels zugeordnet.

Delphi-Quellcode:
OutA.Caption:= IntToStr(countA);
  OutB.Caption:= IntToStr(countB);
  OutC.Caption:= IntToStr(countC);
Wie kann ich das nun mit den Namen der Labels automatisieren. Ist zwar nicht überlebenswichtig, wäre aber gut.

[edit=Sharky]Code-Tags in Delphi-Tags geändert. Mfg, Sharky[/edit]

Sharky 17. Jun 2006 10:42

Re: Variablen sortieren
 
Hai the_source,

"Herzlich Willkommen in der Delphi-PRAXiS" :hi:

hier mal ein Grundgerüst wie ich da ran gehen würde:
Delphi-Quellcode:
type
  TCounterArray = array [1..255] of Cardinal;


procedure InitCounterArray(var aArray: TCounterArray);
var
  ndx: Integer;
begin
  for ndx := Low(aArray) to High(aArray) do
  begin
    aArray[ndx] := 0;
  end;
end;

procedure CountChars(var aArray: TCounterArray; aValue: string);
var
  ndx: Integer;
begin
  for ndx := 1 to Length(aValue) do
  begin
    aArray[Ord(aValue[ndx])] := aArray[Ord(aValue[ndx])] + 1;
  end;
end;

procedure TDemoForm.btn_testClick(Sender: TObject);
var
  s: string;
  myArray: TCounterArray;
begin
  InitCounterArray(myArray);
  s := 'sdöfljösdlafjpoweiurpowejrsnadöflkasdfsdf';
  CountChars(myArray, s);
end;
In dem Array hast Du jetzt für jeden Buchstaben den Wert wie oft er in dem String s vorkommt. Dieses Array könntest Du jetzt zum Beispiel sortieren.

[edit]aArray als Var-Parameter übergeben

Hawkeye219 17. Jun 2006 10:54

Re: Variablen sortieren
 
@Sharky

Wenn du das Array als var-Parameter übergibst, klappt es besser...

Gruß Hawkeye

Sharky 17. Jun 2006 10:57

Re: Variablen sortieren
 
Zitat:

Zitat von Hawkeye219
... als var-Parameter übergibst, ...

:oops: Wie peinlich :oops:

the_source 17. Jun 2006 11:09

Re: Variablen sortieren
 
hmm, also ich habs jetzt mal provisorisch gemacht mit nen labels, aber das ist nicht das Problem.
Das Problem besteht darin, dass ich am Ende für jeden Buchstaben die Häufigkeit "0" rausbekomme. Der "counterDEF", der die Anzahl aller zulässigen Buchstaben zählt, stimmt.

Ich verstehe einfach nict, woran es liegt.

Delphi-Quellcode:
procedure TForm1.StartClick(Sender: TObject);
 var
  s:String;
  counterMAIN:Integer;
  counterSET:Integer;
  counterCHAR:Integer;
  countDEF:Integer;
  laenge:Integer;
  countArray: Array[1..26] of Integer;
 begin
  for counterSET:= 1 to 26 do
   begin
    countArray[counterSET]:= 0;
   end;
  countDEF:= 0;
  s:= eingabe.Text;
  laenge:= Length(s);
  ProgressBar1.Max:= laenge;
   for counterMAIN:= 1 to laenge do
    begin
     for counterCHAR:= 65 to 90 do
      begin
       if UpperCase(s[counterMAIN]) = CHR(counterCHAR) then
        begin
         countArray[counterCHAR]:= countArray[counterCHAR] + 1;
         countDEF:= countDEF + 1;
        end;
      end;
     ProgressBar1.Position:= ProgressBar1.Position + 1;
    end;
  OutA.Caption:= IntToStr(countArray[1]);
  OutB.Caption:= IntToStr(countArray[2]);
  OutC.Caption:= IntToStr(countArray[3]);
  OutD.Caption:= IntToStr(countArray[4]);
  OutE.Caption:= IntToStr(countArray[5]);
  OutF.Caption:= IntToStr(countArray[6]);
  OutG.Caption:= IntToStr(countArray[7]);
  OutH.Caption:= IntToStr(countArray[8]);
  OutI.Caption:= IntToStr(countArray[9]);
  OutJ.Caption:= IntToStr(countArray[10]);
  OutK.Caption:= IntToStr(countArray[11]);
  OutL.Caption:= IntToStr(countArray[12]);
  OutM.Caption:= IntToStr(countArray[13]);
  OutN.Caption:= IntToStr(countArray[14]);
  OutO.Caption:= IntToStr(countArray[15]);
  OutP.Caption:= IntToStr(countArray[16]);
  OutQ.Caption:= IntToStr(countArray[17]);
  OutR.Caption:= IntToStr(countArray[18]);
  OutS.Caption:= IntToStr(countArray[19]);
  OutT.Caption:= IntToStr(countArray[20]);
  OutU.Caption:= IntToStr(countArray[21]);
  OutV.Caption:= IntToStr(countArray[22]);
  OutW.Caption:= IntToStr(countArray[23]);
  OutX.Caption:= IntToStr(countArray[24]);
  OutY.Caption:= IntToStr(countArray[25]);
  OutZ.Caption:= IntToStr(countArray[26]);
  OutGES.Caption:= IntToStr(countDEF);
 end;
[edit=Sharky]Delphi-Tags gesetzt. Mfg, Sharky[/edit]


Alle Zeitangaben in WEZ +1. Es ist jetzt 20:31 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz