Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. public static void main(String[] args)
  2. {
  3. Scanner in = new Scanner(System.in);
  4.  
  5. Stack<Integer> value = new Stack<>();
  6.  
  7. System.out.println("Enter one number or operator per line, E to quit. ");
  8. System.out.println("Current Value: " + value);
  9.  
  10. boolean flag = false;
  11.  
  12. while (!flag)
  13. {
  14.  
  15. String input = in.nextLine();
  16. // command is an operator, pop the arguments and push the result
  17. if (input.equals("+"))
  18. {
  19.  
  20.  
  21. value.push(value.pop() + value.pop());
  22. }
  23. else if (input.equals("-"))
  24. {
  25. Integer argument2 = value.pop();
  26. Integer argument3 = value.pop();
  27.  
  28. value.push(argument3 - argument2);
  29. }
  30. else if (input.equals("*"))
  31. {
  32. value.push(value.pop() * value.pop());
  33. }
  34. else if (input.equals("/"))
  35. {
  36. Integer argument2 = value.pop();
  37. value.push(value.pop() / argument2);
  38. }
  39. else if (input.equals("E") || input.equals("e"))
  40. {
  41. flag = true;
  42. }
  43. else
  44. {
  45. // Is not operator push to stack
  46. value.push(Integer.parseInt(input));
  47. }
  48. System.out.println("Current value: "+value);
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement