Autor Beitrag
TheAxeEffect
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 37



BeitragVerfasst: Mi 13.06.07 17:57 
hallo. kann mir jemand bei meinem unfertigen bubblesort algorithmus helfen?
habe folgendes problem: ich möchte die zufallszahlen im memo1.lines .... sortieren.
wenn ich auf button2(sortierbutton) klicke, meldet er mir: ..."is not a valid integer value"...lasse ich allerdings das a:=strtoint(memo1.lines[j])bla weg, und vergleiche memo1.lines[j] direkt mit memo1.lines[j+1](also die strings, nicht die zahlen), geht alles wunderbar..er sortiert mir dann halt "falsch" dh. "23" nach "3" weil ja "3" größer als "2" is usw...wandle ich die strings in ne zahl um gehts nich... hab keine ahnung was da los is. kann mir wer helfen?
hier der code(wie ich ihn optiemier weiß ich selber, is nur so nen "betatest" sozusagen):
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TForm1.Button2Click(Sender: TObject);
var i,j,a,b:integer;

begin

for i:=1 to memo1.lines.count do begin

        for j:=1 to memo1.Lines.count do begin
                a:=strtoint(memo1.Lines[j]);
                b:=strtoint(memo1.Lines[j+1]);
                if a < b then begin
                memo1.Lines.exchange(j,(j+1));



Moderiert von user profile iconGausi: Topic aus Delphi Language (Object-Pascal) / CLX verschoben am Mi 13.06.2007 um 18:09
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mi 13.06.07 18:00 
Moin und :welcome: im Forum!

Probier´s mal statt StrToInt()einfach mit StrToIntDef(,0)aus. ;)

cu
Narses

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



BeitragVerfasst: Mi 13.06.07 18:07 
wenn das so richtig is, wie du meinst, gehts nich^^:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TForm1.Button2Click(Sender: TObject);
var i,j,a,b:integer;

begin

for i:=1 to memo1.lines.count do begin

        for j:=1 to memo1.Lines.count do begin
                a:=strtointdef(memo1.Lines[j],0);
                b:=strtoint(memo1.Lines[j+1]);
                if a < b then begin
                memo1.Lines.exchange(j,(j+1));

ps: danke fürs willkommenheißen^^
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mi 13.06.07 18:09 
Hallo und :welcome: auch von mir!

Deine Schleife läuft falsch ;-) Die muss von 0 (es sei denn, du hast ne Überschrift-Zeile drin) bis Count-2 laufen (die letzte Zeile ist Count-1, und in der Schleife greifst du auf j+1 zu, daher noch eins weniger).

Zusätzlich musst du noch darauf achten, dass in allen Zeilen gültige Zahlen drin stehen - es passiert recht schnell, dass am Ende irgendwelche Leerzeilen stehen. Das mit dem StrToIntDef ist eine Möglichkeit, Fehler abzufangen. Aber du musst beide StrToInts ersetzen ;-)

_________________
We are, we were and will not be.
TheAxeEffect Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 37



BeitragVerfasst: Mi 13.06.07 18:12 
argh, ich depp^^
und ja: ich hab ne überschrift-zeile^^ dass memox.lines von 0-... gehen, weiß ich, aber danke, hätte mir sicher geholfen, wenn ichs nicht gewusst hätte^^
geht jetzt. eine frage noch: was bedeutet die '0' bei strtointdef(x,0)
ps: auch dir danke für die "gastfreundschaft"^^


Zuletzt bearbeitet von TheAxeEffect am Mi 13.06.07 18:14, insgesamt 1-mal bearbeitet
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Mi 13.06.07 18:12 
Probiers mal mit F1. ;)