Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int evaluate(String expression) {
- Stack211<Integer> stack = new Stack211<Integer>();
- for (int i = 0; i < expression.length(); i++) {
- char c = expression.charAt(i);
- if (c >= '0' && c <= '9') {
- int num = c - '0';
- stack.push(num);
- } else {
- int val1 = stack.pop();
- int val2 = stack.pop();
- if (c == '+') {
- stack.push(val2 + val1);
- } else if (c == '-') {
- stack.push(val2 - val1);
- } else if (c == '*') {
- stack.push(val2 * val1);
- } else {
- stack.push(val2 / val1);
- }
- }
- }
- return stack.pop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement