Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Example: Text Analyzer

In the following program, the printable characters are divided into the following sets:

1.  Lowercase letters
2.  Uppercase letters
3.  Alphabetic characters (which is the union of 1 and 2)
4.  Digits
5.  Punctuation characters
6.  Other characters

The program reads a text file from the keyboard character by character and tests each character to see if it is a member in any one of these sets. The program is straightforward and contains four parts: declarations of sets, initialization of counters, testing memberships of characters, and displaying results.

{ -------------------------- figure 8-1 -------------------------- }
PROGRAM TextAnalyzer(INPUT,OUTPUT);
TYPE
 LowerCase = SET OF 'a'..'z';
 UpperCase = SET OF 'A'..'Z';
 Digits = SET OF '0'..'9';
 Characters = SET OF CHAR;
VAR
 Capital               :UpperCase;
 Small                :LowerCase;
 Numerals               :Digits;
 Alphabet, Punctuation, Others :Characters;
 A, C, S, N, P, O, Counter  :INTEGER;
 Ch                   :CHAR;
BEGIN
 Counter:= 0; { counter of all characters }
 A:= 0;    { counter of alphabetic characters }
 C:= 0;    { counter of capital letters }
 S:= 0;    { counter of small letters }
 N:= 0;    { counter of numeric characters }
 P:= 0;    { counter of punctuation characters }
 O:= 0;    { counter of other characters }
 Small:= ['a'..'z'];
 Capital:= ['A'..'Z'];
 Alphabet:= Small + Capital;
 Numerals:= ['0'..'9'];
 Punctuation:= [',',';','-','''','.','!','?',')','(','"',':','_'];
 WRITELN('Start typing your text file. To terminate press Ctrl+Z:');
 WHILE NOT EOF DO
  BEGIN
   WHILE NOT EOLN DO
    BEGIN
     READ(Ch);
     Counter:= Counter + 1;
     IF Ch IN Alphabet THEN
       BEGIN
        A := A + 1;
        IF Ch IN Small THEN
           S := S + 1
        ELSE IF Ch IN Capital THEN
           C := C + 1
       END
     ELSE IF Ch IN Numerals THEN
       N:= N + 1
     ELSE IF Ch IN Punctuation THEN
       P:= P + 1
     ELSE
       O:= O + 1
    END;
   READLN
  END;
 WRITELN('Total number of characters    = ', Counter);
 WRITELN('Number of alphabetic characters   = ', A);
 WRITELN('.Number of lowercase letters: ', S);
 WRITELN('.Number of uppercase letters: ', C);
 WRITELN('Number of numeric characters     = ', N);
 WRITELN('Number of punctuation characters  = ', P);
 WRITELN('Number of other characters    = ', O)
END.

Sample run:

    Start typing your text file. To terminate press Ctrl+Z:
    The standard set operators are:
         1. Union (+).
         2. Intersection (*).
         3. Difference (-).
    ^Z             ----> Press Ctrl+Z to end the text
    Total number of characters = 85
    Number of alphabetic characters = 53
    .Number of lowercase letters: 49
    .Number of uppercase letters: 4
    Number of numeric characters = 3
    Number of punctuation characters = 14
    Number of other characters = 15

Sets are useful for testing conditions. One common use of sets is to precede a CASE statement in order to filter out the unwanted data which do not belong to any case.

8-4 Records

A record, another structured type in Pascal, is a collection of related data items which may be of different types. Each item in the record is called a field. Take a look at this record, which is used to store information about each employee in a company:

Employee Record
Field # Information Possible Data Type
1. Name STRING
2. Address STRING
3. Phone number STRING/INTEGER
4. Hourly rate REAL
5. Marital status CHAR/Enumeration

Unlike arrays (which contain elements of the same type) records may contain fields of any data type, including the type RECORD itself.

Record Declaration

The declaration of a record takes the form:

type-identifier = RECORD
                 field-list
               END;

The field list contains the name and type of each field as in this declaration of the record “EmployeeRecord.”

    TYPE
      EmployeeRecord = RECORD
                     Name              :STRING[25];
                     Address           :STRING[40];
                     Phone             :STRING[12];
                     Rate              :REAL;
                     MaritalStatus     :CHAR;
                    END;


NOTE:  If your Pascal implementation does not support the STRING type, you may replace the STRING variables by INTEGER or CHAR variables, in which case you need to replace the variable “Name” with another variable like “ID,” etc.

A record declaration must be terminated by the keyword END.

In the VAR section, the record is then declared as a variable of the type “EmployeeRecord”:

    VAR
     EmployeeRec:EmployeeRecord;

As with other structured and user-defined data types, you can declare a record in the VAR section directly, but you now know the advantages of declaring data structures as types.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.