------Delphi7

需要引用:TypInfo单元

Delphi遍历枚举_集合类型


--Unit开始--

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls, TypInfo;

type

TForm1 = class(TForm)

Memo1: TMemo;

Button1: TButton;

procedure Button1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

EMyEnum=(AA,BB,CC,DD,EE,FF);//声明枚举

var

Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

var

i:Integer;

vEnumStr:string;

venum:EMyEnum;

vP:PTypeInfo;

begin

Memo1.Clear;

//遍历枚举

vP:=TypeInfo(EMyEnum);

i:=0;

for venum:=Low(EMyEnum) to High(EMyEnum) do

begin

vEnumStr:=GetEnumName(vP,i);

Memo1.Lines.Add(vEnumStr);

inc(i);

end;

//测试别的

Memo1.Lines.Add('-----------------');

i:=GetEnumValue(vp,'CC');

Memo1.Lines.Add(IntToStr(i));

if EMyEnum(i)=CC then

Memo1.Lines.Add('CC');

end;

end.

--Unit结束

-----Form开始------

object Form1: TForm1

Left = 498

Top = 418

BorderStyle = bsDialog

Caption = 'Form1'

ClientHeight = 327

ClientWidth = 218

Color = clBtnFace

Font.Charset = DEFAULT_CHARSET

Font.Color = clWindowText

Font.Height = -11

Font.Name = 'MS Sans Serif'

Font.Style = []

OldCreateOrder = False

PixelsPerInch = 96

TextHeight = 13

object Memo1: TMemo

Left = 8

Top = 8

Width = 185

Height = 225

ImeName = '中文(简体) - 搜狗拼音输入法'

TabOrder = 0

end

object Button1: TButton

Left = 72

Top = 264

Width = 75

Height = 25

Caption = 'Button1'

TabOrder = 1

OnClick = Button1Click

end

end

-----Form结束-----

---------------------------------------------------其他资料--------------------------------------------- ​

枚举、子界与集合

枚举

type

<类型名称>=(<标识符1>,<标识符2>...<标识符n>);

1

2

1)类型名称:自定义枚举类型名称

2)标识符:常量元素,个数有限

3)一个枚举类型中,枚举常量不允许重复出现,也不允许同一枚举出现在不同类型中

例如,自动以7个常量的枚举类型

Type

Weekday=(Sun,Mon,Tue,Wed,Thu,Fri,Sat)

1

2

枚举规则:先定义类型在定义变量

var w1,w2,w3:weekday;

a,b,c:integer;

1

2

枚举运算:

1)用pred函数求枚举值的前导。pred(mon)的值为sun,sun没有前导

2)用succ函数求枚举值的后继。succ(sun)的值为mon,sat没有后继

3)用ord函数求枚举值的序号。ord(sun)为0

4)用low函数得第一个枚举值。low(weekday)的值为sun.low(w1)和low(w2)也为sun

5)用high函数求最后一个枚举值

6)使用关系运算符比较枚举值的大小。sun>mon的值为false;tue>=sun的值为true。

子界类型

type

<类型名称>=<常量1>...<常量2>;

1

2

1)<类型名称>是子界类型的名称。

2)<常量1>是子界的下界,<常量2>是子界类型的上界,之间为相同的有序类型(同为整型或同为字符型)

3)子界的上界大于或者等于下界

例,

type

month=1..12;

weekday=(sun,mon,tue,wed,thu,fri,sat);

workday=mon..fri;

1

2

3

4

子界类型变量的定义

规则:先定义类型,再定义变量

type

month=1..12;

weekday=(sun,mon,tue,wed,thu,fri,sat);

workday=mon..fri;

var

m1,m2:month;

w1,w2:workday;

1

2

3

4

5

6

7

不建议:m1,m2:1..12;

集合整型

1)集合中的元素是相异的。(不重复)

2)集合中的元素是没有顺序的

3)集合中的元素不能超过256个

集合的定义:

type

<类型名称>=set of<基类型>;

1

2

1)<类型名称>是用户所定义的集合类型的名称

2)<基类型>是集合中元素的类型,可以是字符、布尔、枚举、子界等类型,不能是整型、实型

例,

type

days=set of 28..31;

ch=set of 'A'..'Z';

weekday=(sun,tue,wed,thu,fri,sat);

workday=set of weekday;

1

2

3

4

5

集合类型变量的定义:

先定义集合类型

type

days=set of 28..31;

ch=set of 'A'..'Z';

weekday=(sun,...,sat);

workday=set of weekday;

1

2

3

4

5

再定义集合类型的变量

var

ch1,ch2:ch;

wo1,wo2:workday;

d1,d2:days;

1

2

3

4

集合类型的取值和运算

集合的取值称之为集合的值。

type

weekday=(sun,mon,tue,wed,thu,fir,sat);

workday=set of weekday;

var

wo1,wo2:workday;

wo1的值可以为[]、[sun]、[sun,mon]...

1

2

3

4

5

6

1)一个集合的基类型有n个值,那么集合变量的取值有2的n次方个。

2)[]是空集合

3)集合元素连续出现,可以写成子界形式。1..12等价于1到12

集合的运算

交、差、并运算和集合间的关系运算

前者得到的集合类型或者是boolean类型

1)集合的并。两个集合的所有元素组成(去掉重复的)

2)集合的交运算。两集合公有的

3)集合的差。一个集合去掉公有的

4)相等运算返回false or true

5)不相等运算(<>)返回false or true

6)包含运算。后者有的前者全有都有(>=)false or true

7)被包含运算。前者有的后者都有(<=)false or true

8)元素与集合的运算,判断元素是否在集合中。2 in[1,2,3]的值为true