home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / tpw / docdemos / tddemob.pas < prev    next >
Pascal/Delphi Source File  |  1991-05-20  |  5KB  |  209 lines

  1. (***********************************************************************
  2.  * File: TDDEMOB.PAS
  3.  *
  4.  *  Broken Turbo Pascal Demonstration program for use with Turbo Debugger
  5.  *  Copyright (c) 1988, 1991 - Borland International
  6.  *
  7.  *  Reads words from standard input, analyzes letter and word frequency.
  8.  *  Uses linked list to store command-line parameters on heap.
  9.  *
  10.  *  Uses the following data types:
  11.  *
  12.  *    Boolean,
  13.  *    Char, Byte,
  14.  *    Integer, Word,
  15.  *    LongInt,
  16.  *    Real (can't use 8087 type yet, change to extended)
  17.  *    String,
  18.  *    Array,
  19.  *    Record,
  20.  *    Set,
  21.  *    Pointer
  22.  *
  23.  * Comments that look like this:
  24.  *
  25.  *   { ** Bug: <description> }
  26.  *
  27.  * are inserted above lines that contain bugs.
  28.  ***********************************************************************)
  29.  
  30. program TDDemo;
  31.  
  32. uses
  33.   WinCrt;              { text I/O library for Windows }
  34. const
  35.   BufSize    = 128;    { length of line buffer }
  36.   MaxWordLen =  10;    { maximum word length allowed }
  37.  
  38. type
  39.   BufferStr = string[BufSize];
  40.  
  41.   LInfoRec = record
  42.     Count: Word;               { number of occurrences of this letter }
  43.     FirstLetter: Word;         { number of times as first letter of a }
  44.   end;
  45.  
  46. var
  47.   NumLines, NumWords: Word;                     { counters }
  48.   NumLetters: LongInt;
  49.   WordLenTable: array[1..MaxWordLen] of Word;   { info for each word }
  50.   LetterTable: array['A'..'Z'] of LInfoRec;     { info for each letter }
  51.   Buffer: BufferStr;
  52.  
  53. {***************************************************
  54.  * procedure ShowResults
  55.  ***************************************************}
  56. procedure ShowResults;
  57.  
  58. {+--------------------------------------------------
  59.  | procedure ShowLetterInfo
  60.  +--------------------------------------------------}
  61. procedure ShowLetterInfo(FromLet, ToLet: Char);
  62. var
  63.   ch: Char;
  64. begin
  65.   Writeln;
  66.   Write('Letter:     ');
  67.  
  68.   { ** Bug: Extra semicolon }
  69.  
  70.   for ch := FromLet to ToLet do;                { column titles }
  71.     Write(ch:5);
  72.   Writeln;
  73.  
  74.   Write('Frequency:  ');
  75.   for ch := FromLet to ToLet do                 { letter count }
  76.     Write(LetterTable[ch].Count:5);
  77.   Writeln;
  78.   Write('Word starts:');
  79.   for ch := FromLet to ToLet do                 { first letter count }
  80.     Write(LetterTable[ch].FirstLetter:5);
  81.   Writeln;
  82. end; { ShowLetterInfo }
  83.  
  84.  
  85. {*** ShowResults starts here ***}
  86.  
  87. var
  88.   i: Integer;
  89.   AvgWords: Real;
  90.  
  91. begin { ShowResults }
  92.  
  93.   { ** Bug: should test to avoid divide by zero; should be words per line }
  94.  
  95.   AvgWords := NumLines / NumWords;
  96.   Writeln;
  97.   Writeln(NumLetters, ' char(s) in ',
  98.     NumWords, ' word(s) in ',
  99.     NumLines, ' line(s)');
  100.   Writeln('Average of ', AvgWords:0:2, ' words per line');
  101.   Writeln;
  102.  
  103.   { Dump word count }
  104.   Write('Word length:');
  105.   for i := 1 to MaxWordLen do Write(i:4);
  106.   Writeln;
  107.  
  108.   Write('Frequency:  ');
  109.   for i := 1 to MaxWordLen do Write(WordLenTable[i]:4);
  110.   Writeln;
  111.  
  112.   { Dump letter counts }
  113.   ShowLetterInfo('A', 'M');
  114.   ShowLetterInfo('N', 'Z');
  115. end; { ShowResults }
  116.  
  117. {***************************************************
  118.  * procedure Init
  119.  ***************************************************}
  120. procedure Init;
  121. begin
  122.   NumLines := 0;
  123.   NumWords := 0;
  124.   NumLetters := 0;
  125.   FillChar(LetterTable, SizeOf(LetterTable), 0);
  126.   FillChar(WordLenTable, SizeOf(WordLenTable), 0);
  127.   Writeln('Enter a string to process, an empty string quits.');
  128. end; { Init }
  129.  
  130. {***************************************************
  131.  * procedure ProcessLine
  132.  ***************************************************}
  133. procedure ProcessLine(var S: BufferStr);
  134.  
  135. {+--------------------------------------------------
  136.  | function IsLetter
  137.  +--------------------------------------------------}
  138. function IsLetter(ch: Char): Boolean;
  139. begin
  140.  
  141.   { ** Bug: Should shift character to uppercase before testing }
  142.  
  143.   IsLetter := ch in ['A'..'Z'];
  144. end; { IsLetter }
  145.  
  146.  
  147. {*** Process Line starts here ***}
  148.  
  149. var
  150.   i: Integer;
  151.   WordLen: Word;
  152.  
  153. begin { ProcessLine }
  154.   Inc(NumLines);
  155.   i := 1;
  156.   while i <= Length(S) do
  157.   begin
  158.     { Skip non-letters }
  159.     while (i <= Length(S)) and not IsLetter(S[i]) do
  160.       Inc(i);
  161.  
  162.     { Find end of word, bump letter & word counters }
  163.     WordLen := 0;
  164.     while (i <= Length(S)) and IsLetter(S[i]) do
  165.     begin
  166.       Inc(NumLetters);
  167.       Inc(LetterTable[UpCase(S[i])].Count);
  168.       if WordLen = 0 then                    { bump counter }
  169.         Inc(LetterTable[UpCase(S[i])].FirstLetter);
  170.       Inc(i);
  171.       Inc(WordLen);
  172.     end;
  173.  
  174.     { Bump word count info }
  175.     if WordLen > 0 then
  176.     begin
  177.       Inc(NumWords);
  178.       if WordLen <= MaxWordLen then
  179.         Inc(WordLenTable[WordLen]);
  180.     end;
  181.   end; { while }
  182. end; { ProcessLine }
  183.  
  184. {***************************************************
  185.  * function GetLine
  186.  ***************************************************}
  187. function GetLine: BufferStr;
  188. var
  189.   S: BufferStr;
  190. begin
  191.   Write('String: ');
  192.   Readln(S);
  193.   GetLine := S;
  194. end;
  195.  
  196.  
  197. {*** Program starts here ***}
  198.  
  199. begin { program }
  200.   Init;
  201.   Buffer := GetLine;
  202.   while Buffer <> '' do
  203.   begin
  204.     ProcessLine(Buffer);
  205.     Buffer := GetLine;
  206.   end;
  207.   ShowResults;
  208. end.
  209.