Guest User

Untitled

a guest
Jul 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package FOCS;
  6.  
  7. /**
  8. *
  9. * @author jxe953
  10. */
  11. class IntStack // Alternative implementation 2.
  12. {
  13.  
  14. private int[] stack;
  15. private int sp;
  16.  
  17. public IntStack(int maximumCapacity) {
  18. // A stack with zero maximum capacity is allowed.
  19. // But the maximum capacity cannot be negative.
  20. assert (maximumCapacity >= 0);
  21. // Allocate an array for the stack.
  22. stack = new int[maximumCapacity];
  23. // Initialize stack pointer.
  24. sp = stack.length - 1;
  25. }
  26.  
  27. public void push(int element) {
  28. // We refuse to overflow the stack.
  29. assert (sp > 0);
  30. stack[sp] = element;
  31. sp = sp - 1;
  32. }
  33.  
  34. public int pop() {
  35. // We refuse to pop from an empty stack.
  36. assert (sp + 1 < stack.length);
  37. sp = sp + 1;
  38. return (stack[sp]);
  39. }
  40.  
  41. public boolean isEmpty() {
  42. return (sp == stack.length - 1);
  43. }
  44.  
  45. public boolean isFull() {
  46. return (sp == 0);
  47. }
  48.  
  49. public static void main(String[] args) {
  50. IntStack s = new IntStack(25);
  51. int[] rpn = {6, 5, -1, 4, -2, 3, -1, 2, -2, 1, -1};
  52. int x = 0;
  53. int y = 0;
  54. int i;
  55. //System.out.println(rpn[0]);
  56. for (i = 0; i < rpn.length; i++) {
  57. System.out.println(rpn [i]);
  58. if (rpn[i] >= 0) {
  59. // System.out.println(rpn[i]);
  60. s.push(rpn[i]);
  61.  
  62. } else if (rpn[i] == -1) {
  63. while (s.isEmpty() == false) {
  64. x = s.pop() + x;
  65. //System.out.println(x);
  66. s.push(x);
  67. }
  68. } else if (rpn[i] == -2) {
  69. while (s.isEmpty() == false) {
  70. x = s.pop() * x;
  71. System.out.println(x);
  72. s.push(x);
  73.  
  74. }
  75. }
  76.  
  77.  
  78. }
  79. System.out.println(x);
  80. }
  81. }
Add Comment
Please, Sign In to add comment