在这里插入图片描述

一、枚举类型的定义

枚举类型的定义:枚举类型(enumeration)是 C++ 中的一种派生数据类型,它是由用户定义的若干枚举常量的集合。
定义格式:枚举类型的定义格式为:enum <类型名> {<枚举常量表>};

例如:

enum color_set1 {RED, BLUE, WHITE, BLACK}; // 定义枚举类型color_set1
enum week {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; // 定义枚举类型week

二、枚举声明

  1. 枚举类型定义与变量声明分开

    如:

    enum Suit { Diamonds, Hearts, Clubs, Spades };
    	enum Suit a;
    	enum Suit b,c;
    

    变量a,b,c的类型都定义为枚举类型enum Suit。

  2. 枚举类型定义与变量声明同时进行
    如:

    enum Suit { Diamonds, Hearts, Clubs, Spades }a,b,c;
    	//此处类型名可以省略,如以下的声明也是可以的。
    	enum { Diamonds, Hearts, Clubs, Spades }a,b,c;
    
  3. 用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); //输出:10 20 30
    printf("%d %d %d \n", yesterday, today, tomorrow); //输出:1 2 3
}

方法四:类型定义,变量声明,赋初值同时进行。

#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); //输出:10 20 30
    printf("%d %d %d \n", yesterday, today, tomorrow); //输出:1 2 3
}

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; //类型转换
    //tomorrow = 3; //错误

    printf("%d %d %d \n", yesterday, today, tomorrow); //输出:2 3 30
}

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()
{
    /* Season */
    printf("%d \n", spring); // 0
    printf("%d, %c \n", summer, summer); // 100, d
    printf("%d \n", fall+winter); // 193

    Season mySeason=winter;
    if(winter==mySeason)
        printf("mySeason is winter \n"); // mySeason is winter
    
    int x=100;
    if(x==summer)
        printf("x is equal to summer\n"); // x is equal to summer

    printf("%d bytes\n", sizeof(spring)); // 4 bytes

    /* Weekday */
    printf("sizeof Weekday is: %d \n", sizeof(Weekday)); //sizeof Weekday is: 4

    Weekday today = Saturday;
    Weekday tomorrow;
    if(today == Monday)
        tomorrow = Tuesday;
    else
        tomorrow = (Weekday) (today + 1); //remember to convert from int to Weekday
}

重要提示 枚举变量可以直接输出,但不能直接输入。如:cout >> color3; //非法 不能直接将常量赋给枚举变量。如:
color1=1; //非法 不同类型的枚举变量之间不能相互赋值。如: color1=color3; //非法
枚举变量的输入输出一般都采用switch语句将其转换为字符或字符串;枚举类型数据的其他处理也往往应用switch语句,以保证程序的合法性和可读性。

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