我在这段代码中遇到了一些麻烦。我对交换语句和枚举类型很陌生,所以可能会过度。我设法做到这一点,以输入switch语句,但它不断返回第一个案例。任何想法为什么?
#include
#include
enum express {ADD, SUB, AND, OR, XOR, SHL, SHR};
express m_express;
express switchint(char *str);
int main(){
unsigned int n1=0x00;
unsigned int n2=0x00;
char action[5];
printf("Enter an expression: ");
scanf("%x, %s, %x", &n1, action, &n2);
m_express=switchint(action);
unsigned int result;
switch(m_express){
case ADD:
printf("add works");
break;
case SUB:
printf("SUB works");
break;
default:
printf("Default");
break;
}
}
express switchint(char *str){
if( strcmp(str, "add")){
return ADD;
}
else if ( strcmp(str, "sub")){
return SUB;
}
else if ( strcmp(str, "and")){
return AND;
}
else if ( strcmp(str, "or")){
return OR;
}
else if ( strcmp(str, "xor")){
return XOR;
}
else if ( strcmp(str, "shl")){
return SHL;
}
else {
return SHR;
}
}我还没有写下我需要的其他开关箱。任何帮助解决这个问题,非常感谢!