Autor Beitrag
GericasS
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 540

Windows Vista Home Premium
D2010, VisualStudio2008
BeitragVerfasst: Mo 08.12.08 20:58 
Abend,

ich habe folgendes Problem :

2 Array of Integer (Für eine Aritkel Nummer und den Lagerbestand der Ware)
1 Array of String (Artikelbeschreibung)

jetzt möchte ich, wenn ich eins der beiden Integer Arrays mit meinem Bubblesort sortiere und im StringGrid ausgeben ( vorher sind die Daten alle unsortiert), das die Werte der beiden anderen Arrays jeweils auch sortiert werden und so die zusammen gehörenden Werte auch in der gleichen Reihe im StringGrid stehen.

Ich hoffe ich konnte mich verständlich ausdrücken :roll:

Ich dachte da an sowas etwas

ausblenden Delphi-Quelltext
1:
procedure Swap(var X,Y : Integer; S : String);					


Mfg,

GericasS

_________________
Alexander N.
Neue Bewaffnung Amilo xi2428 T9300
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 09.12.08 00:38 
Moin!

Warum die Daten verstreuen, mach doch ein Record aus deinem Artikel, der aus Nummer, Bestand und Name besteht. Dann ist das Sortieren/Tauschen doch ganz einfach. :idea: ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
GericasS Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 540

Windows Vista Home Premium
D2010, VisualStudio2008
BeitragVerfasst: Do 18.12.08 19:38 
Abend,

