#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
char intt;
cin >> a >> b >> intt;
switch (intt) {
case '+':
cout << a + b;
break;
case '-':
cout << a - b;
break;
case '*':
cout << a * b;
break;
case '/':
if (b == 0)
cout << "Divided by zero!";
else
cout << a / b;
break;
case '%':
if (b == 0)
cout << "Divided by zero!";
else
cout << a % b;
break;
default:
cout << "Invalid operator!";
}
return 0;
}
共 2 条回复
#include #include #include #include #include
int getPriority(char op) { if (op == '+' || op == '-') return 1; if (op == '*' || op == '/') return 2; return 0; }
double executeOperation(double operand1, double operand2, char op) { switch (op) { case '+': return operand1 + operand2; case '-': return operand1 - operand2; case '*': return operand1 * operand2; case '/': if (operand2 == 0) { throw std::runtime_error("除零错误"); } return operand1 / operand2; default: return 0.0; } } double calculateExpression(const std::string& expression) { std::stack operands; std::stack operators; std::istringstream iss(expression); char token;
}
int main() { std::string expression; std::cout << "请输入一个表达式(支持四则运算和括号): "; std::getline(std::cin, expression);
}
我也有计算器