Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TEXTOVÉ SÚBORY SADA ÚLOH
- Úloha 1
- Komponenty: Memo, Button
- Súbory: Textový súbor pomenovaný vstup.txt
- Kód:
- procedure TForm1.Button1Click(Sender: TObject);
- var
- f: TextFile;
- prazdneRiadky: Array of Integer;
- i, pocetriadkov, pocetznakov, max: Integer;
- riadok: String;
- begin
- // Open file for reading
- AssignFile(f, 'vstup.txt');
- Reset(f);
- // Default values
- i:=1;
- pocetriadkov := 0;
- max := 0;
- pocetznakov := 0;
- while not eof(f) do
- begin
- ReadLn(f, riadok);
- pocetriadkov := pocetriadkov + 1;
- pocetznakov := pocetznakov + Length(riadok);
- // Get the Length of longest array
- if(Length(riadok) > max) then max:= Length(riadok);
- // Empty line
- if(Length(riadok) = 0) then
- begin
- // Iterate the length of array by 1
- SetLength(prazdneRiadky, i);
- // Save number of empty line into array
- prazdneRiadky[i-1] := pocetriadkov;
- Inc(i);
- end;
- end;
- // Output
- Memo1.Lines.Add('Najdlhsi: ' + IntToStr(max));
- Memo1.Lines.Add('Pocet Riadkov: ' + IntToStr(pocetriadkov));
- Memo1.Lines.Add('Pocet Znakov: ' + IntToStr(pocetznakov));
- for i:= 0 to Length(prazdneRiadky) - 1 do
- begin
- Memo1.Lines.Add('Prazdny je ' + IntToStr(prazdneRiadky[i]));
- end;
- Memo1.Lines.Add('Prazdnych je dokopy ' + IntToStr(Length(prazdneRiadky)));
- CloseFile(f);
- end;
- --------------------------------------------------------------------------------------------------
- Úloha 2
- Komponenty: Button, Edit
- Súbory: Textový súbor pomenovaný vstup.txt
- Kód:
- procedure TForm1.Button1Click(Sender: TObject);
- var
- f, new: TextFile;
- vetaa, veta, slogan: String;
- vstup : Array of String;
- i : Integer;
- begin
- // Open file for reading
- AssignFile(f, 'vstup.txt');
- Reset(f);
- AssignFile(new, 'vystup.txt');
- ReWrite(new);
- slogan:= Edit1.Text;
- i:= 1;
- WriteLn(new, 'SEKCIA A');
- // a
- while not eof(f) do
- begin
- ReadLn(f, veta);
- WriteLn(new, veta);
- SetLength(vstup, i);
- vstup[i-1] := veta;
- Inc(i);
- end;
- CloseFile(f);
- WriteLn(new, 'SEKCIA B');
- // b
- for i:= 0 to Length(vstup) - 1 do
- begin
- if(Length(vstup[i]) = 0) then WriteLn(new, slogan)
- else WriteLn(new, vstup[i]);
- end;
- WriteLn(new, 'SEKCIA C');
- // c
- for i:= 0 to Length(vstup) - 1 do
- begin
- if(Length(vstup[i]) <> 0) then WriteLn(new, vstup[i]);
- end;
- WriteLn(new, 'SEKCIA D');
- // d
- for i:= 0 to Length(vstup) - 1 do
- begin
- vetaa := vstup[i];
- if(Length(vetaa) > 0) then
- begin
- if(vetaa[1] = ' ') then Delete(vetaa, 1, 1);
- if(vetaa[Length(vetaa)] = ' ') then Delete(vetaa,Length(vetaa),1);
- WriteLn(new, vetaa);
- end;
- end;
- WriteLn(new, 'SEKCIA E');
- // e
- for i:= Length(vstup) - 1 downto 0 do
- begin
- WriteLn(new, vstup[i]);
- end;
- CloseFile(new);
- end;
- --------------------------------------------------------------------------------------------------
- Úloha 4
- Komponenty: Button, Timer, 3x Label, Image
- Súbory: Textový súbor pomenovaný hada.txt
- Kód:
- var
- hra: String;
- kola, kolo, x,y : Integer;
- procedure TForm1.Button1Click(Sender: TObject);
- var
- f: TextFile;
- max,maxid, i: Integer;
- hry: Array of String;
- begin
- Timer1.Enabled := True;
- AssignFile(f, 'hada.txt');
- Reset(f);
- i := 1;
- max := 0;
- // S hore, Z dole, V nalavo, J napravo
- while not eof(f) do
- begin
- ReadLn(f, hra);
- SetLength(hry, i);
- hry[i-1] := hra;
- if(Length(hra) > max) then
- begin
- max:= Length(hra);
- maxid := i-1;
- end;
- Inc(i);
- end;
- hra := hry[maxid];
- kola := Length(hra);
- kolo := 1;
- x:= Image1.Width div 2;
- y:= Image1.Height div 2;
- Image1.Canvas.MoveTo(x,y);
- Label3.Caption := 'Pocet hier: ' + IntToStr(Length(hry));
- end;
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- Image1.Canvas.FillRect(Image1.ClientRect);
- end;
- procedure TForm1.Timer1Timer(Sender: TObject);
- const
- TAH = 20;
- begin
- if(hra[kolo] = 'J') then y := y + TAH
- else if(hra[kolo] = 'S') then y := y - TAH
- else if(hra[kolo] = 'Z') then x := x - TAH
- else if(hra[kolo] = 'V') then x := x + TAH
- else
- begin
- Timer1.Enabled := False;
- ShowMessage('Neplatny tah');
- end;
- Image1.Canvas.LineTo(x,y);
- Image1.Canvas.MoveTo(x,y);
- Label1.Caption := 'TAH ' + IntToStr(kolo) + ' / ' + IntToStr(kola);
- Label2.Caption := '[' +IntToStr(x) + ' ; ' + IntToStr(y) + ']';
- Inc(kolo);
- if(kolo-1 = kola) then
- begin
- Timer1.Enabled := False;
- ShowMessage('Hra skoncila');
- end;
- end;
- --------------------------------------------------------------------------------------------------
- Úloha 5
- Komponenty: Button, Memo, Image
- Súbory: Textový súbor pomenovaný teploty.txt
- Kód:
- procedure TForm1.Button1Click(Sender: TObject);
- var
- f: TextFile;
- teploty: Array[1..20] of Real;
- odchylka,sucet, priemerna: Real;
- i,x : Integer;
- begin
- AssignFile(f, 'teploty.txt');
- Reset(f);
- i := 1;
- while not eof(f) do
- begin
- ReadLn(f, teploty[i]);
- Memo1.Lines.Add(IntToStr(i) + '.den ' + FloatToStr(teploty[i]) + ' °C = '
- + FloatToStr((teploty[i]*9/5) + 32) + ' F');
- Inc(i);
- end;
- sucet := 0;
- x:= 1;
- Image1.Canvas.MoveTo(x*10, Image1.Height - (round(teploty[1]) * 2));
- for i:= 1 to 20 do
- begin
- sucet := sucet + teploty[i];
- Image1.Canvas.LineTo(x*i*10, Image1.Height - (round(teploty[i]) * 2));
- Image1.Canvas.MoveTo(x*i*10, Image1.Height - (round(teploty[i]) * 2));
- end;
- priemerna := sucet / 20;
- Memo1.Lines.Add('Priemerna teplota: ' + FloatToStr(priemerna));
- odchylka := 0;
- for i:= 1 to 20 do
- begin
- if(odchylka < (abs(priemerna - teploty[i]))) then odchylka := abs(priemerna - teploty[i]);
- end;
- Memo1.Lines.Add('Odchylka teploty: ' + FloatToStr(odchylka));
- end;
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- Image1.Canvas.FillRect(Image1.ClientRect);
- end;
- --------------------------------------------------------------------------------------------------
- Úloha 7
- Komponenty: Button, Memo, Image
- Súbor: sviecky.txt
- Kód:
- procedure TForm1.Button1Click(Sender: TObject);
- var
- sviecky : Array[1..20, 1..3] of Real;
- f : TextFile;
- i, j, k, maxid, pocet,x,y : Integer;
- max : Real;
- begin
- for i:= 1 to 20 do
- begin
- for j:= 1 to 2 do
- begin
- sviecky[i][j] := -1;
- end;
- end;
- AssignFile(f, 'sviecky.txt');
- Reset(f);
- k:= 1;
- while not eof(f) do
- begin
- ReadLn(f, sviecky[k][1], sviecky[k][2]);
- Inc(k);
- end;
- max := sviecky[1][1] / sviecky[1][2];
- pocet:= 0;
- for i:= 1 to 20 do
- begin
- if(sviecky[i][1] <> -1) then
- begin
- Inc(pocet);
- sviecky[i][3] := sviecky[i][1] / sviecky[i][2];
- Memo1.Lines.Add(IntToStr(i) + '.sviecka ' + FloatToStr(sviecky[i][1]) + ' cm ' +
- FloatToStr(sviecky[i][2]) + ' cm/hod zhori za ' + FloatToStr(sviecky[i][3]) + ' hod');
- if(max > sviecky[i][3]) then
- begin
- max := sviecky[i][3];
- maxid := i;
- end;
- end;
- end;
- Memo1.Lines.Add('Pocet sviecok: ' + IntToStr(pocet));
- Memo1.Lines.Add('Najrychlejsie zhori sviecka ' + IntToStr(maxid) + ' za ' + FloatToStr(max));
- x:= 15; y:= Image1.Height;
- for i:= 1 to 20 do
- begin
- Image1.Canvas.Brush.Color := Random(255*255*255);
- Image1.Canvas.Rectangle(x,y,x+20,y-Round(sviecky[i][1]));
- x:= x+40;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement