Advertisement
wendyjiang30

Untitled

May 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. public static int evaluate(String expression) {
  2. Stack211<Integer> stack = new Stack211<Integer>();
  3. for (int i = 0; i < expression.length(); i++) {
  4. char c = expression.charAt(i);
  5. if (c >= '0' && c <= '9') {
  6. int num = c - '0';
  7. stack.push(num);
  8. } else {
  9. int val1 = stack.pop();
  10. int val2 = stack.pop();
  11. if (c == '+') {
  12. stack.push(val2 + val1);
  13. } else if (c == '-') {
  14. stack.push(val2 - val1);
  15. } else if (c == '*') {
  16. stack.push(val2 * val1);
  17. } else {
  18. stack.push(val2 / val1);
  19. }
  20.  
  21. }
  22. }
  23. return stack.pop();
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement