unit Unit1; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Nakresli_graf: TButton;
    Image1: TImage;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Nakresli_grafClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  Form1: TForm1;
  subor: textfile;
  vistup: textfile;
  viska: integer;
  x1,x2,y1,y2: integer;
  priemer: integer;
  sucet: integer;
  pocet: integer;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Nakresli_grafClick(Sender: TObject);
begin
   Image1.Canvas.Brush.Color := clWhite;
   Image1.Canvas.FillRect(Image1.ClientRect);
   Image1.Canvas.Brush.Color := clBlack;
   x1:= 20;
   y1:= Image1.Height;
  assignfile(subor,'subor.txt'); // priradi subor
  reset(subor);                  // nastavi ho na citanie
  while not eof (subor) do       // skontroluje ci je nakonci
   begin
     x2:= x1+10;
     readln(subor,viska);        // precita premennu subor a zapise do var viska
     memo1.lines.append(inttostr(viska)); // vipise var viska
     y2:= Image1.Height - viska;
     Image1.Canvas.rectangle(x1,y1,x2,y2);
     x1:= x1+20;

   end;
   closefile(subor);   // ulozi a zavre var subor
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  assignfile(subor,'subor.txt'); // priradi subor
  reset(subor);                  // nastavi ho na citanie
    while not eof (subor) do       // skontroluje ci je nakonci
   begin
     readln(subor,viska);        // precita premennu subor a zapise do var viska
     sucet := sucet + viska;
     pocet := pocet + 1;
   end;
   closefile(subor);   // ulozi a zavre var subor

   priemer := sucet div pocet;

   assignfile(vistup,'vistup.txt'); // priradi subor
   rewrite(vistup);
   writeln(vistup,'priemer je: ', priemer); // napise priemer do var vistup
   writeln(vistup,'sucet je: ', sucet);   // napise sucet do var vistup
   closefile(vistup);   // ulozi a zavre var subor
end;

end.

