Delphi Basics-Data types

Source: Internet
Author: User
Tags case statement

Enum type

The Pascal program is not only used for numerical processing, but is also more widely used to process non-numeric data. For example, gender, month, day of the week, color, unit name, education, occupation, etc.
?

1. Definition of enum type

Format: type enum Types identifier = (identifier 1, identifier 2,..., identifier N)

2. Enumeration type characteristics
    • An enumeration element can only be an identifier, and all enumerated elements that are listed when the enumeration type is defined form the value range of this enumeration type. For example, the following type definitions are legal:
type    days=(sun,mon,tue,wed,thu,fri,sat);    colors=(red,yellow,blue,white,black,green);

The following type definitions are incorrect:

`Pascal type colortype=(‘red‘,‘yellow‘,‘blue‘,‘white‘); numbers=(1,3,5,7,9);

    • Enum types are order types
      The ordinal of each enumeration element is determined according to the order in which it is defined, and the sequence number starts at 0. For example, define type days= (SUN,MON,TUE,WED,THU,FRI,SAT); Then, Ord (Sun) =0,ord (Mon) =1,......, and so on. The first element in an enumeration type has no previous tendency, and the last element has no successor. Pred (Sat) =fri; SUCC (Sun) =mon; Ord (Sat) = 6;
    • The same enumeration element cannot appear in two or more than two enumeration type definitions. The following definitions are incorrect:
  type color1=(red,yellow,white);    color2=(blue,red,black);   // 因为red属于枚举类型color1和 color2    
    • Enumeration type variables can only perform assignment and relational operations, and cannot perform arithmetic and logical operations. When an enumeration element is compared, it is actually a comparison of its ordinal number.
      For example, define the following:
    type         days=(sun,mon,tue,wed,thu,fri,sat);        colors=(red,yellow,blue,white,black,green);      var         color:colors;         weekday:days;       //则下面语句是合法的:       weekday:=mon;      thenwrite(‘rest‘);       //而下面语句是不合法的:          mon:=1;           //错把枚举值当成变量名;          weekday:=blue;   //枚举值blue不属于枚举变量weekday的值域;          read(color);      //枚举类型变量 不能用读语句进行赋值;          write(weekday); writeln(blue);//不能通过写语句输出枚举类型的变量值和枚举值。      
    • You can combine the description of a variable with the definition of a type, such as:
    var         holiday,workday:(sun,mon,tue,wed,thu,fri,sat);          color:(red,yellow,blue,white,black,green);   
对枚举数据的输入与输出可通过间接方式进行。输入时,一般可输入一个代码,通过程序进行转换,输出时,也只是打印出与枚举元素相对应的字符串。这在后面的例题中将有使用示例。  
3. Application examples
    • Example 1, enter today is the number of days of the week, the output tomorrow is the day of the English word (Sunday serial number 0).
Type weekday= (SUN,MON,TUE,WED,THU,FRI,SAT);varIinteger; Today,tomorrow:weekday;beginWriteln (' What's date is it '); READLN (i); CaseI of                              {convert from input to enum}          0: Today:=sun;1: Today:=mon;2: Today:=tue;3: today:=wed;4: Today:=thu;5: Today:=fri;6: Today:=sat;End; if (TODAY=SAT) ThenTomorrow:=sunElseTOMORROW:=SUCC (today);Write(' The Tomorrow is '); CaseTomorrow ofSun:writeln (' Sunday '); Mon:writeln (' Monday '); Tue:writeln (' Tuesday '); Wed:writeln (' Wednesay '); Thu:writeln (' Thursday '); Fri:writeln (' Friday '); Sat:writeln (' Saturday ');End;End.

PS: An enumeration type is an ordered type, so a variable of the enumeration type can be used as a loop variable.

Child boundary type
    • If we define a variable to be an integer, then its value range is generally -32768~32767. In fact, the values of the variables used in each program have a definite range. For example, a person's age is generally 1-120 years, the number of months in a year is January-December, the number of days in January is 1-31 days, and so on.
    • If you can specify the range of variables used in the program, it is convenient to check out the illegal data, which can better guarantee the correctness of the program operation and save the memory space to some extent.

    • The sub-boundary type is a good solution to the above problem. In addition, in the definition of the array, it is common to the sub-bounds type to specify the range of the array subscript.

1. Define the format

Type subtype identifier = constant 1: Constant 2

 常量1称为子界的下界,常量2称为子界的上界;  

Precautions:
?

    1. The nether and upper bounds must be of the same order type (the type is called the base type of the child boundary type), and the upper bound ordinal must be greater than the lower bound ordinal number.
      For example type age=1..100;
      Letter= ' a ' ... ' Z ';
    2. You can define a child boundary type directly in the variable description. Such as:
      Type letter= ' a ': ' Z ';
      var ch1,ch2:letter;
      Can be combined into:
      var ch1,ch2: ' A ' ... ' d ';
2. Operational rules
    • An operation rule that can use a base type also applies to that type's sub-bounds type.
      For example, you can use an integer variable, or you can use a sub-boundary type data that is based on an integral type.

    • An operation rule for a base type also applies to the subtype type of the type.
      For example, Div,mod requires that the data to participate in the operation be integer, and therefore can be any of the sub-bounds of the integer type data.
    • Data of different sub-boundary types with the same base type can be mixed.
      For example: with the following instructions:
      var x:1..100;
      y:1..500;
      z:1..1000;
      A:integer;
      The following statements are valid: A:=SQR (x) +y+z; Z:=x+y
      The following statement: Y:=x+z+a; When the value of X+y+a is within the 1~500 range, it is legal, otherwise an error occurs.

3. Application examples
    • Example 1, using the sub-boundary type Case statement, when input month, day, year (10 30 1986), Output of OCT 1986.
  varMonth1.. A; Day1.. to; Year1900..2003;begin    Write(' Enter date (mm dd yy): '); READLN (month, day, year);Write(day); CaseMonth of      1:Write(' Jan ');2:Write(' Feb ');3:Write(' Mar ');4:Write(' APR ');5:Write(' may ');6:Write(' June ');7:Write(' Jul ');8:Write(' the ");9:Write(' Sep ');Ten:Write(' Oct '); One:Write(' Nov '); A:Write(' Dec ');End;
    • Example 2, converts a four-bit hexadecimal number to a decimal number.
    varCh:Char; N:1..4; D1, D2, D3, D4, T:0.. the; SReal;begin      Write(' The hex number is '); forN: =1  to 4  Do      begin                    {Four hexadecimal digits are read as characters four times}        Read(CH);Write(CH);{Convert to decimal number D1,D2,D3,D4,}        if(Ch >=' 0 ') and(Ch <=' 9 ') ThenT: = Ord (CH)- -;if(Ch >=' A ') and(Ch <=' Z ') ThenT: = Ord (CH)- the;if(Ch >=' A ') and(Ch <=' Z ') ThenT: = Ord (CH)- -; CaseN of          1: di: = t;2: d2: = t;3: d3: = t;4: D4: = t;End      End; s: = D1 * -* -* -+ D2 * -* -+ D3 * -+ D4; Writeln (' Dec: ', s)End.
Collection type

A collection is a whole that consists of elements that have some common characteristics. In Pascal, a collection is made up of a set of data elements that have the same ordered type, which is called the base type of the collection:

1. Definition of the collection type and description of the variable
    • The general form of a collection type is: set of base type;
    • A base type can be any order type, not a real or other constructed type. At the same time, the number of the base type data must not exceed 255. For example, the following instructions are legal:
  type    setof0..9;    setofchar;    day = (sun, mon, tue, wed, thu, fri, sat);  var    s: numbers;    c: ch;    weekday: day;  
    • You can combine type descriptions with variable descriptions, such as:
      "' Pascal
      var s:set of 0..9; {Sub-bounds}
      C:set of Char;
      Weekday: (SUN,MON,TUE,WED,THU,FRI,SAT); {enum-type}

```

