Autor Beitrag
Budo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Mo 21.11.05 09:15 
hallo habe mal eine frage, ich habe eine sehr lange Listbox und in dieser möchte ich ein eingefügtes wort suchen. Die Listbox ist alphabetisch sortiert daher habe ich mir schon überlegt, das die suche erstmal die hälfte befragt zb. a-m und dadurch schon die hälfte ausschliesst damit nicht jedes wort abgefragt werden muss und dann so weiter. Das problem ist nur welchen befehl muss ich da angeben? bitte um hilfe.
danke
opfer.der.genauigkeit
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 754
Erhaltene Danke: 1



BeitragVerfasst: Mo 21.11.05 09:18 
Also prinzipiell kannst du mit dem Methodenaufruf "IndexOf( 'Value' );" den Index deines gesuchten Elements herausfinden.

_________________
Stellen Sie sich bitte Zirkusmusik vor.
Blackheart666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2195

XP
D3Prof, D6Pers.
BeitragVerfasst: Mo 21.11.05 09:45 
Gruss !
Vieleicht nicht ganz das was Du suchst aber geht.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
function WortSuche(Wort:String;Liste:Tstrings):integer;
var
i:Integer;
 begin
 result:=-1;
 for i:=0 to Liste.count-1 do
  if Wort=Liste[i] then begin
   result:=i;
    exit;
  end;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
 Listbox1.itemindex:=WortSuche(Edit1.Text,Listbox1.items);
end;
Budo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Mo 21.11.05 21:47 
also ich möchte gerne mit nem edit-feld und einem button arbeiten also BtSuchen... und das man halt das wort im edit-feld angibt und dann sucht. mit zB einer anzeige wort vorhanden oder nichtvorhanden.
danke
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Mo 21.11.05 22:13 
Solche Sachen sind meistens schon eingebaut, wie das opfer.der.genauigkeit schon treffend feststellte.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
Procedure TForm1.btSuchenClick (Sender : TObject);
Var
  i : Integer;

Begin
  i := MyListBox.Items.IndexOf (MyEdit.Text);
  if i=-1 Then
    Raise Exception.CreateFmt ('Suchbegriff "%s" wurde nicht gefunden, [MyEdit.Text]);
  MyListBox.ItemIndex := i;
End;

Deine Idee am Anfang ist sehr gut und nennt sich 'binary search'. Oder "binäre Suche". www.sortieralgorithm...arysearch/index.html

_________________
Na denn, dann. Bis dann, denn.
Budo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Di 22.11.05 22:26 
vielen danke jetzt funktioniert es!!!
danke danke danke
Budo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Mi 23.11.05 12:08 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure Tmain.BtSuchenClick(Sender: TObject);
var a,b,s:Integer;
 begin
  a:=0;
  b:=ListBox1.Items.Count-1;
  repeat
  s:=(a+b) div 2;
    if Listbox1.items[s] <> Ed1.Text then
      s:=a
        else s:=(a+b) div 2;
    until (a+b) div 2 = a;
      if  Ed1.text = Listbox1.items [s] then
           Listbox1.items [s]:=Ed1.Text;
end;


die suche mit der function hat funktioniert allerdings hat diese zu lange gebraucht, da die liste zu viele einträge hat. trotzdem danke.

jetzt habe ich versucht eine binäre Suche zu erstellen, habe allerdings Probleme dies mit den Variablen umzusetzen.
Bitte um Hilfe!Danke!

Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt.
alzaimar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2889
Erhaltene Danke: 13

W2000, XP
D6E, BDS2006A, DevExpress
BeitragVerfasst: Mi 23.11.05 12:49 
Wieviele Einträge sind denn in der Liste? Normalerweise sollte das suchen in ein paar 10000 Einträgen max. 100ms dauern und das ist schon pessimistisch.

Die Liste muss sortiert sein, damit die Binärsuche funktioniert. Die ist aber auch in TStringList eingebaut. Schau mal nach. Eventuell klappt es, TStringList (ListBox.Items).Sorted := True; zu schreiben und dann per 'IndexOf' das Element zu suchen.

_________________
Na denn, dann. Bis dann, denn.
Budo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Mi 23.11.05 13:54 
die liste ist schon sortiert aber trotz der kurzen zeit möchte ich eine binärsuche erstellen das muss doch möglich sein oder?
Budo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Mi 23.11.05 21:34 
ist den mein ansatz total falsch? ich glaube ich habe nur dreher in den variablen oder? bitte um hilfe! Danke



ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
 procedure Tmain.BtSuchenClick(Sender: TObject);  
var a,b,s:Integer;  
 begin  
  a:=0;  
  b:=ListBox1.Items.Count-1;  
  repeat  
  s:=(a+b) div 2;  
    if Listbox1.items[s] <> Ed1.Text then  
      s:=a  
        else s:=(a+b) div 2;  
    until (a+b) div 2 = a;  
      if  Ed1.text = Listbox1.items [s] then  
           Listbox1.items [s]:=Ed1.Text;  
end;


Moderiert von user profile iconChristian S.: Delphi-Tags hinzugefügt
Grishnak
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 221

Windows XP Home
Delphi 7 PE, Delphi 2005 PE
BeitragVerfasst: Do 24.11.05 00:01 
Die "IndexOf()"-Methode einer TStringList verwendet für die Suche in einer sortierten Liste einen Binärsuch-Algorithmus!

_________________
Mach' etwas idiotensicher und irgendjemand erfindet einen besseren Idioten!
Budo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Fr 25.11.05 15:54 
und wie funktioniert das??? versteh es nicht ganz!
Martin1966
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1068

Win 2000, Win XP
Delphi 7, Delphi 2005
BeitragVerfasst: Fr 25.11.05 15:58 
Naja, einfach die Methode IndexOf verwenden... den Rest übernimmt Delphi bzw. die Klasse für dich.

Lg Martin

_________________
Ein Nutzer der Ecke ;-)
Budo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 18



BeitragVerfasst: Mi 30.11.05 12:28 
ich liege doch richtig damit dass ich eine procedure zur suche erstellen muss
mir ist die Indexof nur in deiser hinsicht bekannt und kann sie nicht auf die suche beziehen
if ListBox1.Items.IndexOf(Ed1.Text)=-1 then ListBox1.items.Add(Ed1.Text);
Bitee um Hilfe!!!!