Delphi basic (constant, set, array [dynamic array, multidimensional array])

Posted by jarvishr on Mon, 09 Dec 2019 04:23:25 +0100

I. constant

1. Constant definition: the value defined at the beginning is not allowed to be changed during the operation of the program

1 const
 2 pi: Double = 3.141592; / / defined as constant
 3 {constant can be defined without type declaration. Compiler will determine the type of constant} according to specific value
4   Pi2 = 3.1415;

 

2. Constant use

Enumerations: constant sets
  type
Enumeration name = (identifier 1, identifier 2, identifier 3,...)

 1 type
 2   {Normally, the index of enumeration type starts from 0}
 3   EColors = (RED,GREEN,BLUE);
 4   EWeek = (SUN=7,MON=1,TUE=2,WED=3,THU=4,FRI=5,SAT=6);
 5 var
 6   Color : EColors;
 7   Week : EWeek;
 8 
 9 begin
10   Color := EColors.RED;
11   Week := EWeek.SUN;
12   Writeln(Ord(Color),',',Ord(Week));
13   REDln;
14 end.

 

Two, sub boundary

Use: in order to prevent cross-border
Precautions:
1. Subbound requires that the upper and lower bounds must be ordered data type integers, characters, enumerations
2. The upper bound of the subbound is greater than or equal to the lower bound

Format: type subbound

 1 {Definition format}
 2 type
 3   {enumeration}
 4   EColors = (RED,GREEN,BLUE);
 5   {Sub boundary}
 6   TSubBoundNumber = 1..10;
 7   TSubBoundChar = 'a'..'z';
 8 
 9 
10 begin
11   Writeln(Low(TSubBoundNumber)); //Lower bound
12   Writeln(High(TSubBoundChar));  //upper bound
13   REDln;
14 end.

 

Print out leap year or normal year, number of days per month and season

 1 {Print out leap year or normal year, number of days per month and season}
 2 type
 3   {Sub boundary}
 4   TSubBoundMonths = 1..12;
 5   {enumeration}
 6   ESeason = (Spring,Summer,Autumn,Winter);
 7 
 8 var
 9   Season : ESeason;
10   Month : TSubBoundMonths;
11   User_Year:Integer;
12   Is_Leap_Year:Boolean;
13   User_Month : Integer;
14 
15 procedure SubBound();
16 begin
17   Writeln('Please enter a year:');
18   REDln(User_Year);
19   {Default to leap year}
20   Is_Leap_Year := True;
21   while Is_Leap_Year do
22   begin
23     if (User_Year mod 4) = 0 then begin
24     if not((User_Year mod 100) = 0) then begin
25       Writeln('Leap year',User_Year);
26       Is_Leap_Year := False;
27     end;
28   end;
29   if (User_Year mod 400) = 0 then begin
30     Writeln('Leap year',User_Year);
31     Is_Leap_Year := False;
32   end
33   else begin
34     Is_Leap_Year := False;
35     Writeln('Ordinary year',User_Year)
36 
37   end;
38   end;
39 
40 end;
41 procedure Months();
42 begin
43   {Season}
44 Writeln('Please enter a month:');
45 REDln(User_Month);
46 begin
47   if (User_Month >= Low(TSubBoundMonths)) and (User_Month <= High(TSubBoundMonths)) then
48     if (User_Month >= 1) and (User_Month <= 3) then begin
49       Writeln('The current season is spring');
50       case User_Month of
51         1 : begin
52               Writeln('31 day');
53             end;
54         2 : begin
55               if Is_Leap_Year then  begin
56               Writeln('29 day');
57               end else begin
58                 Writeln('28 day');
59               end;
60             end;
61         3 : begin
62               Writeln('31 day');
63             end;
64       end;
65     end;
66 
67 end;
68 end;
69 
70 begin
71 //  Writeln(Low(TSubBoundNumber)); //Lower bound
72 //  Writeln(High(TSubBoundChar));  //upper bound
73 
74   SubBound();
75   Months();
76   REDln;
77 end.

 

Three, set

A set is a whole structure of elements with some common characteristics. In pascal, a collection is composed of a set of data elements of an ordered type, in which an ordered type becomes the basic type of the collection.

 

Set definition:

1 type
 2 < type name > = set of < base type >;

 

