Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Stringrid ordnen (https://www.delphipraxis.net/108348-stringrid-ordnen.html)

simon19881 12. Feb 2008 08:04


Stringrid ordnen
 
Hallo leutz, ich hab ne Aufgabe in Info aufbekommen und komm auf keinen grünen Zweig.
Wir sollen in ein Stringgrid (1 Zeile, 30 Spalten) zufällig die werte z.B: A1,A2,A3,B1,B2,B3 erstellen
und dan der reihenfolge nach orden könt ihr mir bitte helfen.
Vielen Dank
Simon

Ps.: Ich bin delphi Neuling ^^

TeronG 12. Feb 2008 08:07

Re: Stringrid ordnen
 
aha ne Info-Aufgabe .. dann nur Such-hinweise ^^


Zitat:

Zitat von simon19881
...zufällig die werte ...

-> Random

Zitat:

Zitat von simon19881
...und dan der reihenfolge nach orden...

-> bubblesort: IMHO recht easy zu verstehen und umzusetzen

simon19881 12. Feb 2008 16:46

Re: Stringrid ordnen
 
Hallo ich mus noch dazui sagen dass ich ein totaler anfänger mit delphi bin
Bitte sagt mir mal wie ich verhindere dass er die anderen werte überschreibt
danke schonmal im voraus ^^

Delphi-Quellcode:
var i,z:byte;
begin
for i:=0 to 30 do
z:=random (31);
if Stringgrid1.Cells[i,0]='' then
begin
stringgrid1.cells[z,0]:='A1';
stringgrid1.cells[z,0]:='A2';
stringgrid1.cells[z,0]:='A3';
stringgrid1.cells[z,0]:='A4';
stringgrid1.cells[z,0]:='B1';
stringgrid1.cells[z,0]:='B2';
stringgrid1.cells[z,0]:='B3';
stringgrid1.cells[z,0]:='B4';
stringgrid1.cells[z,0]:='C1';
stringgrid1.cells[z,0]:='C2';
stringgrid1.cells[z,0]:='C3';
stringgrid1.cells[z,0]:='C4';
end;
end;

Dani 12. Feb 2008 18:07

Re: Stringrid ordnen
 
Edit: hoppla, der erste Parameter von Cells[I,J] ist die Spalte!

#1
For-Schleife geht so:
Delphi-Quellcode:
for I := Low to High do begin
  //Code
end;

//oder so, je nach Styleguide

for I := Low to High do
begin
  //Code
end;
In deinem Code wird z jedes Mal wieder überschrieben, ohne dass du damit irgendwas anstellst.
Delphi-Quellcode:
for i:=0 to 30 do
z:=random (31);

#2
Der Code sollte von Anfang an "richtig" eingerückt werden, weil man dann den Programmablauf schon anhand der Struktur des Quellcodes nachvollziehen kann. Außerdem sollten alle Bezeichner (also Variablennamen etc) aussagekräftig sein. Das ist wichtig, um Fehler zu vermeiden.

Dieser Code tut noch nicht, was du willst, aber vielleicht siehst du schon, warum dein Programm noch nicht funktioniert.
Delphi-Quellcode:
var
  I, ColIndex: Byte;
begin
  for I := 0 to 30 do begin
    ColIndex:= Random(31);
  end;
  //I ist jetzt undefiniert (siehe Onlinehilfe) und ColIndexeine Zahl zwischen 0 und 30
  if Stringgrid1.Cells[I, 0] = '' then begin
    StringGrid1.Cells[ColIndex, 0] := 'A1';
    StringGrid1.Cells[ColIndex, 0] := 'A2';
    StringGrid1.Cells[ColIndex, 0] := 'A3';
    StringGrid1.Cells[ColIndex, 0] := 'A4';
    StringGrid1.Cells[ColIndex, 0] := 'B1';
    StringGrid1.Cells[ColIndex, 0] := 'B2';
    StringGrid1.Cells[ColIndex, 0] := 'B3';
    StringGrid1.Cells[ColIndex, 0] := 'B4';
    StringGrid1.Cells[ColIndex, 0] := 'C1';
    StringGrid1.Cells[ColIndex, 0] := 'C2';
    StringGrid1.Cells[ColIndex, 0] := 'C3';
    StringGrid1.Cells[ColIndex, 0] := 'C4';
  end;
end;
#3
Wer ist "er"? :mrgreen:
Zitat:

Bitte sagt mir mal wie ich verhindere dass er die anderen werte überschreibt

bluesbear 12. Feb 2008 18:17

Re: Stringrid ordnen
 
Hallo Simon,
Zitat:

Zitat von simon19881
Ps.: Ich bin delphi Neuling ^^

ok, eine Starthilfe gebe ich Dir. Kommentarlos :twisted:
Delphi-Quellcode:
var i, a, b : Integer;
    tmpStr : String;
begin
  for i:=0 to Pred(StringGrid1.RowCount) do begin
    StringGrid1.Cells[0, i] := Chr(Ord('A') + (i div 4)) + IntToStr((i mod 4) + 1);
  end;
  for i:=0 to 300 do begin
    a := Random(Pred(StringGrid1.RowCount));
    b := Random(Pred(StringGrid1.RowCount));
    tmpStr := StringGrid1.Cells[0, a];
    StringGrid1.Cells[0, a] := StringGrid1.Cells[0, b];
    StringGrid1.Cells[0, b] := tmpStr;
  end;
end;
Rauskriegen, wie das funktioniert und den Bubblesort programmieren mußt Du nun aber selber :zwinker:


Alle Zeitangaben in WEZ +1. Es ist jetzt 05:55 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-2025 by Thomas Breitkreuz