一、集合
1.集合中的每个元素都必须为序数类型(介于0到255之间)
2. 一个集合最多可以包含256个元素。
3. 示例
program ProjectSetTypes;
Type
Junk = set of char;
Days = (Mon,Tue,Wed,Thu,Fri,Sat,Sun);
var
WorkDays : = set of days;
WorkDay := [Mon,Tue,Wed,Thu,Fri];
begin
end.
二、操作
并集,差集,交集,对称差集(交集取反),包含,排除。
可以使用Include或Exclude运算符从集合中添加或删除元素。
program projectSetOperate;
Type
Day = (mon,tue,wed,thu,fri,sat,sun);
Days = set of Day;
Procedure PrintDays(W: Days);
const
DayNames : Array [Day] of string[3]
=('mon','tue','wed','thu','fri','sat','sun');
var
D : Day;
S : string;
begin
s := '';
for D:=Mon to Sun do
if D in W then
begin
if ( S <> '' ) then S:= S + ',';
S := S + DayNames[D];
end ;
WriteLn('[',S,']');
end;
Var
W : Days;
begin
W := [mon,tue] + [wed,thu,fri]; //并集= [mon,tue,thu,fri]
PrintDays(W);
W := [mon,tue,wed] - [tue]; //差集 = [mon,wed]
PrintDays(w);
W := [mon,tue,wed] - [wed,thu];//差集 = [mon,tue]
PrintDays(W);
W := [mon,tue,wed] * [wed,thu,fri];//交集 = [wed]
PrintDays(W);
W := [mon,tue,wed] >< [wed,thu,fri];//对称差集 = [mon,tue,thu,fri];
PrintDays(W);
W := [mon];
include(W,wed); //= [mon,wed] 相当于并集
PrintDays(W);
Exclude(W,wed);//=[mon] 相当于差集
PrintDays(W);
if (mon in W) then
writeln('true') //此处不能有; 这个语法不好
else
writeln('false');
readln;
end.