Declare the variables of the collection (you can use the name of the collection type to declare, or directly use the collection type itself to declare)

1 Var
2 Vset1:set1;
3 Vset2:set of byte;

 

Be careful:
1. All members of a collection type must be of the same ordered type with a maximum of 256 members.
2. The set type is unique, that is, the same member is not allowed in the same set type.
3. The members of set type are unordered, and the members have no ordinal number. Collection members cannot be identified with ordinals like enumerations.
4. The value field of the set type is determined by the value field of BaseType.

 

Set definition method

1 {definition method}
2. The main function of type {type is to allow users to customize type}
3   TMySet = set of Char;
4   TMySet1 = set of 'a'..'z';
5 var
6   TMySet2 : set of  'a'..'z';

 

Collection example

 1 uses
 2   System.SysUtils,System.TypInfo;
 3 
 4 type
 5   {Define an enumeration}
 6   TColors = (RED,GREEN,BLUE);
 7 type
 8   TSetColors = set of TColors;
 9 var
10   sc : TSetColors;
11   indexs : Integer;
12 procedure set_test1;
13 begin
14   for indexs := Ord(Low(TColors)) to Ord(High(TColors)) do begin
15 
16     Writeln(GetEnumName(TypeInfo(TColors),indexs)) //Display information within enumeration
17   end;
18 
19 end;
20 begin
21   {Traversing set elements: unordered}
22   set_test1();
23   Readln;
24 end.

 

Four, array

Array: a container with the same type of data stored in a certain number

Definition
array[indexType1, ..., indexTypen] of baseType;

Matters needing attention
1. Array must declare length (subscript, total number of elements) before use

 1 type   {Defining variables}
 2   MyArray = array[0..4] of Char;
 3 var   {Variable declaration}
 4   NameArray : MyArray;
 5   Indexs : Integer;
 6 
 7 {Static array element traversal}
 8 procedure MyProc;
 9 begin
10   NameArray[0] := 'a';
11   NameArray[1] := 'b';
12   NameArray[2] := 'c';
13   NameArray[3] := 'd';
14   for Indexs := Low(NameArray) to High(NameArray) do begin
15       Writeln(NameArray[Indexs]);  //Screen output
16   end;
17 
18 end;
19 
20 type
21   TMyFlexibleArray = array[0..4] of Char;
22 var
23   TMyArray : TMyFlexibleArray;
24   Elem : Char;  //Consistent with array type
25 procedure MyProc1;
26 begin
27   TMyArray[0] := 'a';
28   TMyArray[1] := 'b';
29   TMyArray[2] := 'c';
30   TMyArray[3] := 'd';
31   for Elem in TMyArray do   //Take the elements in the array
32   begin
33     Writeln(Elem); //Output specific elements in an array
34   end;
35   Writeln(Length(TMyArray)); //Get the maximum available number of arrays (available elements)
36 
37 end;
38 
39 begin
40 //  MyProc();
41   MyProc1();
42   Readln;
43 end.

 

V. dynamic array

 1 {Dynamic array}
 2 type
 3   StringArray = array of string;
 4 var
 5   NArray : StringArray;
 6   NameArray1 : StringArray;
 7   Indexs1 : Integer;
 8   Elem1 : string;
 9 
10 procedure Dynamic_MyProc1;
11 begin
12   SetLength(NArray,20); //Set length, element not created
13 
14   NameArray1 := StringArray.Create('xiaoqian','xiaoming');  //Create array, set length
15   NameArray1 := ['xiaoming','xiaoyang','xioaogang'];  ////Create array, set length
16 //  Writeln(Length(NArray),',',Low(NArray));
17   Writeln(Length(NameArray1),',',Low(NameArray1));
18   {Traversal 1 array for to }
19   for Indexs1 := Low(NameArray1) to High(NameArray1) do  begin
20     Writeln(NameArray1[Indexs1]);
21   end;
22   {Traversal 2 array for in}
23   for Elem1 in NameArray1 do
24   begin
25     Writeln(Elem1);
26   end;
27 end;
28 
29 begin
30 
31   Dynamic_MyProc1();
32   Readln;
33 end.
 1 {Dynamic array 2}
 2 type
 3   StringArray1 = array of string;
 4 var
 5   NameArray2 : StringArray1;
 6   AddressArray : StringArray1;
 7   DataArray : StringArray1;
 8   DataArray1: StringArray1;
 9   Name : string;
