将用户输入作为运算符导入表达式

JJR*_*thm 0 c parsing expression input

是否可以将用户输入作为变量用于表达?

scanf("%s", op); //User enters "==" or "!="

if(x op y)
   //Go.
Run Code Online (Sandbox Code Playgroud)

Jon*_*Jon 6

不,你能做的最好的事情是这样的:

scanf("%s", &op);
if (strcmp(op, "==") == 0) {
    result = x == y;
}
else if (strcmp(op, "!=") == 0) {
    result = x != y;
}

// now use result
Run Code Online (Sandbox Code Playgroud)