解决C语言难攻克的枚举
原文链接:
https://www.cnblogs.com/JCSU/articles/1299051.html
定义一个变量是枚举类型,可以先定义一个枚举类型名,然后再说明这个变量是该枚举类型。
例如:
enum weekday{sun,mon,tue,wed,thu,fri,sat};
enum weekday day;
当然,也可以直接定义枚举类型变量。例如:
enum weekday{sun,mon,tue,wed,thu,fri,sat} day;
需要说明的有以下几点:
① 枚举元素不是变量,而是常数,因此枚举元素又称为枚举常量。因为是常量,所以不能对枚举元素进行赋值。
② 枚举元素作为常量,它们是有值的,C 语言在编译时按定义的顺序使它们的值为,1,2,…。
在上面的说明中,sun 的值为 0,mon 的值为 1,…sat 的值为 6,如果有赋值语句
day=mon;
则 day 变量的值为 1。当然,这个变量值是可以输出的。例如:
printf ("%d",day);
将输出整数 1。
如果在定义枚举类型时指定元素的值,也可以改变枚举元素的值。例如:
enum weekday{sun=7,mon=1,tue,wed,thu,fri,sat}day;
这时,sun 为 7,mon 为 1,以后元素顺次加 1,所以 sat 就是 6 了。
③ 枚举值可以用来作判断。例如:
if (day==mon) {…}
if (day>mon) {…}
枚举值的比较规则是:按其在说明时的顺序号比较,如果说明时没有人为指定,则第一个枚举元素的值认作 0。例如,mon>sun,sat>fri。
C 语言教程 ?216?
④ 一个整数不能直接赋给一个枚举变量,必须强制进行类型转换才能赋值。例如:
day=(enum weekday)2;
从键盘输入一个整数,显示与该整数对应的枚举常量的英文名称。
void main( )
{
enum weekday {sun,mon,tue,wed,thu,fri,sat} day;
int k;
printf("input a number(0--6)");
scanf("%d",&k);
day=(enum weekday)k;
switch(day)
{
case sun: printf("sunday/n");break;
case mon: printf("monday/n");break;
case tue: printf("tuesday/n");break;
case wed: printf("wednesday/n");break;
case thu: printf("thursday/n");break;
case fri: printf("friday/n");break;
case sat: printf("satday/n");break;
default: printf("input error/n");break;
}
}
input a number(0--6)1
monday
在该程序中,枚举常量与枚举变量可以进行比较,但要输出枚举常量对应的英文单词,不能使用以下语句:
printf(" %s",mon);
因为枚举常量 mon 为整数值,而非字符串。
在使用枚举变量时,主要关心的不是它的值的大小,而是其表示的状态。
在程序中,可能需要为某些整数定义一个别名,我们可以利用预处理指令#define来完成这项工作,您的代码可能是:
以下代码定义了这种新的数据类型 - 枚举型
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};
新的数据类型定义完成后,它就可以使用了。我们已经见过最基本的数据类型,如:整型int, 单精度浮点型float, 双精度浮点型double, 字符型char, 短整型short等等。用这些基本数据类型声明变量通常是这样:
char a; //变量a的类型均为字符型char
char letter;
int x,
y,
z; //变量x,y和z的类型均为整型int
int number;
double m, n;
double result; //变量result的类型为双精度浮点型double
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};
enum DAY yesterday;
enum DAY today;
enum DAY tomorrow; //变量 tomorrow的类型为枚举型enum DAY
enum DAY good_day, bad_day; //变量good_day和bad_day的类型均为枚举型enum DAY
enum //跟第一个定义不同的是,此处的标号DAY省略,这是允许的。
{
saturday,
sunday = 0,
monday,
tuesday,
wednesday,
thursday,
friday
} workday; //变量workday的类型为枚举型enum DAY
enum week { Mon=1, Tue, Wed, Thu, Fri Sat, Sun} days; //变量days的类型为枚举型enum week
enum BOOLEAN { false, true } end_flag, match_flag; //定义枚举类型并声明了两个枚举型变量
typedef enum workday
{
saturday,
sunday = 0,
monday,
tuesday,
wednesday,
thursday,
friday
} workday; //此处的workday为枚举型enum workday的别名
workday today, tomorrow; //变量today和tomorrow的类型为枚举型workday,也即enum workday
enum workday中的workday可以省略:
typedef enum
{
saturday,
sunday = 0,
monday,
tuesday,
wednesday,
thursday,
friday
} workday; //此处的workday为枚举型enum workday的别名
workday today, tomorrow; //变量today和tomorrow的类型为枚举型workday,也即 enum workday
typedef enum workday
{
saturday,
sunday = 0,
monday,
tuesday,
wednesday,
thursday,
friday
};
workday today, tomorrow; //变量today和tomorrow的类型为枚举型workday,也即 enum workday
typedef enum
{
wednesday,
thursday,
friday
} workday;
typedef enum WEEK
{
saturday,
sunday = 0,
monday,
} workday;
typedef enum
{
wednesday,
thursday,
friday
} workday_1;
typedef enum WEEK
{
wednesday,
sunday = 0,
monday,
} workday_2;
实例将枚举类型的赋值与基本数据类型的赋值进行了对比:
#include<stdio.h>
/* 定义枚举类型 */
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN };
void 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);
}
/* 定义枚举类型 */
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN };
void main()
{
/* 使用基本数据类型声明变量同时对变量赋初值 */
int x=10, y=20, z=30;
/* 使用枚举类型声明变量同时对枚举型变量赋初值 */
enum DAY yesterday = MON,
today = TUE,
tomorrow = WED;
printf("%d %d %d /n", yesterday, today, tomorrow);
}
/* 定义枚举类型,同时声明该类型的三个变量,它们都为全局变量 */
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN } yesterday, today, tomorrow;
/* 定义三个具有基本数据类型的变量,它们都为全局变量 */
int x, y, z;
void 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
}
/* 定义枚举类型,同时声明该类型的三个变量,并赋初值。它们都为全局变量 */
enum DAY
{
MON=1,
TUE,
WED,
THU,
FRI,
SAT,
SUN
}
yesterday = MON, today = TUE, tomorrow = WED;
/* 定义三个具有基本数据类型的变量,并赋初值。它们都为全局变量 */
int x = 10, y = 20, z = 30;
void main()
{
printf("%d %d %d /n", x, y, z); //输出:10 20 30
printf("%d %d %d /n", yesterday, today, tomorrow); //输出:1 2 3
}
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN };
void 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
}
#include<stdio.h>
enum
{
BELL = '/a',
BACKSPACE = '/b',
HTAB = '/t',
RETURN = '/r',
NEWLINE = '/n',
VTAB = '/v',
SPACE = ' '
};
enum BOOLEAN { FALSE = 0, TRUE } match_flag;
void 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
enum escapes
{
BELL = '/a',
BACKSPACE = '/b',
HTAB = '/t',
RETURN = '/r',
NEWLINE = '/n',
VTAB = '/v',
SPACE = ' '
};
enum BOOLEAN { FALSE = 0, TRUE } match_flag;
void main()
{
printf("%d bytes /n", sizeof(enum escapes)); //4 bytes
printf("%d bytes /n", sizeof(escapes)); //4 bytes
printf("%d bytes /n", sizeof(enum BOOLEAN)); //4 bytes
printf("%d bytes /n", sizeof(BOOLEAN)); //4 bytes
printf("%d bytes /n", sizeof(match_flag)); //4 bytes
printf("%d bytes /n", sizeof(SPACE)); //4 bytes
printf("%d bytes /n", sizeof(NEWLINE)); //4 bytes
printf("%d bytes /n", sizeof(FALSE)); //4 bytes
printf("%d bytes /n", sizeof(0)); //4 bytes
}
▍5. 综合举例
enum Season
{
spring, summer=100, fall=96, winter
};
typedef enum
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
Weekday;
void 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
}
喜欢本文的话点一下右下角的在看再走吧~
—END—
往期精选
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
▲
一一一一一一一一一一
嵌入式/Linux/C++/Qt
免责声明:整理文章为传播相关技术,版权归原作者所有,如有侵权,请联系删除