Char to Operator C++

wzs*_*sun 6 c++ operators char

嘿,我想知道你怎么能把一个角色'+'变成一个算子.例如,如果我有

char op = '+'
cout << 6 op 1;
Run Code Online (Sandbox Code Playgroud)

谢谢.

joh*_*ohn 7

简单的方法是使用switch语句

switch (op)
{
case '+':
  res = x + y;
  break;
case '-':
  res = x - y;
  break;
case '*':
  res = x * y;
  break;
}
Run Code Online (Sandbox Code Playgroud)