我在处理这段代码时遇到了一些问题。我对 switch 语句和枚举类型很陌生,所以可能有点过度扩展。我设法输入 switch 语句,但它一直返回第一个案例。任何想法为什么?
#include <stdio.h>
#include <string.h>
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;
}
}
我还没有编写我需要的其余开关盒。非常感谢解决此问题的任何帮助!