Autor Beitrag
-delphin-
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 200



BeitragVerfasst: Fr 23.09.05 20:20 
Hallo, ich würde gerne 12 Shapes, deren Höhe der Benutzer am Anfang einstellen darf, nach Höhe sortieren.
Das konkrete Problem besteht darin, die einzelnen Shapes (die Shape1 - Shape12 heißen) anzusprechen und zwar eben allgemein.
Mein Ansatz sieht so aus:
ausblenden 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:
procedure TForm1.Tausche(var a, b: string);
var
  c: string;
begin
  c:=a;
  a:=b;
  b:=c;
end;

procedure TForm1.BSortClick(Sender: TObject);
var Folge: array [1..12of TComponent;
    nichtvertauscht: boolean;
    i: integer;
begin
  repeat nichtvertauscht:=true;
    for i:=0 to 12 do
      If Shape1.Height>Shape2.Height then //<--Diese Zeile verallgemeinern für die Schleife...
      begin
        Tausche(Shape[i], Shape[i+1]); //...oder so
        nichtvertauscht:=false;
      end;
//...und so weiter
end;

end.


Wahrscheinlich muss ich dazu mein array of Tcomponent mit den Shapes vollmachen oder? Wie macht man das? oO
Thx 4 help :D
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 24.09.05 02:15 
Moin!

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:
procedure TForm1.BSortClick(Sender: TObject);
  var
    Shapes: Array [1..12of TShape;
    NichtVertauscht: Boolean;
    i,j: Integer;

  procedure Tausche(Index: Integer);
    var
      h: Integer;
  begin
    h := Shapes[Index].Left;
    Shapes[Index].Left := Shapes[Index+1].Left;
    Shapes[Index+1].Left := h;
    NichtGetauscht := FALSE;
    Application.ProcessMessages;
    // hier ggfs. Pause einbauen, sonst sieht man nix
  end;

begin
  Shapes[1] := Shape1;
  Shapes[2] := Shape2;
  [...]
  Shapes[12] := Shape12;
  for i := 11 downto 1 do begin
    NichtVertauscht := TRUE;
    for j := 1 to i do
      if (Shapes[j].Height > Shapes[j+1].Height) then
        Tausche(j);
    if (NichtVertauscht) then
      Break;
  end;
end;

end.

cu
Narses
-delphin- Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 200



BeitragVerfasst: So 25.09.05 11:34 
funktioniert leider nicht wirklich, er sortiert nur einzelne Teile, d.h. zum Beispiel die ersten 4, dann ein viel kleineres, dann die nächsten 4 sortiert und dann die nächsten 4, also Teile eben nur oO
Grishnak
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 221

Windows XP Home
Delphi 7 PE, Delphi 2005 PE
BeitragVerfasst: So 25.09.05 11:49 
Dein Sortier-Algorithmus stimmt nicht! (wie soll z.B. "NichtVertauscht" false werden?)
Versuch das mal:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
var
  i: integer;
  noXChange: boolean;

[...]

  repeat
    noXChange:=true;
    for i:=1 to 11 do (* Shapes 1 bis 12! werden sortiert *)
      if Shapes[i].Height > Shapes[j+1].Height
        then
          begin
            Tausche(i);
            noXChange:=false;
          end;
  until noXChange;

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



BeitragVerfasst: So 25.09.05 12:01 
ausblenden 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:
procedure TForm1.BSortClick(Sender: TObject);
  var
    Shapes: Array [1..12of TShape;
    NichtVertauscht: Boolean;
    i: Integer;

  procedure Tausche(Index: Integer);
    var
      h: Integer;
  begin
    h := Shapes[Index].Left;
    Shapes[Index].Left := Shapes[Index+1].Left;
    Shapes[Index+1].Left := h;
    NichtVertauscht := FALSE;
  end;

begin
  Shapes[1] := Shape1;
  Shapes[2] := Shape2;
//[...]
  Shapes[12] := Shape12;
  repeat
    NichtVertauscht := TRUE;
    for i := 1 to 11 do
      if (Shapes[i].Height > Shapes[i+1].Height) then begin
        Tausche(i);
        nichtvertauscht:=false;
      end;
  until nichtvertauscht;
end;


Nun kommt er nicht zum Until, d.h. zur Abbruchbedingung, er tauscht also ohne Ende.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 25.09.05 13:31 
Moin!

Hups, stimmt, man muss auch die Höhe vertauschen, nicht die Position des Shapes, das gibt natürlich Unfug. :wink: Hier die getestete Version:
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:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Shape1: TShape;
    Shape2: TShape;
    Shape3: TShape;
    Shape4: TShape;
    Shape5: TShape;
    Shape6: TShape;
    Shape7: TShape;
    Shape8: TShape;
    Shape9: TShape;
    Shape10: TShape;
    Shape11: TShape;
    Shape12: TShape;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
  var
    Shapes: Array [1..12of TShape;
    NichtVertauscht: Boolean;
    i,j: Integer;

  procedure Tausche(Index: Integer);
    var
      h: Integer;
  begin
    h := Shapes[Index].Height;
    Shapes[Index].Height := Shapes[Index+1].Height;
    Shapes[Index+1].Height := h;
    NichtVertauscht := FALSE;
  end;

  procedure Pause(const ms: Cardinal);
    var
      t: Cardinal;
  begin
    t := GetTickCount;
    while ((GetTickCount -t) < ms) do
      Application.ProcessMessages;
  end;

begin
  Button1.Enabled := FALSE;
  Shapes[1] := Shape1;
  Shapes[2] := Shape2;
  Shapes[3] := Shape3;
  Shapes[4] := Shape4;
  Shapes[5] := Shape5;
  Shapes[6] := Shape6;
  Shapes[7] := Shape7;
  Shapes[8] := Shape8;
  Shapes[9] := Shape9;
  Shapes[10] := Shape10;
  Shapes[11] := Shape11;
  Shapes[12] := Shape12;
  // zufällige Höhe setzen
  for i := 1 to 12 do
    Shapes[i].Height := Random(Form1.ClientHeight -100) +30;
  // sortieren
  for i := 11 downto 1 do begin
    NichtVertauscht := TRUE;
    for j := 1 to i do
      if (Shapes[j].Height > Shapes[j+1].Height) then
        Tausche(j);
    if (NichtVertauscht) then
      Break;
    Pause(500);
  end;
  Button1.Enabled := TRUE;
end;

end.

Und das DFM als Text:
ausblenden volle Höhe 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:
object Form1: TForm1
  Left = 982
  Top = 107
  Width = 600
  Height = 541
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poDesktopCenter
  PixelsPerInch = 96
  TextHeight = 13
  object Shape1: TShape
    Left = 16
    Top = 72
    Width = 32
    Height = 65
  end
  object Shape2: TShape
    Left = 64
    Top = 72
    Width = 32
    Height = 65
  end
  object Shape3: TShape
    Left = 112
    Top = 72
    Width = 32
    Height = 65
  end
  object Shape4: TShape
    Left = 160
    Top = 72
    Width = 32
    Height = 65
  end
  object Shape5: TShape
    Left = 208
    Top = 72
    Width = 32
    Height = 65
  end
  object Shape6: TShape
    Left = 256
    Top = 72
    Width = 32
    Height = 65
  end
  object Shape7: TShape
    Left = 304
    Top = 72
    Width = 32
    Height = 65
  end
  object Shape8: TShape
    Left = 352
    Top = 72
    Width = 32
    Height = 65
  end
  object Shape9: TShape
    Left = 400
    Top = 72
    Width = 32
    Height = 65
  end
  object Shape10: TShape
    Left = 448
    Top = 72
    Width = 32
    Height = 65
  end
  object Shape11: TShape
    Left = 496
    Top = 72
    Width = 32
    Height = 65
  end
  object Shape12: TShape
    Left = 544
    Top = 72
    Width = 32
    Height = 65
  end
  object Button1: TButton
    Left = 16
    Top = 16
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
end

Den ersten Versuch hatte ich aus dem Kopf getippert (wie man an dem Variablen-Namen-Fehler sieht...). :wink:

cu
Narses
-delphin- Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 200



BeitragVerfasst: So 25.09.05 17:22 
habs noch ein wenig geändert z.b. den bubblesort, aber danke vielmals, funktioniert wunderbar =) Als dankeschön kannste dir hier das programm runterladen, einfach eine kleingehaltene, aber wie ich finde gelungene BubbleSort-Visualisierung (;
Einloggen, um Attachments anzusehen!
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 25.09.05 21:42 
Moin!

user profile icon-delphin- hat folgendes geschrieben:
aber danke vielmals, funktioniert wunderbar =)

Kein Thema. :wink:

user profile icon-delphin- hat folgendes geschrieben:
Als dankeschön kannste dir hier das programm runterladen,

Mich würde mehr der Quelltext interessieren... :D Ne EXE kann ich dann auch selbst draus machen. :mrgreen:

cu
Narses