Wednesday, November 15, 2017

ປະເພດ Enumerations, SubRanges and Sets ໃນ Delphi (Lesson 5)

ການກຳນົດ enumerations

type
   TSuit = (Hearts, Diamonds, Clubs, Spades);   // Defines enumeration range
 var
   suit : TSuit;                                // Defines enumeration variable
 begin
   suit := Clubs;                               // Set to one of the values
 end;

ການໃຊ້ enumeration numbers

type
   TDay = (Mon=1, Tue, Wed, Thu, Fri, Sat, Sun);   // Enumeration values
 var
   today   : TDay;
   weekend : Boolean;
 begin
   today := Wed;          // Set today to be Wednesday
   
   if today > Fri         // Ask if it is a weekend day
   then weekend := true
   else weekend := false;
 end;

today is set to Wed which has ordinal value = 3
weekend is set to false since Wed (3) <= Fri (5)

ຕົວຢ່າງ

type
   TDay = (Mon=1, Tue, Wed, Thu, Fri, Sat, Sun);   // Enumeration values
 var
   day   : TDay;          // Enumeration variable
 begin
   for day := Mon to Fri do
   begin
     // day has each of the values Mon to Fri ( 1 to 5) in 5 iterations
     // of this loop, allowing you to whatever you want.
   end;
 end;


ການໃຊ້ ​SubRanges

type
   TSmallNum = 0..9;
 var
   smallNum : TSmallNum;
 begin
   smallNum := 5;    // Allowed
   smallNum := 10;   // Not allowed
   smallNum := -1;   // Not allowed
 end;
type
   TUpper = 'A'..'Z';
   TLower = 'a'..'z';
   TDigit = '0'..'9';
 var
   upper : TUpper;
   lower : TLower;
   digit : TDigit;
 begin
   upper := 'G';   // Allowed
   lower := 'g';   // Allowed
   digit := '7';   // Allowed
 
   upper := 'g';   // Not allowed
   lower := '7';   // Not allowed
   digit := 4;     // Not allowed
 end;
 type
   TDay = (Mon=1, Tue, Wed, Thu, Fri, Sat, Sun);   // Enumeration values
   TWeekDays = Mon..Fri;                           // Enumeration subranges
   TWeekend  = Sat..Sun;

ການໃຊ້ Sets

 type
   TDigits = set of '1'..'9';       // Set of numeric digit characters
 var
   digits : TDigits;                // Set variable
   myChar : char;
 begin
   // At the start, digits has all set values switched off
   // So let us switch some on. Notice how we can switch on single
   // values, and ranges, all in the one assignment:
   digits := ['2', '4'..'7'];
 
   // Now we can test to see what we have set on:
   for myChar := '1' to '9' do
     if myChar In digits
     then ShowMessageFmt('''%s'' is in digits',[myChar])
     else ShowMessageFmt('''%s'' is not in digits',[myChar])
 end;

ຜົນ

The In operator tests to see if a set contains a value.
 The ShowMessageFmt function used above displays data in a dialog box.
 Click on the In and ShowMessageFmt items in the code above to learn more.
 
 The data shown is as follows:
 
 '1' is not in digits
 '2' is in digits
 '3' is not in digits
 '4' is in digits
 '5' is in digits
 '6' is in digits
 '7' is in digits
 '8' is not in digits
 '9' is not in digits

Set operators

+  The union of two sets
 *  The intersection of two sets
 -  The difference of two sets
 =  Tests for identical sets
 <> Tests for non-identical sets
 >= Is one set a subset of another
 <= Is one set a superset of another
type
   TNums = set of 1..9;
 var
   nums1, nums2, nums3, nums4, nums5, nums6 : TNums;
 begin
   nums1 := [1,2,3];
   nums2 := [1,2,4];
   nums3 := [1,2,3,4,5,6,7,8,9];
 
   nums4 := nums1 + nums2;    // nums4 now [1,2,3,4]
   nums5 := nums1 * nums2;    // nums5 now [1,2]
   nums6 := nums1 - nums2;    // nums6 now [3]
 
   // Test for equality
   if nums1 = nums2
   then ShowMessage('nums1 =  nums2')
   else ShowMessage('nums1 <> nums2');
 
   // Test for inequality
   if nums1 <> nums3
   then ShowMessage('nums1 <> nums3')
   else ShowMessage('nums1 =  nums3');
 
   // Is nums1 a subset of nums3?
   if nums1 <= nums3
   then ShowMessage('nums1 is a subset of nums3')
   else ShowMessage('nums1 is not a subset of nums3');
 
   // Is nums1 a superset of nums3?
   if nums1 >= nums3
   then ShowMessage('nums1 is a superset of nums3')
   else ShowMessage('nums1 is not a superset of nums3');
 end;

ຜົນ

nums1 <> nums2
 nums1 <> nums3
 nums1 is a subset of nums3
 nums1 is not a superset of nums3

[tag]Enumeration, SubRanges, Sets, Delphi[/tag]

Subscribe

  • RSS Atom

ອອນລາຍ: 1 | ມື້ນີ້: 13 | ວານນີ້: 25 | ທິດນີ້: 93 | ເດືອນນີ້: 872 | ປີນີ້: 11832 | ລວມ: 78935