【将来虫教育】c语言中enum类型的用法!
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="//q0.itc.cn/images01/20240815/bed70a9b6fc148729d61c28affbc2846.png" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1、</span>枚举类型</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在<span style="color: black;">实质</span>问题中,有些变量的取值被限定在一个有限的范围内。例如,一个星期内<span style="color: black;">仅有</span>七天,一年<span style="color: black;">仅有</span>十二个月,一个班每周有六门课程等等。<span style="color: black;">倘若</span>把这些量说明为整型,字符型或其它类型显然是不<span style="color: black;">稳妥</span>的。为此,C语言<span style="color: black;">供给</span>了一种<span style="color: black;">叫作</span>为“枚举”的类型。在“枚举”类型的定义中列举出所有可能的取值,被说明为该“枚举”类型的变量取值<span style="color: black;">不可</span>超过定义的范围。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">应该说明的是,枚举类型是一种基本数据类型,而不是一种构造类型,<span style="color: black;">由于</span>它<span style="color: black;">不可</span>再分解为任何基本类型。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">枚举类型的定义和枚举变量的说明</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. 枚举的定义</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">枚举类型定义的<span style="color: black;">通常</span>形式为:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">enum 枚举名{ 枚举值表 };</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在枚举值表中应<span style="color: black;">陈列</span>出所有可用值。这些值<span style="color: black;">亦</span><span style="color: black;">叫作</span>为枚举元素。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例如:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">该枚举名为weekday,枚举值共有7个,即<span style="color: black;">1星期</span>中的七天。凡被说明为weekday类型变量的取值只能是七天中的某一天。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. 枚举变量的说明</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">如同结构和联合<span style="color: black;">同样</span>,枚举变量<span style="color: black;">亦</span>可用<span style="color: black;">区别</span>的方式说明,即先定义后说明,<span style="color: black;">同期</span>定义说明或直接说明。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">设有变量a,b,c被说明为<span style="color: black;">以上</span>的weekday,可采用下述任一种方式:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">enum weekday{ sun,mou,tue,wed,thu,fri,sat };</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">
<a style="color: black;">
展开全文
</a>
</div>
<div style="color: black; text-align: left; margin-bottom: 10px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">enum weekday a,b,c;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">或</span>为: enum weekday{ sun,mou,tue,wed,thu,fri,sat }a,b,c;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">或</span>为: enum { sun,mou,tue,wed,thu,fri,sat }a,b,c;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2、</span>枚举类型变量的赋值和<span style="color: black;">运用</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">枚举类型在<span style="color: black;">运用</span>中有以下规定:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. 枚举值是常量,不是变量。<span style="color: black;">不可</span>在程序中用赋值语句再对它赋值。 例如对枚举weekday的元素再作以下赋值: sun=5; mon=2; sun=mon; 都是错误的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. 枚举元素本身由系统定义了一个<span style="color: black;">暗示</span>序号的数值,从0<span style="color: black;">起始</span><span style="color: black;">次序</span>定义为0,1,2…。如在weekday中,sun值为0,mon值为1,…,sat值为6。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【例】</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">main(){enum weekday</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{ sun,mon,tue,wed,thu,fri,sat } a,b,c;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a=sun;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">b=mon;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">c=tue;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">printf("%d,%d,%d",a,b,c);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">说明: 只能把枚举值赋予枚举变量,<span style="color: black;">不可</span>把元素的数值直接赋予枚举变量。如:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a=sum;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">b=mon;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">是正确的。而:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a=0;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">b=1;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">是错误的。如<span style="color: black;">必定</span>要把数值赋予枚举变量,则必须用强制类型转换。 如:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a=(enum weekday)2;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其<span style="color: black;">道理</span>是将<span style="color: black;">次序</span>号为2的枚举元素赋予枚举变量a,相当于:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">a=tue; 还应该说明的是枚举元素不是字符常量<span style="color: black;">亦</span>不是字符串常量,<span style="color: black;">运用</span>时不要加单、双引号。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【例】</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">main(){enum body</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{ a,b,c,d } month,j;int i;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">j=a;for(i=1;i<=30;i++){</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">month=j;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">j++;if(j>d) j=a;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}for(i=1;i<=30;i++){switch(month)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{casea:printf(" %2d %c\t",i,a);break;caseb:printf(" %2d %c\t",i,b);break;casec:printf(" %2d %c\t",i,c);break;cased:printf(" %2d %c\t",i,d);break;default:break;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">printf("\n");</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}<a style="color: black;"><span style="color: black;">返回<span style="color: black;">外链论坛: http://www.fok120.com</span>,查看<span style="color: black;">更加多</span></span></a></p>
</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">责任编辑:网友投稿</span></p>
这夸赞甜到心里,让我感觉温暖无比。
页:
[1]