Advertisement
Guest User

DOMIN D.U. INF

a guest
Mar 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 7.54 KB | None | 0 0
  1. TEXTOVÉ SÚBORY SADA ÚLOH
  2.  
  3. Úloha 1
  4. Komponenty: Memo, Button
  5. Súbory: Textový súbor pomenovaný vstup.txt
  6. Kód:
  7. procedure TForm1.Button1Click(Sender: TObject);
  8. var
  9.   f: TextFile;
  10.   prazdneRiadky: Array of Integer;
  11.   i, pocetriadkov, pocetznakov, max: Integer;
  12.   riadok: String;
  13.  
  14. begin
  15.   // Open file for reading
  16.   AssignFile(f, 'vstup.txt');
  17.   Reset(f);
  18.   // Default values
  19.   i:=1;
  20.   pocetriadkov := 0;
  21.   max := 0;
  22.   pocetznakov := 0;
  23.  
  24.  
  25.   while not eof(f) do
  26.   begin
  27.     ReadLn(f, riadok);
  28.     pocetriadkov := pocetriadkov + 1;
  29.     pocetznakov := pocetznakov + Length(riadok);
  30.  
  31.     // Get the Length of longest array
  32.     if(Length(riadok) > max) then max:= Length(riadok);
  33.     // Empty line
  34.     if(Length(riadok) = 0) then
  35.     begin
  36.       // Iterate the length of array by 1
  37.       SetLength(prazdneRiadky, i);
  38.       // Save number of empty line into array
  39.       prazdneRiadky[i-1] := pocetriadkov;
  40.       Inc(i);
  41.  
  42.     end;
  43.   end;
  44.  
  45.   // Output
  46.   Memo1.Lines.Add('Najdlhsi: ' + IntToStr(max));
  47.   Memo1.Lines.Add('Pocet Riadkov: ' + IntToStr(pocetriadkov));
  48.   Memo1.Lines.Add('Pocet Znakov: ' + IntToStr(pocetznakov));
  49.   for i:= 0 to Length(prazdneRiadky) - 1 do
  50.   begin
  51.     Memo1.Lines.Add('Prazdny je ' + IntToStr(prazdneRiadky[i]));
  52.   end;
  53.   Memo1.Lines.Add('Prazdnych je dokopy ' + IntToStr(Length(prazdneRiadky)));
  54.  
  55.   CloseFile(f);
  56.  
  57. end;                  
  58. --------------------------------------------------------------------------------------------------
  59.  
  60.  
  61. Úloha 2
  62. Komponenty: Button, Edit
  63. Súbory: Textový súbor pomenovaný vstup.txt
  64. Kód:
  65. procedure TForm1.Button1Click(Sender: TObject);
  66. var
  67.   f, new: TextFile;
  68.   vetaa, veta, slogan: String;
  69.   vstup : Array of String;
  70.   i : Integer;
  71. begin
  72.   // Open file for reading
  73.   AssignFile(f, 'vstup.txt');
  74.   Reset(f);
  75.   AssignFile(new, 'vystup.txt');
  76.   ReWrite(new);
  77.  
  78.   slogan:= Edit1.Text;
  79.   i:= 1;
  80.   WriteLn(new, 'SEKCIA A');
  81.   // a
  82.   while not eof(f) do
  83.   begin
  84.   ReadLn(f, veta);
  85.   WriteLn(new, veta);
  86.   SetLength(vstup, i);
  87.   vstup[i-1] := veta;
  88.   Inc(i);
  89.   end;
  90.   CloseFile(f);
  91.   WriteLn(new, 'SEKCIA B');
  92.   // b
  93.   for i:= 0 to Length(vstup) - 1 do
  94.   begin
  95.   if(Length(vstup[i]) = 0) then WriteLn(new, slogan)
  96.   else WriteLn(new, vstup[i]);
  97.   end;
  98.  
  99.   WriteLn(new, 'SEKCIA C');
  100.   // c
  101.   for i:= 0 to Length(vstup) - 1 do
  102.   begin
  103.   if(Length(vstup[i]) <> 0) then WriteLn(new, vstup[i]);
  104.   end;
  105.  
  106.   WriteLn(new, 'SEKCIA D');
  107.   // d
  108.   for i:= 0 to Length(vstup) - 1 do
  109.   begin
  110.   vetaa := vstup[i];
  111.   if(Length(vetaa) > 0) then
  112.   begin
  113.   if(vetaa[1] = ' ') then Delete(vetaa, 1, 1);
  114.   if(vetaa[Length(vetaa)] = ' ') then Delete(vetaa,Length(vetaa),1);
  115.   WriteLn(new, vetaa);
  116.   end;
  117.   end;
  118.  
  119.   WriteLn(new, 'SEKCIA E');
  120.   // e
  121.   for i:= Length(vstup) - 1 downto 0 do
  122.   begin
  123.   WriteLn(new, vstup[i]);
  124.   end;
  125.  
  126.   CloseFile(new);
  127. end;  
  128. --------------------------------------------------------------------------------------------------
  129.  
  130. Úloha 4
  131. Komponenty: Button, Timer, 3x Label, Image
  132. Súbory: Textový súbor pomenovaný hada.txt
  133. Kód:
  134. var
  135.   hra: String;
  136.   kola, kolo,  x,y : Integer;
  137.  
  138. procedure TForm1.Button1Click(Sender: TObject);
  139. var
  140.   f: TextFile;
  141.   max,maxid, i: Integer;
  142.   hry: Array of String;
  143. begin
  144.   Timer1.Enabled := True;
  145.  
  146.   AssignFile(f, 'hada.txt');
  147.   Reset(f);
  148.  
  149.   i := 1;
  150.   max := 0;
  151.   // S hore, Z dole, V nalavo, J napravo
  152.   while not eof(f) do
  153.   begin
  154.     ReadLn(f, hra);
  155.     SetLength(hry, i);
  156.     hry[i-1] := hra;
  157.     if(Length(hra) > max) then
  158.     begin
  159.       max:= Length(hra);
  160.       maxid := i-1;
  161.     end;
  162.     Inc(i);
  163.   end;
  164.  
  165.   hra := hry[maxid];
  166.   kola := Length(hra);
  167.   kolo := 1;
  168.   x:=  Image1.Width div 2;
  169.   y:=  Image1.Height div 2;
  170.   Image1.Canvas.MoveTo(x,y);
  171.   Label3.Caption := 'Pocet hier: ' + IntToStr(Length(hry));
  172.  
  173. end;
  174.  
  175. procedure TForm1.FormCreate(Sender: TObject);
  176. begin
  177.   Image1.Canvas.FillRect(Image1.ClientRect);
  178. end;
  179.  
  180. procedure TForm1.Timer1Timer(Sender: TObject);
  181. const
  182.   TAH = 20;
  183.  
  184. begin
  185.     if(hra[kolo] = 'J') then y := y + TAH
  186.     else if(hra[kolo] = 'S') then y := y - TAH
  187.     else if(hra[kolo] = 'Z') then x := x - TAH
  188.     else if(hra[kolo] = 'V') then x := x + TAH
  189.     else
  190.     begin
  191.       Timer1.Enabled := False;
  192.       ShowMessage('Neplatny tah');
  193.     end;
  194.     Image1.Canvas.LineTo(x,y);
  195.     Image1.Canvas.MoveTo(x,y);
  196.  
  197.     Label1.Caption := 'TAH ' + IntToStr(kolo) + ' / ' + IntToStr(kola);
  198.     Label2.Caption := '[' +IntToStr(x) + ' ; ' + IntToStr(y) + ']';
  199.     Inc(kolo);
  200.     if(kolo-1 = kola) then
  201.     begin
  202.       Timer1.Enabled := False;
  203.       ShowMessage('Hra skoncila');
  204.     end;
  205. end;
  206. --------------------------------------------------------------------------------------------------
  207. Úloha 5
  208. Komponenty: Button, Memo, Image
  209. Súbory: Textový súbor pomenovaný teploty.txt
  210. Kód:
  211. procedure TForm1.Button1Click(Sender: TObject);
  212. var
  213.   f: TextFile;
  214.   teploty: Array[1..20] of Real;
  215.   odchylka,sucet, priemerna: Real;
  216.   i,x : Integer;
  217. begin
  218.   AssignFile(f, 'teploty.txt');
  219.   Reset(f);
  220.  
  221.   i := 1;
  222.   while not eof(f) do
  223.   begin
  224.     ReadLn(f, teploty[i]);
  225.     Memo1.Lines.Add(IntToStr(i) + '.den                   ' + FloatToStr(teploty[i]) + ' °C = '
  226.                     + FloatToStr((teploty[i]*9/5) + 32) + ' F');
  227.     Inc(i);
  228.   end;
  229.  
  230.   sucet := 0;
  231.   x:= 1;
  232.   Image1.Canvas.MoveTo(x*10, Image1.Height - (round(teploty[1]) * 2));
  233.   for i:= 1 to 20 do
  234.   begin
  235.     sucet := sucet + teploty[i];
  236.     Image1.Canvas.LineTo(x*i*10, Image1.Height - (round(teploty[i]) * 2));
  237.     Image1.Canvas.MoveTo(x*i*10, Image1.Height - (round(teploty[i]) * 2));
  238.   end;
  239.   priemerna := sucet / 20;
  240.   Memo1.Lines.Add('Priemerna teplota: ' + FloatToStr(priemerna));
  241.  
  242.   odchylka := 0;
  243.   for i:= 1 to 20 do
  244.   begin
  245.     if(odchylka < (abs(priemerna - teploty[i]))) then odchylka := abs(priemerna - teploty[i]);
  246.   end;
  247.   Memo1.Lines.Add('Odchylka teploty: ' + FloatToStr(odchylka));
  248. end;
  249.  
  250. procedure TForm1.FormCreate(Sender: TObject);
  251. begin
  252.   Image1.Canvas.FillRect(Image1.ClientRect);
  253. end;                
  254. --------------------------------------------------------------------------------------------------
  255. Úloha 7
  256. Komponenty: Button, Memo, Image
  257. Súbor: sviecky.txt
  258. Kód:
  259. procedure TForm1.Button1Click(Sender: TObject);
  260. var
  261.   sviecky : Array[1..20, 1..3] of Real;
  262.   f : TextFile;
  263.   i, j, k, maxid, pocet,x,y : Integer;
  264.   max : Real;
  265. begin
  266.  
  267.   for i:= 1 to 20 do
  268.     begin
  269.     for j:= 1 to 2 do
  270.       begin
  271.         sviecky[i][j] := -1;
  272.       end;
  273.     end;
  274.   AssignFile(f, 'sviecky.txt');
  275.   Reset(f);
  276.  
  277.   k:= 1;
  278.   while not eof(f) do
  279.   begin
  280.     ReadLn(f, sviecky[k][1], sviecky[k][2]);
  281.     Inc(k);
  282.   end;
  283.  
  284.   max := sviecky[1][1] / sviecky[1][2];
  285.   pocet:= 0;
  286.   for i:= 1 to 20 do
  287.     begin
  288.       if(sviecky[i][1] <> -1) then
  289.         begin
  290.           Inc(pocet);
  291.           sviecky[i][3] :=  sviecky[i][1] / sviecky[i][2];
  292.           Memo1.Lines.Add(IntToStr(i) + '.sviecka    ' + FloatToStr(sviecky[i][1]) + ' cm    ' +
  293.           FloatToStr(sviecky[i][2]) + ' cm/hod    zhori za ' + FloatToStr(sviecky[i][3]) + ' hod');
  294.           if(max > sviecky[i][3]) then
  295.             begin
  296.               max := sviecky[i][3];
  297.               maxid := i;
  298.             end;
  299.         end;
  300.     end;
  301.   Memo1.Lines.Add('Pocet sviecok: ' + IntToStr(pocet));
  302.   Memo1.Lines.Add('Najrychlejsie zhori sviecka ' + IntToStr(maxid) + ' za ' + FloatToStr(max));
  303.  
  304.   x:= 15; y:= Image1.Height;
  305.   for i:= 1 to 20 do
  306.     begin
  307.     Image1.Canvas.Brush.Color := Random(255*255*255);
  308.     Image1.Canvas.Rectangle(x,y,x+20,y-Round(sviecky[i][1]));
  309.     x:= x+40;
  310.     end;
  311. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement