| Autor |
Beitrag |
boozzz
      
Beiträge: 66
Win XP, Win 2000, SuSe Linux
|
Verfasst: Do 05.01.06 19:48
Hi,
beim sortieren von Werten, die aus einer Textdatei gelesen werden, kommt bei mir immer folgende Fehlermeldung (Fehlermeldung erscheint immer beim Klick auf den Button "bestenlistebutton"):
| Zitat: | | Im Projekt Project1.exe ist eine Exception der Klasse EStackOverflow aufgetreten. Meldung: ‚Stack-Überlauf’. Prozess wurde angehalten. Mit Einzelne Anweisung oder Start fortsetzen. |
Der folgende Quellcode-Ausschnitt liest aus jeder zweiten Zeile der Textdatei "memory" einen Integer-Wert und sortiert alle Werte der Größe nach.
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:
| procedure TForm1.bestenlistebuttonClick(Sender: TObject); var ende,zwischenspeicher,lauefer,x,y:integer; platz: array[1..10000] of integer; begin bestenliste.Show;
highscore:=TStringList.Create; if FileExists ('memory.txt') then highscore.LoadFromFile('memory.txt'); ende:=highscore.Count;
zwischenspeicher:=0; platz[1]:=StrToInt(highscore[0]);
for x:=1 to 10 do begin lauefer:=0; repeat zwischenspeicher:=StrToInt(highscore[lauefer]); lauefer:=lauefer+2;
if zwischenspeicher>platz[x] then begin platz[x+9]:=platz[x+8]; platz[x+8]:=platz[x+7]; platz[x+7]:=platz[x+6]; platz[x+6]:=platz[x+5]; platz[x+5]:=platz[x+4]; platz[x+4]:=platz[x+3]; platz[x+3]:=platz[x+2]; platz[x+2]:=platz[x+1]; platz[x+1]:=platz[x]; platz[x]:=zwischenspeicher; end;
until lauefer=ende; end;
bestenliste.Label1.Caption:=IntToStr(platz[1]); bestenliste.Label2.Caption:=IntToStr(platz[2]); bestenliste.Label3.Caption:=IntToStr(platz[3]); bestenliste.Label4.Caption:=IntToStr(platz[4]); bestenliste.Label5.Caption:=IntToStr(platz[5]); bestenliste.Label6.Caption:=IntToStr(platz[6]); bestenliste.Label7.Caption:=IntToStr(platz[7]); bestenliste.Label8.Caption:=IntToStr(platz[8]); bestenliste.Label9.Caption:=IntToStr(platz[9]); bestenliste.Label10.Caption:=IntToStr(platz[10]);
highscore.Free; end; |
Kann mir jemand sagen, wo der Fehler ist?
|
|
Bruder Lal
      
Beiträge: 31
WIN XP
Delphi 2005
|
Verfasst: Fr 06.01.06 15:23
Wie groß kann memory.txt werden? Ein Fehler wird auftreten wenn es beispielsweise größer als platz wird(von der Anzahl der strings her). Wenn es(wie ich vermute) viel kleiner als platz ist, würde zu viel Speicherplatz genutzt. Wie wär's mit:
Delphi-Quelltext 1: 2: 3: 4: 5: 6: 7:
| var platz: array of integer; ... Setlength(platz, ende+11) ... repeat ... until lauefer>=ende |
|
|
boozzz 
      