das Record ist glaube nicht das was ich brauche, folgendes :

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
var
  Form2: TForm2;
  artnA : array [0..5of Integer ;
  artbA : array [0..5of String ;
  lagbA : array [0..5of Integer ;


Diese drei array habe ich, werden auch alle mit den Werten die sie brauchen befüllt.
Jetzt habe ich ein StringGrid wo diese ausgeben werden.

In der ersten Zeile die Artikel Nummer in der zweiten die Artikel Bezeichnung und in der letzten der Lagerbestand.
Jetzt möchte ich, das wenn ich angebe das nach Lagerbestand mit meinem Bubblesort sortiert werden soll, sich die zu den einzelnen Lagerbeständen gehörenden Werte ( der anderen Arrays ) mit verschieben also immer die zusammengehörenden Werte in einer Zeile stehen.

Grüße

GericasS

_________________
Alexander N.
Neue Bewaffnung Amilo xi2428 T9300
jfheins
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 918
Erhaltene Danke: 158

Win 10
VS 2013, VS2015
BeitragVerfasst: Do 18.12.08 19:52 
Da die Array ja globale Variabelen sind: Übergebe der Swap-Funktion nur die Indizies und tausche dann die Werte in allen Arrays.

Besser:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
type
TArtikel = record
  artn: Integer;
  artb: String;
  lagb: Integer;
end;

var 
  Form2: TForm2; 
  Artikel : array [0..5of TArtikel;
GericasS Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 540

Windows Vista Home Premium
D2010, VisualStudio2008
BeitragVerfasst: Do 18.12.08 21:07 
user profile iconjfheins hat folgendes geschrieben Zum zitierten Posting springen:
Da die Array ja globale Variabelen sind: Übergebe der Swap-Funktion nur die Indizies und tausche dann die Werte in allen Arrays.

Besser:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
type
TArtikel = record
  artn: Integer;
  artb: String;
  lagb: Integer;
end;

var 
  Form2: TForm2; 
  Artikel : array [0..5of TArtikel;


Also meine Swap Funktion ist ja mein BubbleSort oder ?

_________________
Alexander N.
Neue Bewaffnung Amilo xi2428 T9300
platzwart
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1054
Erhaltene Danke: 78

Win 7, Ubuntu 9.10
Delphi 2007 Pro, C++, Qt
BeitragVerfasst: Do 18.12.08 21:49 
Nein! Swap vertauscht in der Regel nur zwei Werte. BubbleSort greift auf Swap zurück, um zwei Werte zu vertauschen, falls dies geschehen soll...

_________________
Wissenschaft schafft Wissenschaft, denn Wissenschaft ist Wissenschaft, die mit Wissen und Schaffen Wissen schafft. (myself)
GericasS Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 540

Windows Vista Home Premium
D2010, VisualStudio2008
BeitragVerfasst: Do 18.12.08 23:07 
user profile iconplatzwart hat folgendes geschrieben Zum zitierten Posting springen:
Nein! Swap vertauscht in der Regel nur zwei Werte. BubbleSort greift auf Swap zurück, um zwei Werte zu vertauschen, falls dies geschehen soll...


Sry, stimmt hatte ich vergessen...btw wäre es möglich mit Swap gleich alle Werte aufeinmal zu tauschen ?

_________________
Alexander N.
Neue Bewaffnung Amilo xi2428 T9300
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19339
Erhaltene Danke: 1752

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Do 18.12.08 23:14 
Wie meinst du das? Swap tauscht ja nur einen Eintrag mit einem anderen. Das Vertauschen wird ja dann erst beim Ablaufen des Algorithmus durchgeführt, und das mehrfach hintereinander.
platzwart
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1054
Erhaltene Danke: 78

Win 7, Ubuntu 9.10
Delphi 2007 Pro, C++, Qt
BeitragVerfasst: Do 18.12.08 23:47 
Genauso ist es. Du schreibst einen Algorithmus zum Sortieren. DIESEN rufst du dann genau einmal auf. Intern wird dieser Algorithmus immer wieder SWAP aufrufen...

Ich hab das Gefühl, deine Frage ist eher, wie ein Sortieralgorithmus ausschaut?!?

_________________
Wissenschaft schafft Wissenschaft, denn Wissenschaft ist Wissenschaft, die mit Wissen und Schaffen Wissen schafft. (myself)
GericasS Threadstarter
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 540

Windows Vista Home Premium
D2010, VisualStudio2008
BeitragVerfasst: Fr 19.12.08 07:48 
Gute Morgen =),

nein also den Algo. hab ich schon, das Programm habe ich hochgeladen schaut es euch mal an :wink:
ich hoffe dann kommt mein Problem verständlicher rüber..

LG

GericasS
Einloggen, um Attachments anzusehen!
_________________
Alexander N.
Neue Bewaffnung Amilo xi2428 T9300
jfheins
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 918
Erhaltene Danke: 158

Win 10
VS 2013, VS2015
BeitragVerfasst: Fr 19.12.08 10:30 
Aha - beim Klick auf sortieren passiert zwar nichts, aber immerhin ist der Algo schon da uund der Button auch :mrgreen:

Ich würde folgendes vorschlagen:
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:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    StringGrid1: TStringGrid;
    Edit1: TEdit;
    Button1: TButton;
    Edit2: TEdit;
    Button2: TButton;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    Button3: TButton;
    Button4: TButton;
    Edit3: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Display();
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;


TArtikel = record
  artNr: Integer;
  artB: String;
  lagB: Integer;
end;

var
  Form2: TForm2;
  ArtikelListe: array[0.5of TArtikel:
  count : Integer;

implementation

{$R *.dfm}

procedure TForm1.Display();
var
  i: Integer;
begin

  for i = 0 to 5 do
  begin
    StringGrid1.Cells[0,i] := ArtikelListe[i].artNr; 
    StringGrid1.Cells[1,i] := ArtikelListe[i].artB;           
    StringGrid1.Cells[2,i] := ArtikelListe[i].lagB;
  end;
end;

procedure LoadStringGridFromFile(StringGrid: TStringGrid; const FileName: string);
var
  F: TStringList;
  i: Integer;
begin
  F := TStringList.Create;
  try
    F.LoadFromFile(FileName);
    StringGrid.RowCount := StrToInt(F[0]);
    StringGrid.ColCount := StrToInt(F[1]);
    for i := 0 to (StringGrid.RowCount - 1do
      StringGrid.Rows[i].CommaText := F[i + 2];
  finally
    F.Free;
  end;
end;

procedure SaveStringGridToFile(StringGrid: TStringGrid; const FileName: string);
var
  F: TStringList;
  i: Integer;
begin
  F := TStringList.Create;
  try
    F.Add(IntToStr(StringGrid.RowCount));
    F.Add(IntToStr(StringGrid.ColCount));
    for i := 0 to (StringGrid.RowCount - 1do
      F.Add(StringGrid.Rows[i].CommaText);
    F.SaveToFile(FileName);
  finally
    F.Free;
  end;
end;

procedure BubbleSort(var data: array of TArtikel );
var
  ready : boolean ;
  i,temp : Integer ;
begin
  repeat
    ready := true ;
    for i := 0 to 5 -1 do
      begin
        if data[i].artNr > data[i+1].artNr then
        begin
          temp := data[i];
          data[i]:= data[i+1];
          data[i+1] := temp ;
          ready := false ;
        end;
      end;
  until ready  ;
end;

procedure TForm2.Button1Click(Sender: TObject);
var
artbe : String ;
artnr,lagbe : Integer ;
begin
  inc(count);                                   //Zähler ++
  
  ArtikelListe[count].artNr := StrToIntDef(Edit1.Text,0);
  ArtikelListe[count].artB := Edit2.Text
  ArtikelListe[count].lagB := StrToIntDef(edit3.Text,0)
  
  Display();
  
  if count = 5 then                             //Datenbanklimit prüfen...
    begin
      showmessage('Datenbank ist voll');
    end;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
  BubbleSort(ArtikelListe);
  Display();  
end;

procedure TForm2.Button4Click(Sender: TObject);
begin
  LoadStringGridFromFile(StringGrid1,'StringGridData');
end;

procedure TForm2.FormCreate(Sender: TObject);
var
i : Integer ;
begin
  StringGrid1.Cells[0,0]:='Art.-Nr';
  StringGrid1.Cells[1,0]:='Art.-Bezeichnung';
  StringGrid1.Cells[2,0]:='Lagerbestand';
end;

end.


Die Datenhaltung erfolgt nun in dem Array mit den records und nicht mehr in dem Stringgrid.

Die Laden/Speichern-Prozeduren müssen noch angepasst werden ;)

P.S. Es compilier bestimmt nicht, da ich hier kein Delphi habe und das in Notepad editiert habe. Das Zeil sollte trotzdem erkennbar sein ... :mrgreen:
XUDO
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45

Win-98SE, Win-XP/1
Delphi-7P, Turbo-Pascal-6
BeitragVerfasst: Sa 20.12.08 15:43 
Hallo GericaS,

hier kommt ein ganz anderer Vorschlag,
der auch bei gleichen Beständen die Artikel auseinanderhält.
Ich hoffe, die Kommentare reichen fürs Verständnis aus.

XUDO
Einloggen, um Attachments anzusehen!