Autor Beitrag
Philipp S
Hält's aus hier
Beiträge: 10

Win XP

BeitragVerfasst: Do 05.11.09 19:48 
Hallo Leute,

ich sollte ein Stringgrid erstellen, welches 9*9 Felder besitzt. Die Zellen [0,1..8] und [1..8,0] sind beschriftet. In den übrigen 64 Feldern ([1..8],[1..8]) sollen Zufallszahlen stehen. Die größte und kleinste Zahl sollen in je einem Editfeld ersichtlich seien. Bis hierhin kein Problem. Im Stringgrid sollen die Zahlen nun von oben links nach unten rechts geordnet werden. Erste und letzte Zahl klappen schon. Ich hoffe ihr könntet mir helfen, denn ich verzweifle schon seit Stunden daran diese Zahlen irgendwie der Größe nach im Sringgrid zu ordnen/anzuzeigen!^^ :?: :cry:

Danke schon mal im Voraus!

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
 unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Grids, ExtCtrls;

type
  TDamen = class(TForm)
    Panel1: TPanel;
    StringGrid1: TStringGrid;
    BitBtn1: TBitBtn;
    Bevel1: TBevel;
    Splitter1: TSplitter;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure TSpeedButton(Sender: TObject);



  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Damen: TDamen;
   i, max, min : integer;
     x, y, z : array [1..8of integer;
      abstand : array [1..60of integer;
implementation

{$R *.dfm}



procedure TDamen.FormCreate(Sender: TObject);

var
    i : integer;

begin

{Bezeichnung Felder A.}
for i:=1 to 8 do stringgrid1.Cells[0,i]:=floattostr(i);

for i:=1 to 8 do
case i of
1: stringgrid1.Cells[1,0]:='A';
2: stringgrid1.Cells[2,0]:='B';
3: stringgrid1.Cells[3,0]:='C';
4: stringgrid1.Cells[4,0]:='D';
5: stringgrid1.Cells[5,0]:='E';
6: stringgrid1.Cells[6,0]:='F';
7: stringgrid1.Cells[7,0]:='G';
8: stringgrid1.Cells[8,0]:='H';
end;
end;
{Bezeichnung Felder Ende}
procedure TDamen.TSpeedButton(Sender: TObject);

var  a,k, x : integer;
    maxi, mini : integer;
begin


Randomize;

a:=0;


for i:= 1 to 8 do
for K:=1 to 8 do begin
a:=Random(1001);
Stringgrid1.Cells[i,K]:=floattostr(a);
 end;

 maxi:=0;
 mini:=1000;

for i:=1 to 8 do
for K:=1 to 8 do begin
        if strtoint(Stringgrid1.Cells[i,k])>Maxi then Maxi:=strtoint(Stringgrid1.Cells[i,k]);
        if strtoint(Stringgrid1.Cells[i,k])<mini then mini:=strtoint(Stringgrid1.Cells[i,k]);

       end;
       //Ausgabe Min und Max
       Edit1.Text:=inttostr(Maxi);
       Edit2.Text:=inttostr(mini);
       //min und max in Stringgrid einsetzen
       Stringgrid1.Cells[1,1]:=inttostr(Mini);
       Stringgrid1.Cells[8,8]:=inttostr(Maxi);

end;

end.
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Fr 06.11.09 10:50 
Hallo,

und erstmal :welcome: in der EE.

Überdenke noch mal Deinen Ansatz. Die Zahlen werden in das StringGrid eingefügt und danach willst Du sie sortieren und einordnen.

Es ist viel einfacher, vorher die Zufallszahlen in ein Array zu setzen, darin zu sortieren und anschließend die sortierten Zahlen geordnet an das StringGrid zu übergeben. Zum Thema Sortierung findest Du genug hier im Forum.

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
Philipp S Threadstarter
Hält's aus hier
Beiträge: 10

Win XP

BeitragVerfasst: Fr 06.11.09 17:44 
Hallo Lannes,

vielen dank. Habe das Problem nun mit einem Array und Bubblesort gelöst, um die Zahlen dann sortiert in ein Stringgrid einzufügen! In dem Programm kann man nun die Anzahl an Zufallszahlen frei bestimmen, wobei die Anzahl der Rows automatisch angepasst wird.

Hier der "Quelltext":

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
unit Bubblesort;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, StdCtrls, Buttons, ExtCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    BitBtn1: TBitBtn;
    Label1: TLabel;
    Label2: TLabel;
    StringGrid1: TStringGrid;
    Panel1: TPanel;
    Edit2: TEdit;
    procedure BitBtn1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.BitBtn1Click(Sender: TObject);

Var Zufall: array[1..100000of integer;
Anzahl,Merke,i,k,z,s : integer;

begin


Randomize;
Anzahl:=strtoint(Edit1.Text); //Anzahl der zu bestimmenden Zufallszahlen
Stringgrid1.RowCount:=(Anzahl Div 10)+1//Zeilenanpassung

For i:=1 to Anzahl do
        Zufall[i]:=Random(1001); //Bestimmung der Zufallszahlen

For i:=1 to Anzahl-1 do            //Ordnen der Zahlen - A
        For K:=i+1 to Anzahl do
                if Zufall[i]>Zufall[k] then
                begin   Merke:= Zufall[i];
                        Zufall[i]:=Zufall[k];
                        Zufall[k]:=Merke;
                end;               //... - E

Z:=0;   S:=0;    //Zeilen- und Spaltenkoordinaten

For i:=1 to Anzahl do Begin
        Stringgrid1.Cells[S,Z]:=inttostr(Zufall[i]);
if s=9 then begin
                s:=0; Inc(Z);  end
else Inc(S);

end;

       end;




procedure TForm1.FormCreate(Sender: TObject);
begin
showmessage('Anzahl darf den Wert 100.000 nicht überschreiten!');
end;

end.