home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l044 / 4.ddi / DOCDEMOS.ZIP / LISTDEMO.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1990-10-23  |  5.1 KB  |  212 lines

  1.  
  2. { Turbo List }
  3. { Copyright (c) 1989,90 by Borland International, Inc. }
  4.  
  5. program ListDemo;
  6. { From Chapter 4 the Turbo Pascal 6.0 User's Guide.
  7.   Dynamic objects & destructors.
  8.  
  9.   If you are running this program in the IDE, be sure to enable
  10.   the full graphics save option when you load TURBO.EXE:
  11.  
  12.     turbo -g
  13.  
  14.   This ensures that the IDE fully swaps video RAM and keeps
  15.   "dustclouds" from appearing on the user screen when in
  16.   graphics mode. You can enable this option permanently
  17.   via the Options|Environment|Startup dialog.
  18.  
  19.   This program uses the Graph unit and its .BGI driver files to
  20.   display graphics on your system. The "PathToDrivers"
  21.   constant defined below is set to \TP\BGI, which is the default
  22.   location of the BGI files as installed by the INSTALL program.
  23.   If you have installed these files in a different location, make
  24.   sure the .BGI file for your system (EGAVGA.BGI, etc.) is in the
  25.   current directory or modify the "PathToDrivers" constant
  26.   accordingly.
  27. }
  28.  
  29. uses Graph, Figures;
  30.  
  31. const
  32.   PathToDrivers = '\TP\BGI';  { Default location of *.BGI files }
  33.  
  34. type
  35.   ArcPtr = ^Arc;
  36.   Arc = object(Circle)
  37.     StartAngle, EndAngle: Integer;
  38.     constructor Init(InitX, InitY: Integer; InitRadius: Integer;
  39.       InitStartAngle, InitEndAngle: Integer);
  40.     procedure Show; virtual;
  41.     procedure Hide; virtual;
  42.   end;
  43.  
  44.   NodePtr = ^Node;
  45.   Node = record
  46.     Item: PointPtr;
  47.     Next: NodePtr;
  48.   end;
  49.  
  50.   ListPtr = ^List;
  51.   List = object
  52.     Nodes: NodePtr;
  53.     constructor Init;
  54.     destructor Done; virtual;
  55.     procedure Add(Item: PointPtr);
  56.     procedure Report;
  57.   end;
  58.  
  59. var
  60.   GraphDriver: Integer;
  61.   GraphMode: Integer;
  62.   Temp: String;
  63.   AList: List;
  64.   PArc: ArcPtr;
  65.   PCircle: CirclePtr;
  66.   RootNode: NodePtr;
  67.  
  68.  
  69. {--------------------------------------------------------}
  70. { Procedures that are not methods:                       }
  71. {--------------------------------------------------------}
  72.  
  73. procedure OutTextLn(TheText: String);
  74. begin
  75.   OutText(TheText);
  76.   MoveTo(0, GetY+12);
  77. end;
  78.  
  79. procedure HeapStatus(StatusMessage: String);
  80. begin
  81.   Str(MemAvail: 6, Temp);
  82.   OutTextLn(StatusMessage+Temp);
  83. end;
  84.  
  85.  
  86. {--------------------------------------------------------}
  87. { Arc's method implementations:                          }
  88. {--------------------------------------------------------}
  89.  
  90. constructor Arc.Init(InitX, InitY: Integer; InitRadius: Integer;
  91.   InitStartAngle, InitEndAngle: Integer);
  92. begin
  93.   Circle.Init(InitX, InitY, InitRadius);
  94.   StartAngle := InitStartAngle;
  95.   EndAngle := InitEndAngle;
  96. end;
  97.  
  98. procedure Arc.Show;
  99. begin
  100.   Visible := True;
  101.   Graph.Arc(X, Y, StartAngle, EndAngle, Radius);
  102. end;
  103.  
  104. procedure Arc.Hide;
  105. var
  106.   TempColor: Word;
  107. begin
  108.   TempColor := Graph.GetColor;
  109.   Graph.SetColor(GetBkColor);
  110.   Visible := False;
  111.   Graph.Arc(X, Y, StartAngle, EndAngle, Radius);
  112.   SetColor(TempColor);
  113. end;
  114.  
  115.  
  116. {--------------------------------------------------------}
  117. { List's method implementations:                         }
  118. {--------------------------------------------------------}
  119.  
  120. constructor List.Init;
  121. begin
  122.   Nodes := nil;
  123. end;
  124.  
  125. destructor List.Done;
  126. var
  127.   N: NodePtr;
  128. begin
  129.   while Nodes <> nil do
  130.   begin
  131.     N := Nodes;
  132.     Nodes := N^.Next;
  133.     Dispose(N^.Item, Done);
  134.     Dispose(N);
  135.   end;
  136. end;
  137.  
  138. procedure List.Add(Item: PointPtr);
  139. var
  140.   N: NodePtr;
  141. begin
  142.   New(N);
  143.   N^.Item := Item;
  144.   N^.Next := Nodes;
  145.   Nodes := N;
  146. end;
  147.  
  148. procedure List.Report;
  149. var
  150.   Current: NodePtr;
  151. begin
  152.   Current := Nodes;
  153.   while Current <> nil do
  154.   begin
  155.     Str(Current^.Item^.GetX:3, Temp);
  156.     OutTextLn('X = ' + Temp);
  157.     Str(Current^.Item^.GetY:3, Temp);
  158.     OutTextLn('Y = ' + Temp);
  159.     Current := Current^.Next;
  160.   end;
  161. end;
  162.  
  163.  
  164. {--------------------------------------------------------}
  165. { Main program:                                          }
  166. {--------------------------------------------------------}
  167.  
  168. begin
  169.   { Let BGI determine which board you're using: }
  170.   DetectGraph(GraphDriver, GraphMode);
  171.   InitGraph(GraphDriver, GraphMode, PathToDrivers);
  172.   if GraphResult <> GrOK then
  173.   begin
  174.     Writeln(GraphErrorMsg(GraphDriver));
  175.     if GraphDriver = grFileNotFound then
  176.     begin
  177.       Writeln('in ', PathToDrivers,
  178.         '. Modify this program''s "PathToDrivers"');
  179.       Writeln('constant to specify the actual location of this file.');
  180.       Writeln;
  181.     end;
  182.     Writeln('Press Enter...');
  183.     Readln;
  184.     Halt(1);
  185.   end;
  186.  
  187.   HeapStatus('Heap space before list is allocated: ');
  188.  
  189.   { Create a list }
  190.   AList.Init;
  191.  
  192.   { Now create and add several figures to it in one operation }
  193.   AList.Add(New(ArcPtr, Init(151, 82, 25, 200, 330)));
  194.   AList.Add(New(CirclePtr, Init(400, 100, 40)));
  195.   AList.Add(New(CirclePtr, Init(305, 136, 5)));
  196.  
  197.   { Traverse the list and display X,Y of the list's figures }
  198.   AList.Report;
  199.  
  200.   HeapStatus('Heap space after list is allocated ');
  201.  
  202.   { Deallocate the whole list with one destructor call }
  203.   AList.Done;
  204.  
  205.   HeapStatus('Heap space after list is cleaned up: ');
  206.  
  207.   OutText('Press Enter to end program: ');
  208.   Readln;
  209.  
  210.   CloseGraph;
  211. end.
  212.