c++ 枚举怎么用switch语句表达??

2024-11-17 03:37:28
推荐回答(4个)
回答1:

其实一个switch就好了(上面代码)~ 如果要用两个的话可以像下面那样:

#include 
using namespace std;
enum etype
{
lab, sec, man, acc, exe, res
};
int main()
{
char temp;
int temp_Int;
cout << "Enter type:" << endl;
cin >> temp;
cout << "The type is:" << endl;
switch(temp)
{
case 'l': cout << "lab" << endl; break;
case 's': cout << "sec" << endl; break;
case 'm': cout << "man" << endl; break;
case 'a': cout << "acc" << endl; break;
case 'e': cout << "exe" << endl; break;
case 'r': cout << "res" << endl; break;
}
}
#include
using namespace std;
enum etype
{
lab, sec, man, acc, exe, res
};
int main()
{
char temp;
int temp_Int;
cout << "Enter type:" << endl;
cin >> temp;
cout << "The type is:" << endl;
switch(temp)
{
case 'l': temp_Int = lab; break;
case 's': temp_Int = sec; break;
case 'm': temp_Int = man; break;
case 'a': temp_Int = acc; break;
case 'e': temp_Int = exe; break;
case 'r': temp_Int = res; break;
}
switch(temp_Int)
{
case lab: cout << "lab" << endl; break;
case sec: cout << "sec" << endl; break;
case man: cout << "man" << endl; break;
case acc: cout << "acc" << endl; break;
case exe: cout << "exe" << endl; break;
case res: cout << "res" << endl; break;
}
}

回答2:

#include
using namespace std;
enum etype {lab,sec,man,acc,exe,res};
void main()
{
char c;
int s;
cout<<"Enter type:";
cin>>c;
switch(c)
{
case 'l':s=0;break;
case 's':s=1;break;
case 'm':s=2;break;
case 'a':s=3;break;
case 'e':s=4;break;
case 'r':s=5;break;
}
cout<<"the type is:";
switch(s)
{
case 0:cout<<"lab"< case 1:cout<<"sec"< case 2:cout<<"man"< case 3:cout<<"acc"< case 4:cout<<"exe"< case 5:cout<<"res"< }
}

回答3:

一个就足矣。它的原型如下switch(value){case value1:statements;case value 2:statements;… …default:statements;}

回答4:

#include
using namespace std;

int main(int argc, char **argv)
{
enum Type{ lab = 1, sec, man, acc, exe, res };
char* type_name[] = {"no such type!", "lab", "sec", "man", "acc", "exe", "res" };

char ch;
cin>>ch;

Type t;
switch(ch)
{
case 'l': t = lab; break;
case 's': t = sec; break;
case 'm': t = man; break;
case 'a': t = acc; break;
case 'e': t = exe; break;
case 'r': t = res; break;
default : break;
}

cout<
return EXIT_SUCCESS;
}