Beiträge: 66
Win XP, Win 2000, SuSe Linux
|
Verfasst: Fr 06.01.06 15:47
Danke Bruder Lal, das Problem ist gelöst und es lag an den ungeraden Ende-Werten (hätte ich auch als Delphi-Neuling selber draufkommen können  ).
Jetzt hab ich aber ein neues Problem. Es soll nun von unten nach oben sortiert werden (wertniedrigste Zahl zuerst):
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:
| procedure TForm1.bestenlistebuttonClick(Sender: TObject); var ende,zwischenspeicher,lauefer,x:integer; platz: array[1..10000] of integer; begin bestenliste.Show;
highscore:=TStringList.Create; if FileExists ('memory.txt') then highscore.LoadFromFile('memory.txt');
ende:=highscore.Count;
lauefer:=0;
for x:=1 to 10 do begin platz[x]:=StrToInt(highscore[lauefer]); lauefer:=lauefer+2; end;
for x:=1 to 10 do begin lauefer:=2; repeat zwischenspeicher:=StrToInt(highscore[lauefer]); if zwischenspeicher<platz[x] then begin platz[x+9]:=platz[x+8]; platz[x+8]:=platz[x+7]; platz[x+7]:=platz[x+6]; platz[x+6]:=platz[x+5]; platz[x+5]:=platz[x+4]; platz[x+4]:=platz[x+3]; platz[x+3]:=platz[x+2]; platz[x+2]:=platz[x+1]; platz[x+1]:=platz[x]; platz[x]:=zwischenspeicher; end else if zwischenspeicher<platz[x+1] then begin platz[x+9]:=platz[x+8]; platz[x+8]:=platz[x+7]; platz[x+7]:=platz[x+6]; platz[x+6]:=platz[x+5]; platz[x+5]:=platz[x+4]; platz[x+4]:=platz[x+3]; platz[x+3]:=platz[x+2]; platz[x+2]:=platz[x+1]; platz[x+1]:=zwischenspeicher; end else if zwischenspeicher<platz[x+2] then begin platz[x+9]:=platz[x+8]; platz[x+8]:=platz[x+7]; platz[x+7]:=platz[x+6]; platz[x+6]:=platz[x+5]; platz[x+5]:=platz[x+4]; platz[x+4]:=platz[x+3]; platz[x+3]:=platz[x+2]; platz[x+2]:=zwischenspeicher; end else if zwischenspeicher<platz[x+3] then begin platz[x+9]:=platz[x+8]; platz[x+8]:=platz[x+7]; platz[x+7]:=platz[x+6]; platz[x+6]:=platz[x+5]; platz[x+5]:=platz[x+4]; platz[x+4]:=platz[x+3]; platz[x+3]:=zwischenspeicher; end else if zwischenspeicher<platz[x+4] then begin platz[x+9]:=platz[x+8]; platz[x+8]:=platz[x+7]; platz[x+7]:=platz[x+6]; platz[x+6]:=platz[x+5]; platz[x+5]:=platz[x+4]; platz[x+4]:=zwischenspeicher; end else if zwischenspeicher<platz[x+5] then begin platz[x+9]:=platz[x+8]; platz[x+8]:=platz[x+7]; platz[x+7]:=platz[x+6]; platz[x+6]:=platz[x+5]; platz[x+5]:=zwischenspeicher; end else if zwischenspeicher<platz[x+6] then begin platz[x+9]:=platz[x+8]; platz[x+8]:=platz[x+7]; platz[x+7]:=platz[x+6]; platz[x+6]:=zwischenspeicher; end else if zwischenspeicher<platz[x+7] then begin platz[x+9]:=platz[x+8]; platz[x+8]:=platz[x+7]; platz[x+7]:=zwischenspeicher; end else if zwischenspeicher<platz[x+8] then begin platz[x+9]:=platz[x+8]; platz[x+8]:=zwischenspeicher; end else if zwischenspeicher<platz[x+9] then begin platz[x+9]:=zwischenspeicher; end;
lauefer:=lauefer+2; until (lauefer=ende) or (lauefer+1=ende); end; Form1.Edit1.Text:=IntToStr(zwischenspeicher); bestenliste.Label1.Caption:=IntToStr(platz[1]); bestenliste.Label2.Caption:=IntToStr(platz[2]); bestenliste.Label3.Caption:=IntToStr(platz[3]); bestenliste.Label4.Caption:=IntToStr(platz[4]); bestenliste.Label5.Caption:=IntToStr(platz[5]); bestenliste.Label6.Caption:=IntToStr(platz[6]); bestenliste.Label7.Caption:=IntToStr(platz[7]); bestenliste.Label8.Caption:=IntToStr(platz[8]); bestenliste.Label9.Caption:=IntToStr(platz[9]); bestenliste.Label10.Caption:=IntToStr(platz[10]);
highscore.Free; end; |
Mein jetztiges Problem: In der Bestenliste wird nur die niedrigste Zahl gespeichert. Das heißt: platz[1], platz[2], platz[3],... haben alle den selben (niedrigsten) Wert.
|
|
Gausi
      
