求做简单计算器。用switch语句编写一个程序计算表达式:data1 op data2的值。其中op为运算符+、-、*、⼀。

2024-12-19 13:10:59
推荐回答(2个)
回答1:

#include main { int data1,data2,a; char op; switch(op) case"_" a = data1 - data2; break; case"+" a = data1 + data2; break; case"*" a = data1* data2; break; case"/" a = data1 / data2; break; }

回答2:

int result = 0; switch (op) { case "+": result = data1 + data2; break; case "-": result = data1 - data2; break; case "*": result = data1 * data2; break; case "/": result = data1 / data2; break; }