Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
The expression string contains only non-negative integers, +, -, *, / operators , open ( and closing parentheses ) and empty spaces . The integer division should truncate toward zero.
You may assume that the given expression is always valid. All intermediate results will be in the range of [-2147483648, 2147483647].
Some examples:
"1 + 1" = 2
" 6-4 / 2 " = 4
"2*(5+5*2)/3+(6/2+8)" = 21
"(2+6* 3+5- (3*14/7+2)*5)+3"=-12
【C++】
class Solution {
public:
int calculate(string s) {
if (s == nullptr || s.length() == 0) {return 0;}
char sign = '+';
int num = 0, res = 0, len = s.length();
stack<int> stack;
for (int i = 0; i < len; i++) {
char c = s[i];
if (isdigit(c)) {num = num * 10 + c - '0';}
if (!isdigit(c) && c != ' ' || i == len - 1) {
if (c == '(') {
int j = i, int count = 0;
for (; i < len; i++) {
if (s[i] == '(') {count++;}
if (s[i] == ')') {count--;}
if (count == 0) {break;}
}
num = calculate(s.substr(j + 1, i - j - 1));
}
switch (sign) {
case '+': stack.push(num);
case '-': stack.push(-1 * num);
case '*': int a = stack.top(); stack.pop(); stack.push(a * cal);
case '/': int a = stack.top(); stack.pop(); stack.push(a / cal);;
}
num = 0;
sign = c; //before next number
}
}
while (!ns.empty()) {
res += stack.top();
stack.pop();
}
return res;
}
}
【Java】
import java.util.*
class Solution {
public int calculate(String s) {
if(s == null || s.length() == 0) {return 0;}
char sign = '+';
int num = 0;
int res = 0;
Stack<Integer> stack = new Stack<>();
int len = s.length();
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
if (Character.isDigit(c)) {num = num * 10 + c - '0';}
if (!Character.isDigit(c) && c != ' ' || i == len - 1) {
if (c == '(') {
int j = i, int count = 0;
for (; i < len; i++) {
if (s.charAt(i) == '(') {count++;}
if (s.charAt(i) == ')') {count--;}
if (count == 0) {break;}
}
num = calculate(s.substring(j + 1, i));
}
switch (sign) {
case '+': stack.push(num);
case '-': stack.push(-num);
case '*': stack.push(stack.pop() * num);
case '/': stack.push(stack.pop() / num);
}
num = 0;
sign = c; //before next number
}
}
for (int val: stack) {res += val;}
return res;
}
}
another
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
public class Solution {
/**
* @param s: the given expression
* @return: the result of expression
*/
public int calculate(String s) {
// Write your code here
Map<Character, Integer> prio = new HashMap<>();
prio.put('(', 1);
prio.put('+', 2);
prio.put('-', 2);
prio.put('*', 3);
prio.put('/', 3);
Deque<Character> ops = new ArrayDeque<>();
Deque<Long> stack = new ArrayDeque<>();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch == ' ') {continue;}
if (ch == '(') {
ops.push('(');
} else if (ch == ')') {
while (ops.peek() != '(') {calc(stack, ops);}
ops.pop();
} else if (Character.isDigit(ch)) {
int j = i;
while (j < s.length() && Character.isDigit(s.charAt(j))) {
j++;
}
long num = Long.parseLong(s.substring(i, j));
stack.push(num);
i = j - 1;
} else {
while (!ops.isEmpty() && prio.get(ops.peek()) >= prio.get(ch)) {
calc(stack, ops);
}
ops.push(ch);
}
}
while (stack.size() > 1) {calc(stack, ops);}
long res = stack.peek();
return (int) res;
}
private void calc(Deque<Long> stack, Deque<Character> ops) {
long n2 = stack.pop(), n1 = stack.pop();
switch (ops.pop()) {
case '+': stack.push(n1 + n2); break;
case '-': stack.push(n1 - n2); break;
case '*': stack.push(n1 * n2); break;
case '/': stack.push(n1 / n2); break;
}
}
}