一、枚举类型的定义
枚举类型的定义:枚举类型(enumeration)是 C++ 中的一种派生数据类型,它是由用户定义的若干枚举常量的集合。 定义格式:枚举类型的定义格式为:enum <类型名> {<枚举常量表>};
例如:
enum color_set1 {RED, BLUE, WHITE, BLACK};
enum week {Sun, Mon, Tue, Wed, Thu, Fri, Sat};
二、枚举声明
-
枚举类型定义与变量声明分开 如: enum Suit { Diamonds, Hearts, Clubs, Spades };
enum Suit a;
enum Suit b,c;
变量a,b,c 的类型都定义为枚举类型enum Suit 。 -
枚举类型定义与变量声明同时进行 如: enum Suit { Diamonds, Hearts, Clubs, Spades }a,b,c;
enum { Diamonds, Hearts, Clubs, Spades }a,b,c;
-
用typedef 先将枚举类型定义为别名,再利用别名进行变量的声明 有以下几种方式: typedef enum Suit { Diamonds, Hearts, Clubs, Spades }Suit;
enum Suit a;
enum Suit b,c;
typedef enum{ Diamonds, Hearts, Clubs, Spades }Suit;
enum Suit a;
enum Suit b,c;
typedef enum Suit { Diamonds, Hearts, Clubs, Spades };
enum Suit a;
enum Suit b,c;
注意: 同一程序中不能定义同类型名的枚举类型;不同枚举类型的枚举元素不能同名。 枚举变量的值只能取枚举常量表中所列的值,就是整型数的一个子集。 枚举变量占用内存的大小与整型数相同。 枚举型可以隐式的转换为int 型,int型不能隐式的转换为枚举型。 枚举变量只能参与赋值和关系运算以及输出操作,参与运算时用其本身的整数值。
三、 使用枚举类型的变量
3.1 对枚举型的变量赋值。
实例将枚举类型的赋值与基本数据类型的赋值进行了对比: 方法一:先声明变量,再对变量赋值
#include<stdio.h>
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN };
int main()
{
int x, y, z;
x = 10;
y = 20;
z = 30;
enum DAY yesterday, today, tomorrow;
yesterday = MON;
today = TUE;
tomorrow = WED;
printf("%d %d %d \n", yesterday, today, tomorrow);
}
方法二:声明变量的同时赋初值
#include <stdio.h>
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN };
itn main()
{
int x=10, y=20, z=30;
enum DAY yesterday = MON,
today = TUE,
tomorrow = WED;
printf("%d %d %d \n", yesterday, today, tomorrow);
}
方法三:定义类型的同时声明变量,然后对变量赋值。
#include <stdio.h>
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN } yesterday, today, tomorrow;
int x, y, z;
int main()
{
x = 10; y = 20; z = 30;
yesterday = MON;
today = TUE;
tomorrow = WED;
printf("%d %d %d \n", x, y, z);
printf("%d %d %d \n", yesterday, today, tomorrow);
}
方法四:类型定义,变量声明,赋初值同时进行。
#include <stdio.h>
enum DAY
{
MON=1,
TUE,
WED,
THU,
FRI,
SAT,
SUN
}
yesterday = MON, today = TUE, tomorrow = WED;
int x = 10, y = 20, z = 30;
int main()
{
printf("%d %d %d \n", x, y, z);
printf("%d %d %d \n", yesterday, today, tomorrow);
}
3.2 对枚举型的变量赋整数值时,需要进行类型转换
#include <stdio.h>
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN };
int main()
{
enum DAY yesterday, today, tomorrow;
yesterday = TUE;
today = (enum DAY) (yesterday + 1);
tomorrow = (enum DAY) 30;
printf("%d %d %d \n", yesterday, today, tomorrow);
}
3.3 使用枚举型变量
#include<stdio.h>
enum
{
BELL = '\a',
BACKSPACE = '\b',
HTAB = '\t',
RETURN = '\r',
NEWLINE = '\n',
VTAB = '\v',
SPACE = ' '
};
enum BOOLEAN { FALSE = 0, TRUE } match_flag;
int main()
{
int index = 0;
int count_of_letter = 0;
int count_of_space = 0;
char str[] = "I'm Ely efod";
match_flag = FALSE;
for(; str[index] != '\0'; index++)
if( SPACE != str[index] )
count_of_letter++;
else
{
match_flag = (enum BOOLEAN) 1;
count_of_space++;
}
printf("%s %d times %c", match_flag ? "match" : "not match", count_of_space, NEWLINE);
printf("count of letters: %d %c%c", count_of_letter, NEWLINE, RETURN);
}
输出:
match 2 times
count of letters: 10
Press any key to continue
四、综合实例
#include<stdio.h>
enum Season
{
spring, summer=100, fall=96, winter
};
typedef enum
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
Weekday;
int main()
{
printf("%d \n", spring);
printf("%d, %c \n", summer, summer);
printf("%d \n", fall+winter);
Season mySeason=winter;
if(winter==mySeason)
printf("mySeason is winter \n");
int x=100;
if(x==summer)
printf("x is equal to summer\n");
printf("%d bytes\n", sizeof(spring));
printf("sizeof Weekday is: %d \n", sizeof(Weekday));
Weekday today = Saturday;
Weekday tomorrow;
if(today == Monday)
tomorrow = Tuesday;
else
tomorrow = (Weekday) (today + 1);
}
重要提示 枚举变量可以直接输出,但不能直接输入。如:cout >> color3; //非法 不能直接将常量赋给枚举变量。如: color1=1 ; //非法 不同类型的枚举变量之间不能相互赋值。如: color1=color3 ; //非法 枚举变量的输入输出一般都采用switch语句将其转换为字符或字符串;枚举类型数据的其他处理也往往应用switch 语句,以保证程序的合法性和可读性。
|