PS Note: The number of elements in the collection is no more than 256, so var s:set of Integer; Is wrong.

2. Value of the collection
    1. The value of the collection is placed in an opposite bracket, separated by commas between the elements. such as: [1,2,5] and [' A ', ' e ', ' I '] are all collections.

    2. There can be no element in the collection, such a collection is called an empty set. [] Empty Set

    3. In the collection, if the value of the element is contiguous, the representation of the child-bounded type is available. For example:
      [1,2,3,4,5, 10,15] can be expressed as: [1.. 5,10,15]
    4. The value of the collection is independent of the order in which the elements appear in square brackets. Values such as [1,5,8] and [5,1,8] are equal.

    5. Repeated occurrences of the same element in the collection have no effect on the value of the collection. For example, [1,8,5,1,8] and [1,5,8] values are equal.

    6. Each element can be represented by an expression allowed by the base type. such as [1,1+2,4], [SUCC (CH)]

3. Operation of the Set

Set type variables cannot perform arithmetic operations, collections are unordered, and functions such as Ord, Pred, SUCC, and so on cannot be used.

    1. Assignment operation: The set variable can only be assigned by an assignment statement, cannot be assigned by a read statement, nor can the value of the set variable be output directly through a write statement. Such as:
      Set Variable assignment: c:=[' 2 ']; I:=[5]; W:=[];
      Set Variable assignment sub-bounds value: c:=[' a ' ... ' Z ']; I:=[1..7];
      Set Variable Assignment enumeration value: c:=[' A ', ' B ', ' d ', ' m ']; I:=[2,4,6,8,10];
      function Assignment Operation:
      Add a collection element include (S, 1);
      Deletes a collection element Exclude (s, 1);

    2. The combination, intersection, and difference operations of a set
      The set can be made with (+), cross (*), Difference (-) three operations, each operation has only one operator, two operands, and the result of the operation is still set. Note the difference between them and arithmetic operations.
    • and operation (relational algebra operator ∪)
      A, B is a collection of two sets, consisting of the elements in collection a plus all the elements in collection B that are not duplicates of a, and are called collections A and B. That is, a+b, such as:
      [X,y,z]+[x] is [x, Y, z] {All elements not duplicated in two collections}
      [1]+[4] for [1,4]
    • Intersection operation (relational algebra operator ∩)
      A, B is a collection of two collections, consisting of both the elements in collection A and all the elements in collection B, referred to as the intersection of A and B. That is, a B, such as:
      [x, Y, z]
      [x] is [x] {Two identical elements in a collection}
      [X,y,z]*[m] for []
    • Difference operation (relational algebra operator-)
      A A, B is a set of two, which is a set of elements in set B that is the same as a, and is called the difference between the set A and B. i.e. AB, such as:
      [X,y,z]-[x] is [y,z] {All elements in collection A that are not in collection B}
      [X,y,z]-[m] for [x, Y, z]
    1. The relational operation of the set: The result of the operation is a Boolean value
    • Relational operators: = Equal, <> unequal >= contains, meaning the former implies the latter, which is equivalent to the <= contained in the set theory, which implies that the former is embodied in the latter, which is equivalent to the set theory.
      For example: [A,b,c]=[b,c,a] is true, the number of elements is the same, the content is the same regardless of the order of arrangement.
      [A,b,c]>=[a] is true;
      [A,b]<=[a,b,c] is true.
    1. In operation: the right side of in is the set, and the left side is the same expression as the collection base type, which is a Boolean value. In tests whether an element is in the collection. Equivalent to the ∈ in set theory. They are all two mesh operations, and the operands of the first 4 operators are compatible collection types. For example: A in[b,c] is false.
    • Set a:=[1..10]; x is an integer, such as X in collection a removes element x from a, otherwise adds the element x to the collection A. The program section is as follows:
      If x in a then a:=a-[x] else a:=a+[x]

Delphi Basics-Data types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.