[Delphi] Proszę o pomoc [losowanie, plik txt, sortowanie]

0

W problemie występują trzy procedury:

  1. Losowanie liczb i wgranie ich do pliku txt.
  2. Podzielenie pliku na dwa oddzielne txt.
  3. Posortowanie liczb w tych plikach od max do min.
    Przesyłam dotychczas nieskończony kod, proszę podpowiedzi, gdzie są błedy.

type
tablica = array of byte;
TForm2 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Button2: TButton;
TrackBar1: TTrackBar;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);

private
Procedure sort( var t:tablica);
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation

{$R *.DFM}

{ TForm2 }

procedure TForm2.sort(var t: tablica);
var
i, j : Integer;
Temp : Integer;
begin
for I := 1 to High(T) do
begin
for J := 1 to High(T) do
begin
if (T[j] > T[j-1]) then
begin
Temp := T[j];
T[j] := T[j-1];
T[j-1] := Temp;
end;
end;
end;
end;

procedure TForm2.Button1Click(Sender: TObject);
var
t, ta: Tablica;
Tf : File of byte;
F, P : TextFile;
i, j, x, y : Integer;
a, b : Byte;
Size : Longint;
begin
AssignFile(P, 'c:\plik2.txt'); Rewrite(P);
AssignFile(F, 'c:\plik1.txt'); Rewrite(F);
AssignFile(Tf, 'c:\plik.txt'); Reset(Tf);
Size:=FileSize(Tf);
seek(Tf, size div 2);
x:=FilePos(Tf);
SetLength(t, TrackBar1.Position);
label1.Caption:=(IntToStr(size));
label2.Caption:=(IntToStr(x));
try
for i:=0 to TrackBar1.Position do
begin
read(Tf, a);

t[i]:=a;
end;
begin
sort(t);
for i:=1 to High(t) do
begin
write(F, IntToStr(t[i]));
write(F,' ');
end;
end;
begin
seek(Tf, size);
y:=(TrackBar1.Position);
seek(Tf, (size div 2)+(1));
SetLength(ta, TrackBar1.Position);
for j:=1 to y do
begin
read(Tf, b);

ta[j]:=b;
end;
begin
sort(ta);
for i:=1 to High(ta) do
begin
write(P, IntToStr(ta[i]));
write(p,' ');
end;
end;
end;
finally
CloseFile(Tf);
CloseFile(F);
CloseFile(P);
end;
end;
procedure TForm2.Button2Click(Sender: TObject);
var
Tf : TextFile;
I, J, X, Y : integer;
begin
AssignFile(Tf, 'c:\Plik.txt'); Rewrite(Tf);
label3.Caption:=('Wylosowano '+IntToStr(TrackBar1.Position)+' liczb.');
try
for i:=1 to TrackBar1.Position do begin
X:=random(100);
write(Tf, IntToStr(X));
write(Tf,';');
end;
begin
writeln(Tf);
Append(Tf);
end;
for j:=1 to TrackBar1.Position do begin
Y:=random(100);
write(Tf, IntToStr(Y));
write(Tf,';');
end;
finally
CloseFile(Tf);
end;
end;
end.

0

Prawidlowy kod i dziala:

procedure TForm2.sort(var t: tablica);
var
i, j : Integer;
Temp : Integer;
begin
for I := 1 to High(T) do
begin
for J := 1 to High(T) do
begin
if (T[j] > T[j-1]) then
begin
Temp := T[j];
T[j] := T[j-1];
T[j-1] := Temp;
end;
end;
end;
end;

procedure TForm2.Button1Click(Sender: TObject);
var
t, ta: Tablica;
Tf : File of byte;
F, P : TextFile;
i, j, x, y : Integer;
a, b : Byte;
Size : Longint;
begin
AssignFile(P, 'c:\plik2.txt'); Rewrite(P);
AssignFile(F, 'c:\plik1.txt'); Rewrite(F);
AssignFile(Tf, 'c:\plik.txt'); Reset(Tf);
Size:=FileSize(Tf);
seek(Tf, size div 2);
x:=FilePos(Tf);
SetLength(t, TrackBar1.Position);
label1.Caption:=(IntToStr(size));
label2.Caption:=(IntToStr(x));
try
for i:=0 to TrackBar1.Position do
begin
read(Tf, a);
t[1]:=a; //1 blad poprawiony
end;
begin
sort(t);
for i:=1 to High(t) do
begin
write(F, IntToStr(t[1])); //2 blad poprawiony
write(F,' ');
end;
end;
begin
seek(Tf, size);
y:=(TrackBar1.Position);
seek(Tf, (size div 2)+(1));
SetLength(ta, TrackBar1.Position);
for j:=1 to y do
begin
read(Tf, b);

ta[j]:=b;
end;
begin
sort(ta);
for i:=1 to High(ta) do
begin
write(P, IntToStr(ta[1])); //3 blad poprawiony
write(p,' ');
end;
end;
end;
finally
CloseFile(Tf);
CloseFile(F);
CloseFile(P);
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
Tf : TextFile;
I, J, X, Y : integer;
begin
AssignFile(Tf, 'c:\Plik.txt'); Rewrite(Tf);
label3.Caption:=('Wylosowano '+IntToStr(TrackBar1.Position)+' liczb.');
try
for i:=1 to TrackBar1.Position do begin
X:=random(100);
write(Tf, IntToStr(X));
write(Tf,';');
end;
begin
writeln(Tf);
Append(Tf);
end;
for j:=1 to TrackBar1.Position do begin
Y:=random(100);
write(Tf, IntToStr(Y));
write(Tf,';');
end;
finally
CloseFile(Tf);
end;
end;
end.

Wiecej nie znalazlem :) Te bledy wynikaja z tego ze tablica ma pola, ktore sa numerowane i w tych momentach trzeba sie dokladnie do nich odwolac.
Pomoglem ?

--
Umiem troche, ale nie wszystko :) a na koncu i tak BLUE SCREEN

0

Aha, zamiast tych jedynek moze byc jakas zmienna

np. t[1] = t[i]

--
Umiem troche, ale nie wszystko :) a na koncu i tak BLUE SCREEN

0

Kurcze prawidlowo mialo byc

np.
var i:integer;

"t[1] = t[ i ];"

Za pierwszym razem mi obcielo :)

--
Umiem troche, ale nie wszystko :) a na koncu i tak BLUE SCREEN

1 użytkowników online, w tym zalogowanych: 0, gości: 1