Pascal-sets

提供:Dev Guides
移動先:案内検索

パスカル-セット

セットは、同じタイプの要素のコレクションです。 Pascalでは、設定されたデータ型を定義できます。 セット内の要素は、そのメンバーと呼ばれます。 数学では、集合は_braces \ {} _でメンバーを囲むことによって表されます。 ただし、Pascalでは、セット要素は角括弧[]で囲まれ、セットコンストラクターと呼ばれます。

セットのタイプと変数の定義

パスカルセットタイプは次のように定義されます

type
set-identifier = set of base type;

セット型の変数は次のように定義されます

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

or,

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

これにより2つのセットが結合され、両方のセットのメンバーを持つ新しいセットが作成されます。

2

Difference

2つのセットの差を取得し、どちらのセットにも共通しない要素を持つ新しいセットを提供します。

3

Intersection

2つのセットの共通部分を取得し、両方のセットに共通の要素を持つ新しいセットを提供します。

4

Inclusion

PのすべてのアイテムがQにも含まれているが、その逆ではない場合、セットPはセットQに含まれます。

5

Symmetric difference

2つのセットの対称差を取得し、要素セットを提供します。これらのセットは、いずれかのセットにあり、交差点にはありません。

6

In

メンバーシップをチェックします。

次の表は、Free Pascalでサポートされているすべての集合演算子を示しています。 S1S2 は2つの文字セットであり、

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]><[yellow, green, white, black];
   displayColors(c);
end.

上記のコードをコンパイルして実行すると、次の結果が生成されます-

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