NBPrg40. HISTOGR.PAS. MiniOS. OpenSource.

NBPrg40. HISTOGR.PAS. MiniOS. OpenSource.
***************************************

{ Nikita Beloshenko (GNU) 2013 }

Unit Histogr;

(***********************************
 *                                 *
 *   HISTOGR.PAS                   *
 *                                 *
 *   Histogr.                      *
 *                                 *
 ***********************************)

Interface

Uses Graph;

Procedure Histogr_run;

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;

Function Func(x : Integer):Real;
Var y : Real;
Begin
  y := x*x*x;
  Func := y;
End;

Procedure Histogr_run;

Const MaxN = 15;

Var i : Integer;
    gd,gm : Integer;
    x1,y1,x2,y2 : Integer;
    MaxFunc,MinFunc : Real;

    r : Real;

    DATE : Array[1..MaxN] Of Real;
    N    : Byte;
    T    : Text;

    S : String;
Begin
  WriteLn(' Nikita Beloshenko (GNU) 2013 ');
  WriteLn(' ----- Histogram 2013 {(graph) (module)} ----- ');

  { Function / File ? }

  S:='';
  While ((S<>'1') And (S<>'2')) Do
  Begin
    WriteLn(' 1 or 2 ? . Function/File. ?');
    ReadLn(S);
  End;

  { Pause }

  WriteLn(' Press ENTER . ');
  ReadLn;

  { Init GRAPHICAL MODE. }

  gd := Detect;
  InitGraph(gd,gm,'');

  If S='1' Then
  Begin
    { 1 - Function }

    N := MaxN;

    { MaxFunc, MinFunc }
    MaxFunc := Func(1);    MinFunc := Func(1);
    For i := 1 To MaxN Do
    Begin
      If MaxFunc < Func(i) Then MaxFunc := Func(i);
      If MinFunc > Func(i) Then MinFunc := Func(i);
    End;
  End Else
  Begin
    { 2 - File }
    { 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);

    { MaxFunc, MinFunc }
    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;
  End;

  For i := 1 To N Do
  Begin
    x1 := 100 + i * 30;
    y1 := 200;
    x2 := x1 + 20;
    If S='1' Then
    Begin
      { 1 - Function }
      y2 := 200 - trunc(((Func(i) - MinFunc)/(MaxFunc - MinFunc)) * 100);
    End Else
    Begin
      { 2 - Edit }
      y2 := 200 - trunc(((DATE[i] - MinFunc)/(MaxFunc - MinFunc)) * 100);
    End;

    y1 := y1 + 50;
    y2 := y2 + 50;

    { Draw histogram. }
    { 1 - Function.
    { 2 - (DATE[1..N]). }
    DrawRectangle(x1,y1,x2,y2);
  End;

  { Frame }
  x1 := 100                   - 20;
  y1 := 100                   - 20;
  x2 := 100 + (MaxN + 1) * 30 + 20;
  y2 := 300                   + 20;
  DrawRectangle(x1,y1,x2,y2);

  { Pause. }
  (*
  WriteLn(' ');
  WriteLn(' Press ENTER. ');
  *)
  SetColor(White);
  SetTextStyle(5,HorizDir,5);
  OutTextXY(10,10,' Press ENTER. ');
  ReadLn;

  { Close GRAPHICAL MODE. }

  CloseGraph;

End;

End.