将变量与if语句中的值列表进行比较的更易读的方法是什么?

Ema*_*uoy -1 c variables if-statement

如何将变量与if语句中相同类型的值列表进行比较并使其可读且干净?

例如,我有以下内容,但有很多值,我想让它更具可读性.

if ((lst->type == 'p' || lst->type == 'x' || lst->type == 'X' ||
    lst->type == 'o' || lst->type == 'O' || (lst->type == 'd' ||
    lst->type == 'i' || lst->type == 'D'))
Run Code Online (Sandbox Code Playgroud)

t0m*_*13b 5

使用该strchr功能处理此问题的最简单方法:

const char *lookups = "pxXoOdiD";
if (strchr(lookups, lst->type)){ 
    // Do your thing here to handle the condition
}
Run Code Online (Sandbox Code Playgroud)

有关strchr的更多信息,请参阅.

返回指向C字符串str中第一个字符出现的指针.