资源描述:
《c语言 - 枚举类型详解》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
C语言详解-枚举类型注:以下全部代码的执行环境为VC++6.0在程序中,可能需要为某些整数定义一个别名,我们可以利用预处理指令#define来完成这项工作,您的代码可能是:#defineMON1#defineTUE2#defineWED3#defineTHU4#defineFRI5#defineSAT6#defineSUN7在此,我们定义一种新的数据类型,希望它能完成同样的工作。这种新的数据类型叫枚举型。1.定义一种新的数据类型-枚举型以下代码定义了这种新的数据类型-枚举型 enumDAY{MON=1,TUE,WED,THU,FRI,SAT,SUN};(1)枚举型是一个集合,集合中的元素(枚举成员)是一些命名的整型常量,元素之间用逗号,隔开。(2)DAY是一个标识符,可以看成这个集合的名字,是一个可选项,即是可有可无的项。(3)第一个枚举成员的默认值为整型的0,后续枚举成员的值在前一个成员上加1。(4)可以人为设定枚举成员的值,从而自定义某个范围内的整数。(5)枚举型是预处理指令#define的替代。(6)类型定义以分号;结束。2.使用枚举类型对变量进行声明新的数据类型定义完成后,它就可以使用了。我们已经见过最基本的数据类型,如:整型int,单精度浮点型float,双精度浮点型double,字符型char,短整型short等等。用这些基本数据类型声明变量通常是这样:chara;//变量a的类型均为字符型char charletter;intx,y,z;//变量x,y和z的类型均为整型intintnumber;doublem,n;doubleresult;//变量result的类型为双精度浮点型double既然枚举也是一种数据类型,那么它和基本数据类型一样也可以对变量进行声明。方法一:枚举类型的定义和变量的声明分开enumDAY{MON=1,TUE,WED,THU,FRI,SAT,SUN};enumDAYyesterday;enumDAYtoday;enumDAYtomorrow;//变量tomorrow的类型为枚举型enumDAYenumDAYgood_day,bad_day;//变量good_day和bad_day的类型均为枚举型enumDAY方法二:类型定义与变量声明同时进行: enum//跟第一个定义不同的是,此处的标号DAY省略,这是允许的。{saturday,sunday=0,monday,tuesday,wednesday,thursday,friday}workday;//变量workday的类型为枚举型enumDAYenumweek{Mon=1,Tue,Wed,Thu,FriSat,Sun}days;//变量days的类型为枚举型enumweekenumBOOLEAN{false,true}end_flag,match_flag;//定义枚举类型并声明了两个枚举型变量方法三:用typedef关键字将枚举类型定义成别名,并利用该别名进行变量声明:typedefenumworkday{saturday,sunday=0,monday,tuesday, wednesday,thursday,friday}workday;//此处的workday为枚举型enumworkday的别名workdaytoday,tomorrow;//变量today和tomorrow的类型为枚举型workday,也即enumworkdayenumworkday中的workday可以省略:typedefenum{saturday,sunday=0,monday,tuesday,wednesday,thursday,friday}workday;//此处的workday为枚举型enumworkday的别名workdaytoday,tomorrow;//变量today和tomorrow的类型为枚举型workday,也即enumworkday也可以用这种方式: typedefenumworkday{saturday,sunday=0,monday,tuesday,wednesday,thursday,friday};workdaytoday,tomorrow;//变量today和tomorrow的类型为枚举型workday,也即enumworkday注意:同一个程序中不能定义同名的枚举类型,不同的枚举类型中也不能存在同名的命名常量。错误示例如下所示:错误声明一:存在同名的枚举类型typedefenum{wednesday, thursday,friday}workday;typedefenumWEEK{saturday,sunday=0,monday,}workday;错误声明二:存在同名的枚举成员typedefenum{wednesday,thursday,friday}workday_1;typedefenumWEEK {wednesday,sunday=0,monday,}workday_2;3.使用枚举类型的变量3.1对枚举型的变量赋值。实例将枚举类型的赋值与基本数据类型的赋值进行了对比:方法一:先声明变量,再对变量赋值#include/*定义枚举类型*/enumDAY{MON=1,TUE,WED,THU,FRI,SAT,SUN};voidmain(){/*使用基本数据类型声明变量,然后对变量赋值*/ intx,y,z;x=10;y=20;z=30;/*使用枚举类型声明变量,再对枚举型变量赋值*/enumDAYyesterday,today,tomorrow;yesterday=MON;today=TUE;tomorrow=WED;printf("%d%d%d ",yesterday,today,tomorrow);}方法二:声明变量的同时赋初值#include/*定义枚举类型*/enumDAY{MON=1,TUE,WED,THU,FRI,SAT,SUN}; voidmain(){/*使用基本数据类型声明变量同时对变量赋初值*/intx=10,y=20,z=30;/*使用枚举类型声明变量同时对枚举型变量赋初值*/enumDAYyesterday=MON,today=TUE,tomorrow=WED;printf("%d%d%d ",yesterday,today,tomorrow);}方法三:定义类型的同时声明变量,然后对变量赋值。#include/*定义枚举类型,同时声明该类型的三个变量,它们都为全局变量*/enumDAY{MON=1,TUE,WED,THU,FRI,SAT,SUN}yesterday,today,tomorrow;/*定义三个具有基本数据类型的变量,它们都为全局变量*/intx,y,z; voidmain(){/*对基本数据类型的变量赋值*/x=10;y=20;z=30;/*对枚举型的变量赋值*/yesterday=MON;today=TUE;tomorrow=WED;printf("%d%d%d ",x,y,z);//输出:102030printf("%d%d%d ",yesterday,today,tomorrow);//输出:123}方法四:类型定义,变量声明,赋初值同时进行。#include/*定义枚举类型,同时声明该类型的三个变量,并赋初值。它们都为全局变量*/enumDAY {MON=1,TUE,WED,THU,FRI,SAT,SUN}yesterday=MON,today=TUE,tomorrow=WED;/*定义三个具有基本数据类型的变量,并赋初值。它们都为全局变量*/intx=10,y=20,z=30;voidmain(){printf("%d%d%d ",x,y,z);//输出:102030printf("%d%d%d ",yesterday,today,tomorrow);//输出:123}3.2对枚举型的变量赋整数值时,需要进行类型转换。 #includeenumDAY{MON=1,TUE,WED,THU,FRI,SAT,SUN};voidmain(){enumDAYyesterday,today,tomorrow;yesterday=TUE;today=(enumDAY)(yesterday+1);//类型转换tomorrow=(enumDAY)30;//类型转换//tomorrow=3;//错误printf("%d%d%d ",yesterday,today,tomorrow);//输出:2330}3.3使用枚举型变量#includeenum {BELL='a',BACKSPACE='b',HTAB='t',RETURN='r',NEWLINE=' ',VTAB='v',SPACE=''};enumBOOLEAN{FALSE=0,TRUE}match_flag;voidmain(){intindex=0;intcount_of_letter=0;intcount_of_space=0;charstr[]="I'mElyefod";match_flag=FALSE; for(;str[index]!=' ';index++)if(SPACE!=str[index])count_of_letter++;else{match_flag=(enumBOOLEAN)1;count_of_space++;}printf("%s%dtimes%c",match_flag?"match":"notmatch",count_of_space,NEWLINE);printf("countofletters:%d%c%c",count_of_letter,NEWLINE,RETURN);}输出:match2timescountofletters:10Pressanykeytocontinue4.枚举类型与sizeof运算符#include enumescapes{BELL='a',BACKSPACE='b',HTAB='t',RETURN='r',NEWLINE=' ',VTAB='v',SPACE=''};enumBOOLEAN{FALSE=0,TRUE}match_flag;voidmain(){printf("%dbytes ",sizeof(enumescapes));//4bytesprintf("%dbytes ",sizeof(escapes));//4bytesprintf("%dbytes ",sizeof(enumBOOLEAN));//4bytesprintf("%dbytes ",sizeof(BOOLEAN));//4bytesprintf("%dbytes ",sizeof(match_flag));//4bytes printf("%dbytes ",sizeof(SPACE));//4bytesprintf("%dbytes ",sizeof(NEWLINE));//4bytesprintf("%dbytes ",sizeof(FALSE));//4bytesprintf("%dbytes ",sizeof(0));//4bytes}5.综合举例#includeenumSeason{spring,summer=100,fall=96,winter};typedefenum{Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday}Weekday; voidmain(){/*Season*/printf("%d ",spring);//0printf("%d,%c ",summer,summer);//100,dprintf("%d ",fall+winter);//193SeasonmySeason=winter;if(winter==mySeason)printf("mySeasoniswinter ");//mySeasoniswinterintx=100;if(x==summer)printf("xisequaltosummer ");//xisequaltosummerprintf("%dbytes ",sizeof(spring));//4bytes/*Weekday*/printf("sizeofWeekdayis:%d ",sizeof(Weekday));//sizeofWeekdayis:4Weekdaytoday=Saturday; Weekdaytomorrow;if(today==Monday)tomorrow=Tuesday;elsetomorrow=(Weekday)(today+1);//remembertoconvertfrominttoWeekday}