You are on page 1of 12

7.

1 Enumeration Types (1)


• Keyword: enum
• Allows the programmer to assign a user-defined
name to a set with a finite number of elements.
tag name
/* type specification */

enum day { sun, mon, tue, wed, thu, fri, sat };

enumerators
{ 0, 1, 2, 3, 4, 5, 6}
– The value of enumerators are constant integers.
– By default, the first one is given the value 0 and each
succeeding one has the next integer value.
– Enumerators can be considered as programmer-
specified constants.
7.1 Enumeration Types (2)
/* specification of enumeration type */
enum day { sun, mon, tue, wed, thu, fri, sat };
/* to declare variables of type enum day */
enum day d1, d2;
/* to assign a value sun to d1 */
d1 = sun;
/* to assign a value wed+1 to d2 */
d2 = wed + 1;
printf("The value of d1 = %d", d1); /* 0 is printed */
printf("The value of d2 = %d", d2); /* 4 is printed */
– The variables d1 and d2 are declared as a type
enum day.
– The variables d1 and d2 can have as their values
only the enumerators in the set.
7.1 Enumeration Types (3)

enum suit { clubs = 1, diamonds, hearts, spades } a, b, c;

variables of
type specifier
this type

– The enumerator clubs is initialized as 1.


– The successive enumerators of diamonds, hearts,
and spades have the values 2, 3, and 4, respectively.
– We can declare variables of the enumeration type
right after the enumerator declaration.

enum veg {beet = 17, corn = 17} vege1, vege2;

– The different enumerators can have the same


initialization value.
7.2 The Use of typedef
• Allows the programmer to use type names that
are appropriate for a specific application.

typedef int color;


The identifier color is
color red, blue, green; synonymous with a type int

enum day { sun, mon, tue, wed, thu, fri, sat };

enum day d1;


The identifier day is
typedef enum day day; synonymous with
a user-defined type
day d2; enum day
/* 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 tue; next_day = wed; break;
case wed; next_day = thu; break;
case thu; next_day = fri; break;
case fri; next_day = sat; break;
case sat; next_day = sun; break;
}
return next_day;
}
4.3 An Example: The Game of Paper, Rock, Scissors

• The structure of the program


#include directives
p_r_s.h declarations for enumeration types
type definitions
function prototypes

main.c select.c compare.c report.c prn.c


#include "p_r_s.h" to select to determine to report the to print out
int main(void) among the outcome results the final
...... paper, between the (win or lose) results of the
rock, player and of each game whole games
scissors the machine

– The following source code is slightly modified from the


original code in the text book
/* 1) In file p_r_s.h */
 

#include <ctype.h> /* for isspace() */


#include <stdio.h> /* for printf(), etc */
#include <stdlib.h> /* for rand() and srand() */
#include <time.h> /* for time() */

enum p_r_s {paper, rock, scissors, quit};


enum outcome {win, lose, tie, error};

typedef enum p_r_s p_r_s;


typedef enum outcome outcome;

outcome compare(p_r_s player_choice, p_r_s machine_choice);

void report(outcome result, int *win_cnt_ptr, int *lose_cnt_ptr, int *tie_cnt_ptr);


void prn_final_status(int win_cnt, int lose_cnt);

p_r_s selection_by_machine(void);
p_r_s selection_by_player(void);
/* 2) In file main.c: */
 

#include "p_r_s.h"
int main(void)
{
int win_cnt = 0, lose_cnt = 0, tie_cnt = 0;
outcome result;
p_r_s player_choice, machine_choice;
 

srand(time(NULL)); /* seed the random number generator */


 

while (( player_choice = selection_by_player() ) != quit)


switch (player_choice) {
case paper:
case rock:
case scissors:
machine_choice = selection_by_machine();
result = compare(player_choice, machine_choice);
report(result, &win_cnt, &lose_cnt, &tie_cnt);
break;
default: printf("PROGRAMMER ERROR: Cannot get to here!\n\n"); exit(1);
}
prn_final_status(win_cnt, lose_cnt, tie_cnt);
return 0;
}
/* 3) In file select.c: */
 

#include "p_r_s.h"

p_r_s selection_by_machine(void)
{
return ( (p_r_s) (rand() % 3) );
}
 
p_r_s selection_by_player(void)
{
char c;
p_r_s player_choice;
 

printf("Select among p( 보 ), r( 바위 ), or s( 가위 ) (q = quit) :


");
scanf(" %c", &c);
switch (c) {
case 'p': player_choice = paper; break;
case 'r': player_choice = rock; break;
case 's': player_choice = scissors; break;
case 'q': player_choice = quit; break;
}
return player_choice;
/* 4) In file compare.c: */
 

#include "p_r_s.h"

outcome compare(p_r_s player_choice, p_r_s machine_choice)


{
outcome result;
 

if (player_choice == machine_choice)
return tie;

switch (player_choice)
{
case paper: /* 보 */
result = (machine_choice == rock) ? win : lose; break;
case rock: /* 바위 */
result = (machine_choice == scissors) ? win : lose; break;
case scissors: /* 가위 */
result = (machine_choice == paper) ? win : lose; break;
default:
printf("PROGRAMMER ERROR: Unexpected choice!\n\n"); exit(1);
}
return result;
}
/* 5) In file report.c: */

#include "p_r_s.h"

void report(outcome result, int *win_cnt_ptr, int *lose_cnt_ptr, int *tie_cnt_ptr)


{
switch (result) {
case win:
++*win_cnt_ptr;
printf("%27sYou win.\n", "");
break;
case lose:
++*lose_cnt_ptr;
printf("%27sYou lose.\n", "");
break;
case lose:
++*tie_cnt_ptr;
printf("%27sA tie.\n", "");
break;
default:
printf("PROGRAMMER ERROR: Unexpected result!\n\n"); exit(1);
}
}
/* 6) In file prn.c: */
 

#include "p_r_s.h"

void prn_final_status(int win_cnt, int lose_cnt)


{
printf("\n%s\n%s%4d\n%s%4d\n%s%4d\n%s%4d\n\n",
"GAME STATUS:",
" Win: ", win_cnt,
" Lose:", lose_cnt,
" Tie: ", tie_cnt,
" Total:", win_cnt + lose_cnt + tie_cnt);

if (win_cnt > lose_cnt)


printf("CONGRATULATIONS -You won!\n\n") ;
else if (win_cnt == lose_cnt)
printf("A DRAW - You tied!\n\n");
else
printf("SORRY - You lost!\n\n");
}
 

You might also like