Advertisement
BorjanCrvenkov

Aips lab4 Постфикс нотација

Nov 12th, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.NoSuchElementException;
  4.  
  5. class SLLNode<E> {
  6. protected E element;
  7. protected SLLNode<E> succ;
  8.  
  9. public SLLNode(E elem, SLLNode<E> succ) {
  10. this.element = elem;
  11. this.succ = succ;
  12. }
  13.  
  14. @Override
  15. public String toString() {
  16. return element.toString();
  17. }
  18. }
  19. interface Stack<E> {
  20.  
  21. public boolean isEmpty ();
  22. public E peek ();
  23.  
  24. public void clear ();
  25.  
  26. public void push (E x);
  27.  
  28. public E pop ();
  29. }
  30. class ArrayStack<E> implements Stack<E> {
  31.  
  32. private E[] elems;
  33. private int depth;
  34.  
  35. @SuppressWarnings("unchecked")
  36. public ArrayStack (int maxDepth) {
  37. // Konstrukcija na nov, prazen stek.
  38. elems = (E[]) new Object[maxDepth];
  39. depth = 0;
  40. }
  41.  
  42.  
  43. public boolean isEmpty () {
  44. // Vrakja true ako i samo ako stekot e prazen.
  45. return (depth == 0);
  46. }
  47.  
  48.  
  49. public E peek () {
  50. // Go vrakja elementot na vrvot od stekot.
  51. if (depth == 0)
  52. throw new NoSuchElementException();
  53. return elems[depth-1];
  54. }
  55.  
  56.  
  57. public void clear () {
  58. // Go prazni stekot.
  59. for (int i = 0; i < depth; i++) elems[i] = null;
  60. depth = 0;
  61. }
  62.  
  63.  
  64. public void push (E x) {
  65. // Go dodava x na vrvot na stekot.
  66. elems[depth++] = x;
  67. }
  68.  
  69.  
  70. public E pop () {
  71. // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  72. if (depth == 0)
  73. throw new NoSuchElementException();
  74. E topmost = elems[--depth];
  75. elems[depth] = null;
  76. return topmost;
  77. }
  78. }
  79.  
  80.  
  81. public class PostFixEvaluation {
  82.  
  83.  
  84. public static boolean isNumber(String a) {
  85. for(int i=0; i<a.length(); i++) {
  86. if(Character.isDigit(a.charAt(i))) {
  87. continue;
  88. } else {
  89. return false;
  90. }
  91. }
  92. return true;
  93. }
  94. public static int calculate(char[]exp) {
  95. ArrayStack<Integer> stack = new ArrayStack<>(exp.length);
  96. String izraz=new String();
  97. for(int i=0; i<exp.length; i++) {
  98. izraz+=exp[i];
  99. }
  100. String []parts=izraz.split("\\s");
  101. int rezultat=0;
  102.  
  103. for(int i=0; i<parts.length; i++) {
  104. if (isNumber(parts[i])) {
  105. stack.push(Integer.parseInt(parts[i]));
  106. } else {
  107. int pop1= stack.pop();
  108. int pop2=stack.pop();
  109. char operator = parts[i].charAt(0);
  110. if(operator == '+') {
  111. rezultat = pop2 + pop1;
  112. } else if(operator=='-') {
  113. rezultat=pop2-pop1;
  114. } else if(operator=='*') {
  115. rezultat=pop2*pop1;
  116. } else {
  117. rezultat=pop2/pop1;
  118. }
  119. if(i== parts.length-1) {
  120. if (stack.isEmpty()) {
  121. return rezultat;
  122. } else {
  123. return 0;
  124. }
  125. } else {
  126. stack.push(rezultat);
  127. }
  128. }
  129. }
  130. return -1;
  131. }
  132.  
  133.  
  134. public static void main(String[] args) throws Exception {
  135.  
  136. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  137.  
  138. String expression = br.readLine();
  139. char exp[] = expression.toCharArray();
  140.  
  141. System.out.print(calculate(exp));
  142.  
  143.  
  144.  
  145. br.close();
  146.  
  147. }
  148.  
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement