home *** CD-ROM | disk | FTP | other *** search
/ Jason Aller Floppy Collection / 180.img / TURBOD.ZIP / TPDEMO.PAS < prev    next >
Pascal/Delphi Source File  |  1989-05-02  |  5KB  |  228 lines

  1. {$N+,E+}
  2.  
  3. {  File: TPDEMO.PAS
  4.  
  5.    Turbo Pascal Demonstration program to show off Turbo Debugger
  6.  
  7.    Reads words from standard input, analyzes letter and word frequency.
  8.    Uses linked list to store commandline parameters on heap.
  9.  
  10.    Uses the following data types:
  11.  
  12.      Boolean,
  13.      Char, Byte,
  14.      Integer, Word,
  15.      LongInt,
  16.      Extended (8087 type, links in emulator)
  17.      String,
  18.      Array,
  19.      Record,
  20.      Set,
  21.      Pointer
  22. }
  23. program TPDemo;
  24.  
  25. const
  26.   BufSize    = 128;    { length of line buffer }
  27.   MaxWordLen =  10;    { maximum word length allowed }
  28.  
  29. type
  30.   BufferStr = String[BufSize];
  31.  
  32.   LInfoRec = record
  33.     Count : Word;               { number of occurrences of this letter }
  34.     FirstLetter : Word;         { number of times as first letter of a }
  35.   end;
  36.  
  37.  
  38. var
  39.   NumLines, NumWords : Word;                     { counters }
  40.   NumLetters : LongInt;
  41.   WordLenTable : array [1..MaxWordLen] of Word;  { info for each word }
  42.   LetterTable : array['A'..'Z'] of LInfoRec;     { info for each letter }
  43.   Buffer : BufferStr;
  44.  
  45. procedure ShowResults;
  46.  
  47. procedure ShowLetterInfo(FromLet, ToLet : Char);
  48. { Dump letter information }
  49. var
  50.   ch : Char;
  51. begin
  52.   Writeln;
  53.   Write('Letter:     ');
  54.   for ch := FromLet to ToLet do                   { column titles }
  55.     Write(ch:5);
  56.   Writeln;
  57.  
  58.   Write('Frequency:  ');
  59.   for ch := FromLet to ToLet do                 { letter count }
  60.     Write(LetterTable[ch].Count:5);
  61.   Writeln;
  62.   Write('Word starts:');
  63.   for ch := FromLet to ToLet do                 { first letter count }
  64.     Write(LetterTable[ch].FirstLetter:5);
  65.   Writeln;
  66. end; { ShowLetterInfo }
  67.  
  68. var
  69.   i : Integer;
  70.   AvgWords : Extended;
  71.  
  72. begin { ShowResults }
  73.   if NumLines <> 0 then
  74.     AvgWords := NumWords / NumLines
  75.   else
  76.     AvgWords := 0;
  77.   Writeln;
  78.   Writeln(NumLetters, ' char(s) in ',
  79.           NumWords, ' word(s) in ',
  80.           NumLines, ' line(s)');
  81.   Writeln('Average of ', AvgWords:0:2, ' words per line');
  82.   Writeln;
  83.  
  84.   { Dump word count }
  85.   Write('Word length:');
  86.   for i := 1 to MaxWordLen do
  87.     Write(i:4);
  88.   Writeln;
  89.  
  90.   Write('Frequency:  ');
  91.   for i := 1 to MaxWordLen do
  92.     Write(WordLenTable[i]:4);
  93.   Writeln;
  94.  
  95.   { Dump letter counts }
  96.   ShowLetterInfo('A', 'M');
  97.   ShowLetterInfo('N', 'Z');
  98. end; { ShowResults }
  99.  
  100. procedure Init;
  101. begin
  102.   NumLines := 0; NumWords := 0; NumLetters := 0;
  103.   FillChar(LetterTable, SizeOf(LetterTable), 0);
  104.   FillChar(WordLenTable, SizeOf(WordLenTable), 0);
  105.   Writeln('Enter a string to process, an empty string quits.');
  106. end; { Init }
  107.  
  108. procedure ProcessLine(var S : BufferStr);
  109.  
  110. function IsLetter(ch : Char) : Boolean;
  111. begin
  112.   IsLetter := UpCase(ch) in ['A'..'Z'];
  113. end; { IsLetter }
  114.  
  115. var
  116.   i : Integer;
  117.   WordLen : Word;
  118.  
  119. begin { ProcessLine }
  120.   Inc(NumLines);
  121.   i := 1;
  122.   while i <= Length(S) do
  123.   begin
  124.     { Skip non-letters }
  125.     while (i <= Length(S)) and not IsLetter(S[i]) do
  126.       Inc(i);
  127.  
  128.     { Find end of word, bump letter & word counters }
  129.     WordLen := 0;
  130.     while (i <= Length(S)) and IsLetter(S[i]) do
  131.     begin
  132.       Inc(NumLetters);
  133.       Inc(LetterTable[UpCase(S[i])].Count);
  134.       if WordLen = 0 then                    { bump counter }
  135.         Inc(LetterTable[UpCase(S[i])].FirstLetter);
  136.       Inc(i);
  137.       Inc(WordLen);
  138.     end;
  139.  
  140.     { Bump word count info }
  141.     if WordLen > 0 then
  142.     begin
  143.       Inc(NumWords);
  144.       if WordLen <= MaxWordLen then
  145.         Inc(WordLenTable[WordLen]);
  146.     end;
  147.   end; { while }
  148. end; { ProcessLine }
  149.  
  150. function GetLine : BufferStr;
  151. var
  152.   S : BufferStr;
  153. begin
  154.   Write('String: ');
  155.   Readln(S);
  156.   GetLine := S;
  157. end;
  158.  
  159. procedure ParmsOnHeap;
  160. { Builds a linked list of commandline parameters on the heap.
  161.   Note that the zero'th parameter, ParamStr(0), returns the
  162.   Exec name of the program on Dos 3.xx only.
  163. }
  164. type
  165.   ParmRecPtr = ^ParmRec;
  166.   ParmRec = record
  167.     Parm : ^String;
  168.     Next : ParmRecPtr;
  169.   end;
  170. var
  171.   Head, Tail, Temp : ParmRecPtr;
  172.   i : Integer;
  173.   s : String;
  174. begin
  175.   Head := nil;
  176.   for i := 0 to ParamCount do
  177.   begin
  178.     { Get next commandline parameter }
  179.     s := ParamStr(i);
  180.     if MaxAvail < SizeOf(ParmRec) + Length(s) + 1 then  { room on heap? }
  181.     begin
  182.       Writeln('Heap full, procedure aborting...');
  183.       Exit;
  184.     end;
  185.  
  186.     { Add to linked list }
  187.     New(Temp);                         { another Parm record }
  188.     with Temp^ do
  189.     begin
  190.       GetMem(Parm, Length(s) + 1);     { string + length byte }
  191.       Parm^ := s;
  192.       Next := nil;
  193.     end;
  194.     if Head = nil then                 { initialize list pointer }
  195.       Head := Temp
  196.     else
  197.       Tail^.Next := Temp;              { add to end }
  198.     Tail := Temp;                      { update tail pointer }
  199.   end; { for }
  200.  
  201.   { Dump list }
  202.   Writeln;
  203.   with Head^ do
  204.     if Parm^ <> '' then
  205.       Writeln('Program name: ', Parm^);
  206.   Write('Command line parameters: ');
  207.   Tail := Head^.Next;
  208.   while Tail <> nil do
  209.     with Tail^ do
  210.     begin
  211.       Write(Parm^, ' ');
  212.       Tail := Next;
  213.     end;
  214.   Writeln;
  215. end; { ParmsOnHeap }
  216.  
  217. begin { program }
  218.   Init;
  219.   Buffer := GetLine;
  220.   while Buffer <> '' do
  221.   begin
  222.     ProcessLine(Buffer);
  223.     Buffer := GetLine;
  224.   end;
  225.   ShowResults;
  226.   ParmsOnHeap;
  227. end.
  228.