void fun(const char* cmd){
typedef struct cmdR{
const char* name;
int32 nchar;
int32 cmdId;
} cmdR;
enum cmdRID{
SC_AD=0,
SC_NI,
SC_WK,
SC_WL
};
static cmdR cmdList[]={
{"A",0,SC_AD},
{"B",0,SC_NI}.
{"C",0,SC_WK},
{"D",0,SC_WL}
};
static int32 size=0;
size=sizeof(cmdList)/sizeof(cmdR);
for(int32 pos=0;pos<size;pos++){
bool cmp=strncmp(cmd,cmdList[pos].name,cmdList[pos].nchar);
if(cmp==0){ssss=cmdList[pos].cmdId;}
if(cmp<0){...}
if(cmp>0){...}
}
switch((enum cmdRID)){
case SC_AD:
..... break;
}
}
作用,在这个函数里面对形参进行了匹配分流,不同的cmd调用不同的函数
其中if(cmp=0,>0,<0)这个地方在匹配cmdList时候可以用二分法的思想。
int first,last,pos,cmp;
last=size-1;
first=0;
pos=(first+last)/2;
while(fist<=last){
cmp=strncmp(cmd,cmdList[pos].name,cmdList[pos].nchar);
if(cmp==0) {.... break;}
if(cmp<0) last=pos-1;
else first=pos+1;
pos=(first+last)/2;
}
enum作为成员变量:
enum week {Mon, Tue, Wed};
class date{
public:
week weekend;
}
class date{
public:
enum week {Mon, Tue, Wed};
week weekend;
};
void fun(){
date::week other = date::Tue;
}