题解 | 加减乘除
#include <bits/stdc++.h> using namespace std; int getFac(int x) { int ans = 1; while (x != 0) { ans *= x; x--; } return ans; } int main() { int a, b; char c; while (cin >> a) { cin >> c; if (c == '!') { cout << getFac(a)<<endl; } else { cin >> b; if (c == '+')cout << a + b << endl; else if (c == '-')cout << a - b << endl; else if (c == '*')cout << a* b << endl; else if (c == '/') { if (b == 0)cout << "error" << endl; else cout << a / b << endl; } else if (c == '%') { if (b == 0)cout << "error" << endl; else cout << a % b << endl; } } } }
不等长获取,加上个条件判断即可