|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
Example: Text AnalyzerIn the following program, the printable characters are divided into the following sets:
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 RecordsA 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:
Unlike arrays (which contain elements of the same type) records may contain fields of any data type, including the type RECORD itself. Record DeclarationThe 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;
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.
|
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. |