10   Indexs2 :Integer;
11   NameArray3 : StringArray1;
12 
13 procedure Dynamic_MyProc2;
14 begin
15   NameArray2 := ['Beijing','Nanjing','Tianjin'];
16   AddressArray := ['Lin Chiling','Xiao Qiang'];
17   {Concat Concatenate arrays}
18   DataArray := Concat(NameArray2,AddressArray);
19 
20   {Copy array,High(DataArray1),Copy Head without tail}
21   for Name in DataArray do begin
22 //    Writeln(Name)     //Beijing, Nanjing, Tianjin, Lin Zhiling, Xiao Qiang
23   end;
24 
25   DataArray1 := Copy(DataArray,Low(DataArray),High(DataArray));
26   for Indexs2 := Low(DataArray1) to Length(DataArray1) do begin
27 //    Writeln(DataArray1[Indexs2])     //Beijing, Nanjing, Tianjin, Lin Zhiling, Xiao Qiang
28   end;
29 
30   {Insert insert}
31    NameArray3 := ['11','22'];   //Data waiting to be inserted
32   Insert(NameArray3,DataArray,10);
33   for Name in DataArray do begin
34 //    Writeln(Name)     //Beijing Nanjing Tianjin Lin Zhiling Xiao Qiang 11 22
35   end;
36 
37   {Delete delete}
38    Delete(DataArray,Length(DataArray)-2,Length(DataArray));
39   for Name in DataArray do begin
40       Writeln(Name)     //Beijing, Nanjing, Tianjin, Lin Zhiling, Xiao Qiang
41   end;
42 
43 end;
44 
45 begin
46 
47   Dynamic_MyProc2();
48   Readln;
49 end.

 

Vi. multidimensional array

 1 {multidimensional array: 1. Static multidimensional array 2. Dynamic multidimensional array}
 2 type
 3 {declare a dynamic binary array}
 4   TStringDynamicArray2 = array of array of string;
 5 {declare a static two-dimensional array}
 6   TStringStaticArray3 = array[0..3] of array[0..2] of string;
 7 var
 8   TDynamicNameArray : TStringDynamicArray2;
 9   TStaticArray : TStringStaticArray3;
