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();
}
};
全部评论

相关推荐

05-01 22:41
中南大学 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务