NBPrg40. N40T2.PAS. MiniOS. OpenSource.

NBPrg40. N40T2.PAS. MiniOS. OpenSource.
***************************************

{ Nikita Beloshenko (GNU) 2013  }

{ NBPRG23 -> ... -> NBPRG40 }

{ Nikita Beloshenko (GNU) 2012 }

Unit N40t2;

(*****************************************
 *                                       *
 *   N40T2.PAS                           *
 *                                       *
 *   N40t2.                              *
 *                                       *
 *****************************************)

Interface

Uses Graph, Crt;

Procedure DoTask2;

Implementation


Procedure DrawRectangle(Var x1,y1,x2,y2:Integer);
Begin
  Line(x1,y1,x2,y1);
  Line(x2,y1,x2,y2);
  Line(x2,y2,x1,y2);
  Line(x1,y2,x1,y1);
End;

Procedure DoTask2;
Const
  MaxN = 15;
Var
  x1,y1,x2,y2 : Integer;
  ch1 : Char;

  { 'HISTOGR.DAT' }
  DATE: Array[1..MaxN] Of Real;
  N,i : Integer;
  T   : Text;
  r   : Real;

  { Histogram }
  x1_,y1_,x2_,y2_ : Integer;
  MinFunc,MaxFunc : Real;
Begin
  x1 := 200; x2 := 500;
  y1 := 200; y2 := 300;

  SetFillStyle(0,SolidFill);
  Bar(x1,y1,x2,y2);

  ch1:=' ';

  { Read DATE[] from 'HISTOGR.DAT' }

    Assign(T,'Histogr.Dat');
    Reset(T);

    ReadLn(T,N);

    If N>MaxN Then
    Begin
      N := MaxN;
    End;

    i:=0;

    While Not eof(T) Do
    Begin
      Inc(i);
      If ((i < N)Or(i=N)) Then
      Begin
        ReadLn(T,DATE[i]);
      End Else
      Begin
        ReadLn(T,r);
      End;
    End;

    Close(T);

  { (END) Read DATE[] from 'HISTOGR.DAT' }

  { Draw histogram (DATE[1..N]). }

    MaxFunc := DATE[1];
    MinFunc := DATE[1];
    For i := 1 To N Do
    Begin
      If MaxFunc < DATE[i] Then MaxFunc := DATE[i];
      If MinFunc > DATE[i] Then MinFunc := DATE[i];
    End;

    If (x2-x1>20) And (y2-y1>20) Then
    Begin
      For i := 1 To N Do
      Begin
        x1_ := x1+5 + (i-1)*(((x2-5) - (x1+5)) Div N);
        x2_ := x1_ + (((x2-5) - (x1+5)) Div N) Div 2;

        y1_ := (y2-5) - trunc(((DATE[i]-MinFunc)/(MaxFunc-MinFunc))*((y2-5)-(y1+5)));
        y2_ := (y2-5);

        SetColor(2);
        DrawRectangle(x1_,y1_,x2_,y2_);
      End;
    End;

  { (END) Draw histogram (DATE[1..N]). }

  While Ord(ch1)<>27 Do
  Begin
    SetColor(2);
    DrawRectangle(x1,y1,x2,y2);
    SetTextStyle(2, HorizDir, 5);
    OutTextXY(x1 + (x2-x1) Div 4,
              y1 + (y2-y1) Div 4,'task2(histogr)');

    ch1 := ReadKey;
  End;
  SetFillStyle(0,SolidFill);
  Bar(x1,y1,x2,y2);
End;

End.