10   I,J : Integer;
11 {initialize dynamic binary array}
12 procedure TStringDynamic_Array2;
13 begin
 14 tdynamicnamearray: = ['Zhang San', 'Li Si', 'Wang Wu'], ['Chengdu', 'Chongqing', 'Xi'an']];
15 {traversal 2-D array}
16   for I := Low(TDynamicNameArray) to High(TDynamicNameArray) do begin
17     for J := Low(TDynamicNameArray[I]) to High(TDynamicNameArray[I]) do begin
18       Writeln(TDynamicNameArray[I][J]);
19     end;
20 
21   end;
22 
23   Writeln(TDynamicNameArray[0][1]);
24 end;
25 
26 {initialize static binary array}
27 procedure TStringStatic_Array3;
28 begin
 29 tstaticrarray [0] [0]: = 'happy twist';
30    for I := Low(TStaticArray) to High(TStaticArray) do begin
31     for J := Low(TStaticArray[I]) to High(TStaticArray[I]) do begin
32       Writeln(TStaticArray[I][J]);
33     end;
34 
35   end;
36 end;
37 
38 begin
39 //  TStringDynamic_Array2();
40   TStringStatic_Array3();
41   Readln;
42 end.

 

7. Basic exercises

  1 type
  2   {Define the record type of a student's information}
  3   TStudent = record
  4     Id: string; //Student ID
  5     Name: string; //Full name
  6   end;
  7 
  8 {Array storage information}
  9 TStudentList = array of TStudent;
 10 var
 11   Student: TStudent;
 12   Studentlist : TStudentList;
 13 
 14 {view menu}
 15 procedure ShowMemo();
 16 begin
 17   Writeln('');
 18   Writeln('*****Memo*****');
 19   Writeln('1,increase');
 20   Writeln('2,query');
 21   Writeln('3,modify');
 22   Writeln('4,delete');
 23   Writeln('');
 24 end;
 25 
 26 {Add user information}
 27 procedure AddStudent();
 28 var
 29   UserName : string;
 30   ArrayLength : Integer;
 31   Uuid:TGUID;   //Generate a unique ID
 32 begin
 33   Writeln('You have selected the add function. Please enter the student name');
 34   Readln(UserName);
 35   Student.Name := UserName;
 36   ArrayLength := Length(Studentlist);
 37   CreateGUID(Uuid);
 38   Student.Id := GUIDToString(Uuid);
 39 //  Writeln(GUIDToString(Uuid));   //Output Uuid information
 40 
 41   Insert(Student,Studentlist,ArrayLength-1); //Add to
 42   if (Length(Studentlist)>ArrayLength) then begin
 43     Writeln('Added successfully!');   //output Uuid information
 44   end else begin
 45     Writeln('Add failure');
 46   end;
 47 
 48 end;
 49 
 50 {Query user information}
 51 procedure SelectStudent();
 52 var
 53   Student1 : TStudent;
 54 begin
 55   Writeln('You have selected the query function, and the query results are as follows:');
 56   for Student1 in Studentlist do begin
 57     with Student1 do begin
 58       Writeln('Full name:',Name,#$09,'number:',Id);
 59     end;
 60   end;
 61 end;
 62 
 63 {Delete user information}
 64  procedure DeleteStudent();
 65 var
 66 Student2 : TStudent;
 67 DelUserName : string;
 68 IsTrue : string;
 69 Indexs : Integer;
 70 ArrayLength : Integer;
 71 begin
 72   Writeln('You have selected the deletion function, please output the deletion name');
 73   Readln(DelUserName);
 74   Indexs := 0;
 75   {Get the sum of data before deletion}
 76   ArrayLength := Length(Studentlist);
 77   for Student2 in Studentlist do begin
 78     with Student2 do begin
 79       if (DelUserName=Name) then begin
 80         {The deletion operation needs to be careful and the user needs to confirm it twice}
 81         Writeln('You confirm to delete',Name,',',Id,'Information? Y/N');
 82         Readln(IsTrue);
 83         if (IsTrue = 'Y') then begin
 84           Delete(Studentlist,Indexs,1);
 85         {After deletion,Compare the total number of elements to determine whether it is successful}
 86           if (Length(Studentlist) > ArrayLength) then begin
 87             Writeln('Delete successful');
 88           end
 89           else begin
 90             Writeln('delete!');
 91           end;
 92         end;
 93         Exit;
 94       end;
 95     end;
 96     Inc(Indexs);  //Add 1 at a time
 97   end;
 98 end;
 99 
100 {To update/modify}
101 procedure UpdateUser();
102 var
103   Uname : string;
104   I : Integer;
105   IsExist : Boolean;
106 begin
107   IsExist := False; {This message does not exist by default}
108   Writeln('You have selected the modify function,Please output the required user name:');
109   Readln(Uname);
110   {Traverse the array to query the data to be modified}
111   for I := Low(Studentlist) to High(Studentlist) do begin
112     with Studentlist[I] do begin
113       if (Uname = Name) then begin
114         Writeln('Please output a new name');
115         Readln(Uname); //Give again Uanme assignment
116         Name := Uname;
117         IsExist := True;
118         Break;
119       end;
120     end;
121   end;
122   if (IsExist = False) then begin
123   Writeln('This information does not exist');
124   end;
125 end;
126 
127 {User selection function}
128 procedure UserChoose();
129 var
130   UserChooseIndex: Integer;
131 begin
132   Writeln('Please enter your feature options:');
133   {Get user input number}
134   Readln(UserChooseIndex);
135   {Judge user input number}
136   case UserChooseIndex of
137     1:
138       begin
139         AddStudent();
140       end;
141     2:
142       begin
143         SelectStudent();
144       end;
145     3:
146       begin
147         UpdateUser();
148       end;
149     4:
150       begin
151         DeleteStudent();
152       end;
153 
154   else
155     begin
156       Writeln('Please output the correct function number');
157     end;
158   end;
159 end;
160 
161 begin
162 {Initialize student information list}
163 Studentlist := [];
164 {Infinite cycle}
165   while True do
166   begin
167     ShowMemo();
168     UserChoose();
169   end;
170   Readln;
171 end.

Topics: Delphi Spring