পৃষ্ঠাসমূহ

.

Search Your Article

Total Pageviews

Monday, February 6, 2017

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.
Operations Descriptions
Union This joins two sets and gives a new set with members from both sets.
Difference Gets the difference of two sets and gives a new set with elements not common to either set.
Intersection Gets the intersection of two sets and gives a new set with elements common to both sets.
Inclusion A set P is included in set Q, if all items in P are also in Q but not vice versa.
Symmetric difference Gets the symmetric difference of two sets and gives a set of elements, which are in either of the sets and not in their intersection.
In It 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'];
Operator Description Example
+ Union of two sets S1 + S2 will give a set
['a', 'b', 'c', 'd', 'e']
- Difference of two sets S1 - S2 will give a set
['a', 'b']
* Intersection of two sets S1 * S2 will give a set
['c']
>< Symmetric difference of two sets S1 >< S2 will give a set ['a', 'b', 'd', 'e']
= Checks equality of two sets S1 = S2 will give the boolean value False
<> Checks non-equality of two sets S1 <> 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
Include Includes an element in the set; basically it is the Union of a set and an element of same base type Include (S1, ['d']) will give a set
['a', 'b', 'c', 'd']
Exclude Excludes an element from a set; basically it is the Difference of a set and an element of same base type Exclude (S2, ['d']) will give a set
['c', 'e']
In Checks 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]

No comments:

Post a Comment