Autor Beitrag
MJ87
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21

Win XP
D5 Pro
BeitragVerfasst: Di 27.09.05 20:42 
Hallo,

ich will ein Programm programmieren, wo ich durch das Verfahren "Sortieren durch Einfügen" zahle sortieren kann, habe jetzt ein Problem, wenn ich die Zahlen sortieren will, dann zeigt das Programm mir in der Ausgabe(Memo1) nur nullen an.

Mein Quelltext sieht so aus:
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:
const
maxanzahl =5000;

var
  TForm1: TTForm1;
  Bereich:0..maxanzahl;
  Elementtyp:integer;
  Liste:array[0..maxanzahl] of integer;
  n:integer;
implementation

{$R *.DFM}

procedure TTForm1.BeendenClick(Sender: TObject);
begin
close();
end;

procedure TTForm1.SortierenClick(Sender: TObject);
Begin
N:= memo1.lines.count;
sort();

end;


procedure TTForm1.Sort();
var i,j:integer;
    hilf:integer;
    platzgef:boolean;
    k : Integer;
begin
  for i:= 1 to n do
    begin
      hilf:= liste[i];
      platzgef:=false;
      j:= i-1;
        While (j>0and not platzgef do
        begin
          if hilf < liste[j] then
            begin
            liste[j+1] := liste[j];
            j:=i-1
          end
          else
            platzgef:=true;
        end;
        liste[j+1]:=hilf;
      end;
      memo1.clear;
    for k:=0 to High(liste) do
    memo1.lines.add(IntToStr(liste[k]));
    end;


procedure TTForm1.EinfuegenClick(Sender: TObject);
begin
memo1.lines.Add(Edit1.Text);
edit1.text:='';
end;

procedure TTForm1.FormCreate(Sender: TObject);
begin
memo1.clear;
edit1.text:='';
end;

end.


Gruß MJ87


Moderiert von user profile iconChristian S.: Topic aus Multimedia / Spiele / Grafik verschoben am Di 27.09.2005 um 20:46
Grishnak
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 221

Windows XP Home
Delphi 7 PE, Delphi 2005 PE
BeitragVerfasst: Di 27.09.05 21:34 
Änder dein Sortier-Algorithmus mal wie folgt ab:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
var
  i, j, hilf: integer;
begin
  for i:=0 to n-1 do
    begin
      j:=i;
      hilf:=liste[i];
      while (j > 0and (liste[j-1] > hilf) do
        begin
          liste[j]:=liste[j-1];
          Dec(j);
        end;
      liste[j]:=hilf;
    end;


PS: Immer dran denken, ein TMemo-, TList-, TListBox-, etc. -Object hat zwar "Count" Einträge, aber die Indizes gehen von "0" bis "Count-1" (nicht von "1" bis "Count")!

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

Win XP
D5 Pro
BeitragVerfasst: Di 27.09.05 21:43 
damit gehts auch nicht, bekomme immer noch im memofeld nur nullen angeziegt.

Gruß MJ87
Horst_H
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1654
Erhaltene Danke: 244

WIN10,PuppyLinux
FreePascal,Lazarus
BeitragVerfasst: Di 27.09.05 22:54 
Hallo,

wo liest Du denn integer-Zahlen in deine Liste ein?
Die Liste ist bei der Erstellung mit 0 belegt.

Also musst Du irgendwie String-Zahlen umwandelm.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TTForm1.EinfuegenClick(Sender: TObject);   
begin
n := memo1.lines.count;
try
  Liste[n] := StrToInt(Edit1.Text);
  memo1.lines.Add(Edit1.Text);
except
  Showmessage('Da ist was schiefgelaufen');
  memo1.lines.count:= n;{die Zeile in memo1 entfernen}
end;
  
end;



So oder so aehnlich.

Gruss Horst