Wednesday, 28 May 2014

Pascal - Sets

Filled under:

Post By: Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------

Pascal - Sets

A set is a collection of elements of same type. Pascal allows defining the set data type. The elements in a set are called its members. In mathematics, sets are represented by enclosing the members within braces{}. However, in Pascal, set elements are enclosed within square brackets [], which are referred as set constructor.

Defining Set Types and Variables

Pascal Set types are defined as
type
set-identifier = set of base type;
Variables of set type are defined as
var
s1, s2, ...: set-identifier;
or,
s1, s2...: set of base type;
Examples of some valid set type declaration are:
type
Days = (mon, tue, wed, thu, fri, sat, sun);
Letters = set of char;
DaySet = set of days;
Alphabets = set of 'A' .. 'Z';
studentAge = set of 13..20;

Set Operators

You can perform the following set operations on Pascal sets.
OperationsDescriptions
UnionThis joins two sets and gives a new set with members from both sets.
DifferenceGets the difference of two sets and gives a new set with elements not common to either set.
IntersectionGets the intersection of two sets and gives a new set with elements common to both sets.
InclusionA set P is included in set Q, if all items in P are also in Q but not vice versa.
Symmetric differenceGets the symmetric difference of two sets and gives a set of elements, which are in either of the sets and not in their intersection.
InIt checks membership.
Following table shows all the set operators supported by Free Pascal. Assume that S1 and S2 are two character sets, such that:
S1 := ['a', 'b', 'c'];
S2 := ['c', 'd', 'e'];
OperatorDescriptionExample
+Union of two setsS1 + S2 will give a set 
['a', 'b', 'c', 'd', 'e']
-Difference of two setsS1 - S2 will give a set 
['a', 'b']
*Intersection of two setsS1 * S2 will give a set 
['c']
><Symmetric difference of two setsS1 >< S2 will give a set ['a', 'b', 'd', 'e']
=Checks equality of two setsS1 = S2 will give the boolean value False
<>Checks non-equality of two setsS1 <> S2 will give the boolean value True
<=Contains (Checks if one set is a subset of the other)S1 <= S2 will give the boolean value False
IncludeIncludes an element in the set; basically it is the Union of a set and an element of same base typeInclude (S1, ['d']) will give a set 
['a', 'b', 'c', 'd']
ExcludeExcludes an element from a set; basically it is the Difference of a set and an element of same base typeExclude (S2, ['d']) will give a set 
['c', 'e']
InChecks set membership of an element in a set['e'] in S2 gives the boolean value True

Example:

The following example illustrates the use of some of these operators:
program setColors;
type  
color = (red, blue, yellow, green, white, black, orange);  
colors = set of color;  
 
procedure displayColors(c : colors);  
const  
names : array [color] of String[7]  
  = ('red', 'blue', 'yellow', 'green', 'white', 'black', 'orange');  
var  
   cl : color;  
   s : String;  
begin  
   s:= ' ';  
   for cl:=red to orange do  
      if cl in c then  
      begin  
         if (s<>' ') then s :=s +' , ';  
         s:=s+names[cl];  
      end;  
   writeln('[',s,']');  
end;  
 
var  
   c : colors;  
 
begin  
   c:= [red, blue, yellow, green, white, black, orange];
   displayColors(c);

   c:=[red, blue]+[yellow, green]; 
   displayColors(c);  

   c:=[red, blue, yellow, green, white, black, orange] - [green, white];     
   displayColors(c);    

   c:= [red, blue, yellow, green, white, black, orange]*[green, white];     
   displayColors(c);  

   c:= [red, blue, yellow, green]><[yellow, green, white, black]; 
   displayColors(c);  
end.
When the above code is compiled and executed, it produces the following result:
[ red , blue , yellow , green , white , black , orange]
[ red , blue , yellow , green]
[ red , blue , yellow , black , orange]
[ green , white]
[ red , blue , white , black]

0 comments: