S.K.H. Kei Hau Secondary School 2003-2004 Set Data type S.K.H. Kei Hau Secondary School Department of Computer AL Computer Studies Set Data Type Content 1. Set Data Type 2. Set Operator 3. Set Union, Intersection, and Difference 1. Set Data Type Sets are structured variables that contain lists of integers, characters, or enumerated type values. Type DigitSet = Set of 0..9; Var Odds Evens, Middle, Mixed : DigitSet; (* four sets *) Set assignment and set literals Set literal is a list of values from the set base type enclosed in brackers. Odds := [1, 3, 5, 7, 9]; Evens := [0, 2, 4, 6, 8]; The empty set It has zero elements and is denoted by a pair of brackets. (different from undefined set) Middle := []; 2016/2/12 1/6 S.K.H. Kei Hau Secondary School 2003-2004 Set Data type Sets with enumerated type value You can define sets with values chosen from your own enumerated data type. Type Cars = (Dodge, Ford, Lincoln, Cadillac, Fiesta, Pointiac, Corvette, Buick, Chevrolet, Mercury, Mustang); CarSet = set of cars; Var Avis, Hertz, Merger : CarSet; Avis := [Dodge, Lincoln, Fiesta]; Hertz := [Dodge..Cadillac, Mercury]; Merger := [Dodge..Mustang]; Avis consists of three elements. Hertz consists of five elements. Merger, which is the universal set for type CarSet, consists of all elements. So, the following assignment is valid: Merger := Hertz Note that: Valid set: [1, 3, 1..5] Invalid set: [1, 3, ‘1’ .. ‘5’] 2. Set Operator Pascal provides a set membership operator, in, to determine whether a particular value is a set element. Consider: Statement 1: (ch = ‘.’) or (ch = ‘?’) or (ch = ‘;’) or (ch = ‘!’) Statement 2: Ch in [‘.’, ‘?’, ‘;’, ‘!’] Both conditions are true if ch is one of the characters in the set. 2016/2/12 2/6 S.K.H. Kei Hau Secondary School 2003-2004 Set Data type Example Type CharSet = set of Char; Var Vowels, Uppercase, Lowercase : CharSet; (* 3 sets *) NextChar : Char; Begin Vowels := [‘A’, ‘a’, ‘E’, ‘e’, ‘I’, ‘i’, ‘O’, ‘o’, ‘U’, ‘u’]; Uppercase := [‘A’ .. ‘Z’]; Lowercase := [‘a’ .. ‘z’]; … … repeat write(‘Enter a letter>’); Readln(NextChar); If NextChar in Vowels then Writeln(NextChar, ‘ is a vowel’) Else if NextChar in Uppercase then Writeln(NextChar, ‘ is an uppercase consonant’) Else if NextChar in Lowercase then Writeln(NextChar, ‘ is a lowercase consonant’) Else Writeln(NextChar, ‘ is not a letter’) Until NextChar in [‘A’ .. ‘Z’, ‘a’ .. ‘z’] … … Note that, if you want to use the complement of the condition statement, Valid statement: Not NextChar in [‘A’ .. ‘Z’, ‘a’ .. ‘z’] Invalid statement: NextChar Not in [‘A’ .. ‘Z’, ‘a’ .. ‘z’] 2016/2/12 3/6 S.K.H. Kei Hau Secondary School 2003-2004 Set Data type 3. Set Union, Intersection, and Difference Union Intersection Difference Operation Condition Operator Union Or + Intersection And * Difference --- - Note that, The difference of set A and set B is the set of elements that are in set A but not in set B. The set operator ‘-’ is not commutative, that is A-B is different to B-A The set operator ‘+’ and ‘*’ are commutative. The precedence rules of the set operator in Pascal is +, * and –. Example [1, 3, 4] + [1, 2, 4] is [1, 2, 3, 4] [‘A’, ‘C’, ‘F’] + [‘A’, ‘C’, ‘D’, ‘F’] is [‘A’, ‘C’, ‘D’, ‘F’] [1, 3, 4, 5] + [2] is [1, 2, 3, 4, 5] (unit set) [1, 3, 4] * [1, 2, 4] is [1, 4] [‘A’, ‘C’, ‘F’] + [‘A’, ‘C’, ‘D’, ‘F’] is [‘A’, ‘C’, ‘F’] [1, 3, 4] – [1, 2, 4] is [3] [1, 3] – [2, 4] is [1,3] [‘A’, ‘C’, ‘F’] – [‘B’, ‘C’, ‘D’, ‘F’] is [‘A’] [‘A’, ‘C’, ‘F’] – [[‘A’, ‘C’, ‘D’, ‘F’] is [] (empty set) Common errors [1, 3, 4, 5] + 2 (2nd operand is not a set) Avis + Cadillac (2nd operand is not a set) [Avis] + [Cadillac] (1st operand is not a set) 2016/2/12 4/6 S.K.H. Kei Hau Secondary School 2003-2004 Set Data type Set relational operators = , <>, <= and >= Operator Meaning = Set equality <> Set inequality <= Subset >= Superset Example [1, 3] = [1, 3] is true [1, 3] = [2, 4] is false [1, 3] = [3, 1] is true [] = [1] is false [1, 3] <> [1, 3] is false [1, 3] <> [2, 4] is true [1, 3] <> [3, 1] is false [] <> [1] is true Set A is a subset of set B (A<= B) If every element of A is also an element of B: [1, 3] < [1, 2, 3, 4] is true [1, 3] <= [1, 3] is true [] <= [1, 3] is true Set A is a superset of set B (A>=B) if every element of B is also an element of A: [1, 3] >= [1, 2, 3, 4] is false [1, 3] <= [1, 3] is true [] >= [1, 3] is false 2016/2/12 5/6 S.K.H. Kei Hau Secondary School 2003-2004 Set Data type Read and write set Example Read set Procedure ReadSet (var Letters : CharSet); Const Sentine1 = ‘.’; Var NextChar : Char; Begin Letters :[]; Writeln(‘enter a sentence ending with symbol’, Sentine1); Read(NextChar); While NextChar <> Sentine1 do Begin NextChar := UpCase(NextChar); If NextChar in [‘A’ .. ‘Z’] then Letters := Letters + [NextChar]; Read(NextChar) End End; Write Set Procedure PrintSet(Letters : CharSet); Var NextLetter : Char; Begin Write(‘{’); For NextLetter := ‘A’ to ‘Z’ fo If NextLetter in Letters then Write(NextLetter, ‘, ’); Writeln(‘}’) End; 2016/2/12 6/6