把简单的事情搞复杂:
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
int divide(int a, int b) {
if (b == 0) {
throw "Division by zero exception!";
}
return a / b;
}
int main() {
int a, b;
char op;
cin >> a >> op >> b;
int result;
try {
if (op == '+') {
result = add(a, b);
} else if (op == '-') {
result = subtract(a, b);
} else if (op == '*') {
result = multiply(a, b);
} else if (op == '/') {
result = divide(a, b);
} else {
throw "Invalid operator!";
}
cout << result << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}