Beiträge: 8554
Erhaltene Danke: 481
Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
|
Verfasst: Fr 06.01.06 15:50
Kleiner Tipp, auch wenn er gemein ist: Such mal im Forum nach Sortierverfahren wie z.B. Bubblesort oder Quicksort. Da findest du dann Sortierprozeduren, die auch wirklich funktionieren. Was du da in dem Code veranstaltest, und warum das sortieren sollte, raff ich nicht so ganz.
_________________ We are, we were and will not be.
|
|
boozzz 
      
Beiträge: 66
Win XP, Win 2000, SuSe Linux
|
Verfasst: Fr 06.01.06 19:10
habs alleine hinbekommen:
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:
| procedure TForm1.bestenlistebuttonClick(Sender: TObject); var ende,zwischenspeicher,lauefer,lauefer2,x,y,wertespeicher,wabbel:integer; personenspeicher: string; platz: array[1..100] of integer; person: array[1..100] of string; begin bestenliste.Show;
highscore:=TStringList.Create; if FileExists ('memory.txt') then begin highscore.LoadFromFile('memory.txt'); ende:=highscore.Count;
lauefer:=0; lauefer2:=1;
for x:=1 to 10 do begin if (highscore.Count/2>=x) then begin platz[x]:=StrToInt(highscore[lauefer]); lauefer:=lauefer+2; person[x]:=highscore[lauefer2]; lauefer2:=lauefer2+2; end; end;
for y:=1 to 10 do begin for x:=1 to 10 do begin if (highscore.Count/2>=x) then begin if platz[x+1]<platz[x] then begin wertespeicher:=platz[x]; personenspeicher:=person[x]; platz[x]:=platz[x+1]; person[x]:=person[x+1]; platz[x+1]:=wertespeicher; person[x+1]:=personenspeicher; end; end; end; end;
bestenliste.Label1.Caption:=IntToStr(platz[2]); bestenliste.Label2.Caption:=IntToStr(platz[3]); bestenliste.Label3.Caption:=IntToStr(platz[4]); bestenliste.Label4.Caption:=IntToStr(platz[5]); bestenliste.Label5.Caption:=IntToStr(platz[6]); bestenliste.Label6.Caption:=IntToStr(platz[7]); bestenliste.Label7.Caption:=IntToStr(platz[8]); bestenliste.Label8.Caption:=IntToStr(platz[9]); bestenliste.Label9.Caption:=IntToStr(platz[10]);
bestenliste.Label39.Caption:=person[2]; bestenliste.Label38.Caption:=person[3]; bestenliste.Label37.Caption:=person[4]; bestenliste.Label36.Caption:=person[5]; bestenliste.Label35.Caption:=person[6]; bestenliste.Label34.Caption:=person[7]; bestenliste.Label33.Caption:=person[8]; bestenliste.Label32.Caption:=person[9]; bestenliste.Label31.Caption:=person[10];
highscore.Free; end; end; |
|
|
boozzz 
      
Beiträge: 66
Win XP, Win 2000, SuSe Linux
|
Verfasst: So 05.03.06 17:03
Hi,
der von mir gepostete Quellcode hat irgendwo einen Fehler, den ich aber nicht finde. Das Problem besteht darin, dass nur die ersten 10 Werte aus der Textdatei geladen und sortiert werden. Wenn die Textdatei mehr als 10 Einträge hat, werden die restlichen nicht mehr in "highscore" geladen!
Würde mich freuen, wenn mir jemand weiterhelfen kann.
Gruß,
boozzz
PS: Die Textdatei hat folgendes Aussehen:
Quelltext 1: 2: 3: 4: 5: 6:
| 22 Franz 14 Michael 35 Jürgen |
Quelltext 1: 2: 3: 4:
| Punktzahl von Spieler 1 Name von Spieler 1 Punktzahl von Spieler 2 Name von Spieler 2 |
|
|
|