LeetCode 计算器一直出错,不知道错误在哪儿,求助
显示的是terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid
class Solution {
public:
int calculate(string s) {
stack<int> intel;
stack<char> op;
int a, b;
char c;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' || s[i] == ' ')
{
op.push(s[i]);
}
else
{
intel.push(s[i] - '0');
}
}
int n = s.size();
while (!op.empty()) {
b = intel.top();
intel.pop();
a = intel.top();
intel.pop();
c = op.top();
op.pop();
if (c == '+') intel.push(a + b);
if (c == '-') intel.push(a - b);
if (c == '*') intel.push(a * b);
if (c == '/') intel.push(a / b);
if (c == ' ') {
op.pop();
}
}
return intel.top();
}
};