1 / 19

L ecture 11

L ecture 11. Enumeration Type User-defined Structure Use of typedef Readings: Chapter 7 Section 1-5 , Chapter 12 Section 1-7. Enumeration Types. Allows us to name a finite set and to declare identifiers , called enumerators, for the elements of the set Declaration:

Download Presentation

L ecture 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 11 • Enumeration Type • User-defined Structure • Use of typedef • Readings: Chapter 7 Section 1-5, Chapter 12 Section 1-7

  2. Enumeration Types • Allows us to name a finite set and to declare identifiers, called enumerators, for the elements of the set • Declaration: enum day {sun, mon, tue, wed, thu, fri, sat}; • This creates the user-defined typeenum day • The keyword enum is followed by the tag name day • The enumerators are the identifiers sun, mon,…,sat. They are constant of type int • The value of first enumerator is 0, and each succeeding one has the next integer value i.e.,sun = 0, mon = 1, tue = 2,….,sat = 6 • To declare a new variable with enumeration type: enum day d1;

  3. Enumeration Type (con’t) • Declare multiple variables: enum day d1, d2; • This declares d1 and d2to be of type enum day • The variables can take on as values only the elements (enumerators) in the set Example: d1 = fri; d2 = wed; • Test whether d1 is equal to d2: if (d1 == d2)

  4. Enumeration Type • Initialize enumerators enum suit {clubs = 1, diamonds, hearts, spades}; • clubsis initialized to1, diamonds, hearts, spades have the values of 2, 3, 4 enum fruit {apple = 7, pear, orange = 3, lemon}; • pear has value 8, lemon has value 4 • Multiple values are allowed, but the enumerators themselves must be unique • Initialize enumerators and declare variables: enum suit { clubs=1, diamonds, hearts, spades} a, b, c;

  5. The use of typedef • C provides the typedef facility so that an identifier can be associated with a specific type: • Example: typedef int color; • This makes color a type that is synonymous with int • color can be used to declare variables: color c1, c2; /* c1 and c2 are variables of type color */ • Use typedeffor enumeration type: enum day {sun, mon, tue, wed, thu, fri, sat}; typedef enum day day; day next_day;

  6. Example /* Compute the next day */ enum day {sun, mon, tue, wed, thu, fri, sat}; typedef enum day day; day find_next_day(day d) { day next_day; switch (d) { case sun: next_day = mon; break; case mon: next_day = tue; break; ... case sat: next_day = sun; } return next_day; }

  7. Programming Style • Use of enumeration types is considered a good programming style!!! • Example: Rather than use 0 and 1 to distinguish between alternate choices, a programmer can easily use an enumeration type enum bool {false, true}; enum on_off {off, on}; enum yes_no {no, yes}; • Can further use typedef: typedef enum veg {beet, carrot, corn} veg; OR typedef enum {beet, carrot, corn} veg;

  8. Common Programming Error #include <stdio.h> typedef enum {yes, no} yes_no; yes_no is_good(); void main(){ if (is_good()) printf(“Hooray! A good condition exists!\n”); else printf(“Rats! Another bummer\n”); } yes_no is_good() { ……………… /* return yes if good, otherwise return no */ } • Be careful! The order in which you define enumerators can easily lead to logical errors.

  9. User defined structures • Allows the user to create a container for a collection of data items having different types • Format: Creation of a user-defined structure structtag_name{ data_typevariable1; data_type variable2; ... }; Declare variable with this user defined structure: structtag_namevariable_name;

  10. Variations – create and declare Creation of user defined structure Variable declaration

  11. With user defined structure - structure.c • A structure is a container storing a number of variables • Use a structure to define a card (card type, number) A new user defined structure - card Associate card with structcard

  12. This function returns a structure that contains two members. Variable declaration Use the variable face within the card random with the dot (structure member operator)

  13. The complete program #include <stdio.h> #include <stdlib.h> #include <time.h> typedef enum cardtype {diamond=1, club, heart, spade} cardtype; typedef enum {false, true} boolean; struct card { cardtype face; int num; }; typedef struct card card; card getAcard() /* generate a card with a type and number */ { card random; random.face = rand()%4+1; random.num = rand()%13+1; return random; }

  14. boolean greater(card c1, card c2) { if (c1.num > c2.num) return true; else if (c1.num < c2.num) return false; else { if (c1.face > c2.face) return true; else return false; } }

  15. void printCard(card c) { switch (c.face) { case diamond: printf(“Diamond “); break; case club: printf(“Club “); break; case heart: printf(“Heart “); break; case spade: printf(“Spade “); break; } switch (c.num) { case 11: printf(“J\n”); break; case 12: printf(“Q\n”); break; case 13: printf(“K\n”); break; case 1: printf(“A\n”); break; default: printf(“%d\n”, c.num); /* c.num is 2-10 */ } }

  16. void main() { card myCard, yourCard; srand((unsigned) time(NULL)); /* init. Random generator */ myCard = getAcard(); printf(“My card is:\t”); printCard(myCard); yourCard = getAcard(); printf(“Your card is:\t”); printCard(yourCard); if (greater(myCard, yourCard)) printf(“My card is greater than your card\n”); else if (greater(yourCard, myCard)) printf(“Your card is greater than my card\n”); else printf(“The two cards are the same\n”); }

  17. Another example – structure2.c • Modify the previous program to: • Pick 5 cards from the pile, and find out all the pairs • Modifications: • An array (pile) to store a pile of cards • A variable (nr_left) to keep track of no. of remaining cards • A function (initPile) to initialize the pile of cards • A function (pickAcard) to pick one card from the pile • A function (pair) to check if 2 cards is a pair

  18. card pile[52]; int nr_left; void initPile() { int i; for (i=0; i<52; i++) { pile[i].face = (i/13)+1; pile[i].num = (i%13)+1; } nr_left=52; } card pickAcard() { card random; int i; i = rand()%nr_left; random = pile[i]; pile[i] = pile[nr_left-1]; nr_left--; return random; }

  19. boolean pair(card c1, card c2) { if (c1.num == c2.num) return true; else return false; } void main() { card myCard[5]; int i, j; srand((unsigned) time(NULL)); /* init. random generator */ initPile(); printf(“My cards are:\n”); for (i=0; i<5; i++) { myCard[i] = pickAcard(); printf(“%2d: “, i); printCard(myCard[i]); } for (i=0; i<4; i++) for (j=i+1; j<5; j++) if (pair(myCard[i], myCard[j])==true) printf(“Card %d and %d is a pair\n”, i, j); }

More Related