题解:力扣
class Solution {
public:
int calculate(string s) {
int sum = 0;
if(s.size() == 0) {
return sum;
}
vector<int> stk;
int num = 0;
char pre_ops = '+';
int val = 0;
for(int i = 0; i < s.size(); ++i) {
if(isdigit(s[i])) {
num = num*10 + (s[i]-'0');
}
// 如果当前是+,-,*,/
// 或者是字符串末尾,把最后一组数字压入栈里
if(!isdigit(s[i]) && s[i] != ' ' || i >= s.size()-1) {
//判断前一个操作符
switch(pre_ops) {
case '+':
stk.push_back(num);
break;
case '-':
stk.push_back(-num);
break;
case '*':
stk.back() *= num;
break;
case '/':
stk.back() /=num;
break;
}
num = 0;
pre_ops = s[i];
}
}
num = accumulate(stk.begin(), stk.end(), 0);
return num;
}
};
1.切割表达式
2.遍历表达式
2.1如果表达式为加减乘除则直接压入栈中
2.2如果当前栈顶为乘除,则弹出两个元素,一个为数字,一个为操作符号。与当前数字计算乘除,将结果押入栈中
3.最后栈中的元素都为,数字和加减号,从头往后计算即可
class Solution {
public:
/**
* @param s: the given expression
* @return: the result of expression
*/
int calculate(string &s) {
// Write your code here
if (s.size() <= 0) {
return 0;
}
// 切割表达式
std::deque<std::string> expressions = get_expression(s);
std::deque<std::string> sta;
std::unordered_set<std::string> table{"+", "-", "*", "/"};
std::unordered_set<std::string> table1{"*", "/"};
for (auto& expression : expressions) {
// 如果为+-*/直接压入栈
if (table.find(expression) != table.end()) {
sta.push_back(expression);
} else if (!sta.empty() && table1.find(sta.back()) != table1.end()) {
// 当前栈为非空,且栈顶为*或者/
// 弹出操作符
std::string op = sta.back();
sta.pop_back();
// 弹出操作数
int a = atoi(sta.back().c_str());
sta.pop_back();
int b = atoi(expression.c_str());
// 计算 a*b 或者 a/b
int val = calc(a, op, b);
// 将计算结果压入栈
sta.push_back(std::to_string(val));
} else {
// 其余情况
sta.push_back(expression);
}
}
int sum = atoi(sta[0].c_str());
// 此时的sta是内容都是数字或者是+-符号
for (int i = 1; i < sta.size(); i += 2) {
sum = calc(sum, sta[i], atoi(sta[i+1].c_str()));
}
return sum;
}
private:
int calc(int a, const std::string& op, int b) {
if (op == std::string("+")) {
return a + b;
} else if (op == std::string("-")) {
return a - b;
} else if (op == std::string("*")) {
return a * b;
} else if (op == std::string("/")) {
return a / b;
}
}
std::deque<std::string> get_val(std::stack<std::string>& sta) {
std::deque<std::string> result;
while (!sta.empty() && sta.top() != std::string("(")) {
result.push_front(sta.top());
sta.pop();
}
sta.pop();
return result;
}
std::deque<std::string> get_expression(const std::string& s) {
if (s.size() <= 0) {
return {};
}
std::deque<std::string> result;
std::unordered_set<char> table{'+', '-', '*', '/'};
bool is_null = true;
int val = 0;
for (auto& ch : s) {
if (ch == ' ') {
continue;
}
if (table.find(ch) != table.end()) {
if (!is_null) {
result.push_back(std::to_string(val));
}
is_null = true;
val = 0;
std::string tmp;
tmp.push_back(ch);
result.push_back(tmp);
} else {
if (is_null) {
is_null = false;
val = 0;
}
val = val * 10 + (ch -'0');
}
}
if (!is_null) {
result.push_back(to_string(val));
}
return result;
}
};