Complex data types are also called compound data types, which are relative to simple data types (or basic data types). I prefer to call it a composite data type, obviously it is actually a combination of one or more basic data types
Enumerated type
Pascal program is not only used for numerical processing, but also more widely used for processing non-numerical data. For example, gender, month, day of the week, color, unit name, educational background, occupation, etc.
definition
type enumeration type identifier = (identifier 1, identifier 2, ..., identifier n)
Features
Enumeration elements can only be identifiers ; all enumeration elements listed when defining an enumeration type constitute the range (value range) of this enumeration type. E.g
The following type definitions are legal:
type
days=(sun,mon,tue,wed,thu,fri,sat);
colors=(red,yellow,blue,white,black,green);
And the following type definition is wrong:
type
colortype=('red','yellow','blue','white');
numbers=(1,3,5,7,9);
At first, when I used enumeration for the first time, I directly applied the usage of enumeration in Java. It was obviously broken.
Enumeration types are sequential types: the sequence number of each enumeration element is determined according to the arrangement order of the enumeration elements when the type is defined, and the sequence number starts from 0. E.g
Define type days=(sun,mon,tue,wed,thu,fri,sat); then, ord(sun)=0,ord(mon)=1,..., and so on.
The same enumeration element cannot appear in two or more enumeration type definitions. The following definition is wrong:
type
color1=(red,yellow,white); // 因为red属于枚举类型color1和 color2
color2=(blue,red,black);
Enumeration type variables can only perform assignment operations and relational operations , but cannot perform arithmetic operations and logical operations. When comparing enumerated elements, it is actually a comparison of their serial numbers. E.g:
type
days=(sun,mon,tue,wed,thu,fri,sat);
colors=(red,yellow,blue,white,black,green);
var
color:colors;
weekday:days;
//则下面语句是合法的:
weekday:=mon;
if weekday=sun then
write('rest');
{而下面语句是不合法的:}
mon:=1; {错把枚举值当成变量名}
weekday:=blue; {枚举值blue不属于枚举变量weekday的值域}
read(color); {枚举类型变量 不能用读语句进行赋值}
write(weekday); {不能通过写语句输出枚举类型的变量值和枚举值。}
writeln(blue);
You can merge the variable description with the type definition . This kind of gameplay is very common in Delphi, which is equivalent to merging the original two steps into one. E.g:
var
holiday,workday:(sun,mon,tue,wed,thu,fri,sat);
color:(red,yellow,blue,white,black,green);
The input and output of enumerated data can be done indirectly. When inputting, generally you can input a code and convert it through the program. When outputting, it just prints out the character string corresponding to the enumerated element. This will be used in the following sample questions.
Get enumeration element
Single value acquisition, although this situation is relatively rare, it does not mean that it cannot be acquired. I myself did not know how to acquire the key and value in the enumeration a long time ago.
implementation
//该单元必须引入
uses System.TypInfo;
{$R *.dfm}
type
TColors = (Red, Green, Blue);
procedure TForm1.Button1Click(Sender: TObject);
var
Colors: TColor;
Info: PTypeInfo;
EnumName: string;
EnumValue: Integer;
begin
Info := TypeInfo(TColors);
//获取value对应的名称
EnumName := GetEnumName(Info, 1);
//获取名称对应的value
EnumValue := GetEnumValue(Info, EnumName);
ShowMessage(EnumName + ',' + EnumValue.ToString);
end;
Traverse the values in the enumeration: the enumeration type is an ordered type, so variables of the enumeration type can be used as loop variables.
uses System.TypInfo;
{$R *.dfm}
type
TColors = (Red, Green, Blue);
procedure TForm1.Button1Click(Sender: TObject);
var
Colors: TColors;
Info: PTypeInfo;
EnumPoint: PTypeData;
EnumName: string;
EnumValue: Integer;
begin
Info := TypeInfo(TColors);
//获取PTypeInfo的指针对象
EnumPoint := GetTypeData(Info);
//利用TypeData中的MinValue和MaxValue函数实现遍历
for var I := EnumPoint.MinValue to EnumPoint.MaxValue do begin
EnumName := GetEnumName(Info, I);
EnumValue := GetEnumValue(Info, EnumName);
ShowMessage(EnumName + ',' + EnumValue.ToString);
end;
end;
Applications
Example: Determine what the current color is.
implementation
uses System.TypInfo;
{$R *.dfm}
type
TColors = (Red, Green, Blue);
procedure TForm1.Button1Click(Sender: TObject);
var
Colors: TColors;
begin
case Colors of
Red: begin
ShowMessage('红色');
end;
Green: begin
ShowMessage('绿色');
end;
Blue: begin
ShowMessage('蓝色');
end;
end;
end;