![]() |
Sortieren von Zufallszahlen im Stringgrid
Hallo,
kann mir jemand helfen, wie man die Zufallszahlen der zweiten Spalte in der dritten Spalte eines Stinggrids wieder sortiert ausgibt? Da ich Anfänger bin, bitte ich bei groben Schnitzern um Nachsicht.(Delphi Prof.2005) Ich versuche damit zu verstehen, wie das Verfahren funktioniert.
Delphi-Quellcode:
Herzlichen Dank im voraus
type
Zahl= array[1.. 100] of integer; procedure TForm1.Button2_BubblesortClick(Sender: TObject); var j,i,n, Merke: integer; begin n:=5; with StringGrid1 do for i:= n-1 downto 1 do for j:= 1 to i do if Zahl[j]> Zahl[j+1] then begin Merke:= Zahl[j]; Zahl[j]:= Zahl[j+1]; Zahl[j+1]:= Merke; end; StringGrid1.Cells[2,i]:=IntToStr(Merke); end; Technikus |
Re: Sortieren von Zufallszahlen im Stringgrid
Da fehlt ein Begin...End. Soll ich sagen, wo? :wink:
|
Re: Sortieren von Zufallszahlen im Stringgrid
Hallo alzaimar,
Ich habs mal probiert und in folgendes geändert:
Delphi-Quellcode:
ich hoffe, das begin und end habe ich richtig gesetzt
procedure TForm1.Button2_BubblesortClick(Sender: TObject);
var j,ende,i,n,Merke: integer; begin j:= StrToInt (StringGrid1.Cells[2,j]); n:=5; with StringGrid1 do begin for i:= ende-1 downto 1 do for j:= 1 to i do if Stringgrid1.Cells[2,j]> StringGrid1.Cells[2,j+1] then begin Merke:= StrToInt(StringGrid1.Cells[2,j]); Stringgrid1.Cells[2,j]:= StringGrid1.Cells[2,j+1]; StringGrid1.Cells[2,j+1]:= IntToStr(Merke); end; StringGrid1.Cells[2,i]:= (StringGrid1.Cells[2,j+1]); end end; Der wurm ist aber noch drin. Gruß Technikus |
Re: Sortieren von Zufallszahlen im Stringgrid
die j-Schleife, Technikus, die j-Schleife...
marabu |
Re: Sortieren von Zufallszahlen im Stringgrid
Hallo marabu,
ich habe den Text folgender Maßen geändert:
Delphi-Quellcode:
Bei der Sache mit der j-Schleife --gib mir bitte noch ein Stichwort,ja?
procedure TForm1.Button2_BubblesortClick(Sender: TObject);
var j,ende,i,n,Merke: integer; begin ende:=5; with StringGrid1 do begin for i:= ende-1 downto 1 do for j:= 1 to i do if Stringgrid1.Cells[2,j+1]> StringGrid1.Cells[2,j+2] then begin Merke:= StrToInt(StringGrid1.Cells[2,j]); Stringgrid1.Cells[2,j+1]:= StringGrid1.Cells[2,j+2]; StringGrid1.Cells[2,j+2]:= IntToStr(Merke); end; StringGrid1.Cells[2,i+1]:= (StringGrid1.Cells[2,j+2]); end end; Danke Technikus |
Re: Sortieren von Zufallszahlen im Stringgrid
Ach Technikus, wenn du nur die Einrückung etwas sorgfältiger machen würdest, dann würdest du sofort sehen:
Delphi-Quellcode:
Ich habe deinen Code nicht geprüft, aber von der Optik und wegen der Verwendung von j in der markierten Zeile, gehört der Block wohl in die j-Schleife
procedure TForm1.Button2_BubblesortClick(Sender: TObject);
var j, ende, i, n, Merke: integer; begin ende := 5; with StringGrid1 do begin for i := ende-1 downto 1 do for j := 1 to i do begin if Stringgrid1.Cells[2, j+1] > StringGrid1.Cells[2, j+2] then begin Merke := StrToInt(StringGrid1.Cells[2, j]); Stringgrid1.Cells[2, j+1] := StringGrid1.Cells[2, j+2]; StringGrid1.Cells[2, j+2] := IntToStr(Merke); end; StringGrid1.Cells[2, i+1] := (StringGrid1.Cells[2, j+2]); // hier werden i und j verwendet end end end; Grüße vom marabu |
Re: Sortieren von Zufallszahlen im Stringgrid
Hallo marabu,
jetzt habe ich gesehen, und geändert. Ich bemühe mich um Besserung und weniger Schlamperei.
Delphi-Quellcode:
Funktionieren will es aber trotzdem nicht.
procedure TForm1.Button2_BubblesortClick(Sender: TObject);
var j, ende, i, Merke: integer; begin ende := 5; with StringGrid1 do begin for i := ende-1 downto 1 do for j := 1 to i do begin if Stringgrid1.Cells[2, j+1] > StringGrid1.Cells[2, j+2] then begin Merke := StrToInt(StringGrid1.Cells[2, j]); Stringgrid1.Cells[2, j+1] := StringGrid1.Cells[2, j+2]; StringGrid1.Cells[2, j+2] := IntToStr(Merke); end; StringGrid1.Cells[2, j+1] := (StringGrid1.Cells[2, j+2]); end end; end; Wie kann ich die sortierte Ausgabe so hinbekommen, dass die sortierten Ergebnisse in die 3. Spalte erscheinen? Gruß Technikus |
Re: Sortieren von Zufallszahlen im Stringgrid
So müsste es funktionieren:
Delphi-Quellcode:
procedure TForm1.Button2Click(Sender: TObject);
var help: string; i, j: integer; noXChange: boolean; begin (* alle "Zahlen" aus Spalte #1 in Spalte #2 kopieren *) StringGrid1.Cols[2].Text:=StringGrid1.Cols[1].Text; (* Bubblesort *) repeat (* noch keine Vertauschung *) noXChange:=true; (* Reihen 1 bis 100 sortieren *) for i:=1 to 99 do if StrToInt(StringGrid1.Cells[2, i]) > StrToInt(StringGrid1.Cells[2, i+1]) then begin (* Tauschen *) help:=StringGrid1.Cells[2, i]; StringGrid1.Cells[2, i]:=StringGrid1.Cells[2, i+1]; StringGrid1.Cells[2, i+1]:=help; (* Vertauschung hat stattgefunden *) noXChange:=false; end; until noXChange; end; |
Re: Sortieren von Zufallszahlen im Stringgrid
Guten Morgen,
Delphi-Quellcode:
Damit kopiere ich aber auch den Spaltenkopf und das möchte ich nicht. Der Spaltenkopf soll erhalten bleiben.
begin
(* alle "Zahlen" aus Spalte #1 in Spalte #2 kopieren *) StringGrid1.Cols[2].Text:=StringGrid1.Cols[1].Text ... Gruß Technikus |
Re: Sortieren von Zufallszahlen im Stringgrid
Delphi-Quellcode:
:wink:
var
s: string; [...] s:=StringGird1.Cells[2, 0]; StringGrid1.Cols[2].Text:=StringGrid1.Cols[1].Text StringGrid1.Cells[2, 0]:=s; Was ich immer noch für besser halte, als die einzelnen Zellen per for-Schleife zu kopieren! |
Alle Zeitangaben in WEZ +1. Es ist jetzt 16:20 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz