C ++ enum types lesson plans

First, enumerated types of application scenarios

  Only need to enumerate the value of variables that need to come out, they constitute an enumerated type.

Second, the definition of the enumeration type

Define ways:

enum enumeration type name} {enum constant table;
· Keyword enum: Description The next definition is an enumeration type
 
· Enumerated type name: specified herein defined enumeration type name.
 
· Enumeration constant table: consists of enumeration constant (or enumeration member). Enumeration constant table lists all possible values of enumerated types, each with between enumeration constants, "" interval, and each enumeration constants must be different.
 
·Application examples:
enum Weekday {the SUN, the MON, the TUE, WEDs, the THU, the FRI, the SAT}; // definition of an enumerated type WEEKDAY 

enum color_set {RED, BLUE, the WHITE, BLACK}; // definition of an enumerated type color_set

* Special Note: enumeration constants can only be expressed in the form of an identifier, not a integer, character and other literals . For example, the following definitions illegal :

enum year_set { 2010 , 2011 , 2012 , , 2013 , , 2014 }; // enumeration constants can not be integer constant 

enum book_set { ' A ' , ' B ' , ' C ' , ' D ' }; // enumerated type is not character constant

Third, the enumeration type Application Notes

1, the enumeration elements have default values. The default starting from 0, followed by 0,1,2,3 ......

E.g:

enum Weekday {the SUN, the MON, the TUE, WEDs, the THU, the FRI, the SAT}; // definition of an enumerated type WEEKDAY
 // from zero by default, i.e. SUN = 0, MON = 1, followed by 0,1,2,3 , 4,5,6, SAT = 6

 

2, you can further define enumeration values ​​of elements in the statement.

enum Weekday = {the SUN . 7 , the MON = 1 , the TUE, WEDs, the THU, the FRI, the SAT};
 // define SUN = 7, MON = 1, then the following values on the basis of the previous denomination incremented by 1, i.e., TUE = 2, WED = 3, ...... SAT = 6

 

3, enumerated types can be relational operators.

 

E.g:

= the SUN DAY1; // DAY1 number of elements in the value of SUM 0
DAY2 MON =; // DAY2 the number of elements in MON value of an
IF (day2> day1) // relationship day2> day1 comparison value is a number> 0

4. enumeration can be directly output, the output value is an integer variable.

enum color_set1 {RED, BLUE, the WHITE, BLACK}; 
color3 = RED;            // enumerated enum constant values are assigned 
COUT << color3;          // output color3 is an integer value, i.e. integer value 1, RED

 

5, the integer value can not be directly assigned to enumeration. For enumeration from integer, should be cast.

      Enumeration (implicit conversion) Plastic

      Plastic (display conversion) enumeration

1) the enumeration data type implicitly converted to an integer data (implicit conversion)

 

int count = MON; // the value of the enumerated type is assigned to data MON integer count data

 

int   A; 
A = the SUN; // Enumeration value assigned to integer variables 
cout << a;

2) Integer enumeration data is converted into data (explicit conversion)

= GameResult Result (COUNT); // display conversion, integer data into brackets enumerated data

 

6, the enumeration of processing elements by a constant, but can not directly impart a constant value thereof.

= SUN 0 ; // SUN is an enumeration element can not be assigned, this statement is illegal

If the integer value assigned enumerations required cast: preceding value plus (type)

= The SUN (Weekday) . 7 ; // earlier value plus (Type) is a common method of cast

 

7, if the assigned enumeration, they must be an integer . Enum is equivalent to a subset of integer variables .

enum WEEKDAY the SUN = { 1.1 , the MON = 1.2 , ......}; // enumeration assign only be an integer, corresponding to a subset of the integer variables, this statement illicit

Fourth, the typical example - use the code

1)声明枚举类型Weekday,包括SUNDAY到SATURDAY七个元素在程序中声明Weekday类型的变量,对其赋值,声明整型变量,看看能否对其赋Weekday类型的值。

#include<iostream>
using namespace std;
enum Weekday {SUN,MON,TUE,WED,TUR,FRI,STA};
int main()
{
    Weekday day;
    enum Weekday today = FRI;
    day = SUN;
    cout << day << endl << today << endl;
    return 0;
}

实验测试:若将整型数据赋给枚举变量,则会出现以下错误。

int main()
{
    Weekday day;
    enum Weekday today = FRI;
    day = 5;//修改为  day =(Weekday)5;
    cout << day << endl << today << endl;
    return 0;
}

2)口袋中有红、黄、蓝、白、黑5种颜色的球若干个。每次从口袋中取出3个不同颜色的球,问有多少种取法。

#include<iostream>
using namespace std;
void transfer(int i)//将相应的数值组合通过switch语句转换为字符串
{
    switch (i)
    {
    case 0:cout << "red "; break;
    case 1:cout << "yello "; break;
    case 2:cout << "blue "; break;
    case 3:cout << "white "; break;
    case 4:cout << "black "; break;
    }
}
enum ball { red, yello, blue, white, black };//定义球的枚举类型
int main()
{
    int i, j, k, count = 0;
    for (int i = red; i <= black; i++)
    {
        for (int j = i + 1; j <= black; j++)
        {
            for (int k = j + 1; k <= black; k++)
            {
                transfer(i);
                transfer(j);
                transfer(k);
                cout << endl;
                count++;
            }
        }
    }
    cout << count;
    return 0;
}

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/nanaa/p/11518733.html