home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_7 / 7.ddi / TDEXMPL.ZIP / TPDEMO.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1991-02-13  |  5.2 KB  |  229 lines

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