C/C++ language can use # define and const to create symbolic constants, while enum tools can not only create symbolic constants, but also define new data types.
For example, there are the following definitions
enum enumType {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
This sentence has two functions:
First: declare enumType as a new data type, called enumeration;
Second: declare Monday, Tuesday, etc. as symbolic constants, commonly known as enumerations, whose default values are 0-6. (How to explicitly initialize the value of an enumerator is described later)
Enumeration elements are constants and cannot be assigned to them. They cannot be written as assignment expressions: Sunday = 0
Enumeration elements have default values, which are in turn 0, 1, 2,....
For enumeration, only assignment operators are defined, and no arithmetic operations are defined for enumeration.
The enumeration type value method is (enumeration type variables) the default value of the enumeration element, such as: (color_set)j.
A switch statement is different from an if statement in that it can only test for equality.
Therefore, case statements can only be followed by integer or character type constants or constant expressions.
It can also test relationships and logical expressions in if statements.
In switch statements, don't forget to add a break statement at the end of each case statement
In switch statements, default statements are mainly used to check defaults or to handle errors.
#include <iostream> #include<iomanip> using namespace std; int main(){ enum color_set {red,yellow,blue,white,black}; //Declare enumeration type color color_set color;//Define a variable color of enumeration type int i,j,k,counter=0,loop; //counter is the sum of combinations of different colors. for(i=red;i<=black;i++) { for(j=red;j<=black;j++) { if(i!=j) { //The first two balls have different colors for(k=red;k<=black;k++) if(k!=i&&k!=j) { //The third ball is different from the first two and meets the requirements. counter++; if((counter)%22==0) { //22 rows per screen cout<<"Please press Enter to continue."; cin.get(); } cout<<setw(15)<<counter; /*Next output three colors for each selection, one action, one selection.*/ for(loop=1;loop<=3;loop++) { switch(loop) { case 1: color=(color_set)i; break; //The first is i. case 2: color=(color_set)j; break; //The second is j. case 3: color=(color_set)k; break; //The third one is k. } switch(color) { case red: cout<<setw(15)<<"red"; break; case yellow:cout<<setw(15)<<"yellow";break; case blue: cout<<setw(15)<<"blue"; break; case white: cout<<setw(15)<<"white"; break; case black: cout<<setw(15)<<"black"; break; } } cout<<endl; //Output a fetch followed by line break } } } } cout<<"Share:"<<counter<<"Seed selection method"<<endl; return 0; }