列舉型別(轉載只是為了查閱方便,若侵權立刪)
注:以下全部程式碼的執行環境為VC++ 6.0
在程式中,可能需要為某些整數定義一個別名,我們可以利用預處理指令#define來完成這項工作,您的程式碼可能是:
#define MON 1
#define TUE 2
#define WED 3
#define THU 4
#define FRI 5
#define SAT 6
#define SUN 7
在此,我們定義一種新的資料型別,希望它能完成同樣的工作。這種新的資料型別叫列舉型。
1. 定義一種新的資料型別 - 列舉型
以下程式碼定義了這種新的資料型別 - 列舉型
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};
(1) 列舉型是一個集合,集合中的元素(列舉成員)是一些命名的整型常量,元素之間用逗號,隔開。
(2) DAY是一個識別符號,可以看成這個集合的名字,是一個可選項,即是可有可無的項。
(3) 第一個列舉成員的預設值為整型的0,後續列舉成員的值在前一個成員上加1。
(4) 可以人為設定列舉成員的值,從而自定義某個範圍內的整數。
(5) 列舉型是預處理指令#define的替代。
(6) 型別定義以分號;結束。
2. 使用列舉型別對變數進行宣告
新的資料型別定義完成後,它就可以使用了。我們已經見過最基本的資料型別,如:整型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關鍵字將列舉型別定義成別名,並利用該別名進行變數宣告:
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;
3. 使用列舉型別的變數
3.1 對列舉型的變數賦值。
例項將列舉型別的賦值與基本資料型別的賦值進行了對比:
方法一:先宣告變數,再對變數賦值
#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);
}
方法二:宣告變數的同時賦初值
#include <stdio.h>
/* 定義列舉型別 */
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);
}
方法三:定義型別的同時宣告變數,然後對變數賦值。
#include <stdio.h>
/* 定義列舉型別,同時宣告該型別的三個變數,它們都為全域性變數 */
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
}
方法四:型別定義,變數宣告,賦初值同時進行。
#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;
void 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 };
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
}
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;
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
4. 列舉型別與sizeof運算子
#include <stdio.h>
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. 綜合舉例
#include<stdio.h>
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
}