📜  Pascal-套装

📅  最后修改于: 2020-11-03 16:18:59             🧑  作者: Mango


集合是相同类型的元素的集合。 Pascal允许定义设置的数据类型。集合中的元素称为其成员。在数学中,集合通过将成员在花括号{}中来表示。但是,在Pascal中,集合元素包含在方括号[]中,这被称为集合构造函数。

定义集合类型和变量

Pascal集类型定义为

type
set-identifier = set of base type;

集类型的变量定义为

var
s1, s2, ...: set-identifier;

要么,

s1, s2...: set of base type;

一些有效的集合类型声明的示例是-

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;

集合运算符

您可以在Pascal集上执行以下集操作。

Sr.No Operations & Descriptions
1

Union

This joins two sets and gives a new set with members from both sets.

2

Difference

Gets the difference of two sets and gives a new set with elements not common to either set.

3

Intersection

Gets the intersection of two sets and gives a new set with elements common to both sets.

4

Inclusion

A set P is included in set Q, if all items in P are also in Q but not vice versa.

5

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.

6

In

It checks membership.

下表显示了Free Pascal支持的所有集合运算符。假设S1S2是两个字符集,例如-

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

以下示例说明了其中一些运算符的用法-

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]>

编译并执行上述代码后,将产生以下结果-

[ red , blue , yellow , green , white , black , orange]
[ red , blue , yellow , green]
[ red , blue , yellow , black , orange]
[ green , white]
[ red , blue , white , black]