prometheus800

Постфикс нотација АПС

Nov 15th, 2020 (edited)
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.99 KB | None | 0 0
  1. //Постфикс нотација Problem 1 (1 / 29)
  2. //Да се напише алгоритам кој ќе врши евалуација на израз во постфикс нотација.
  3. //На влез се чита низа од знаци за изразот (стринг), а на излез се печати вредноста на изразот по евалуацијата.
  4. //Име на класата (Java): PostFixEvaluation
  5.  
  6. //Sample input: 1 2 +
  7. //Sample output: 3
  8.  
  9. import java.io.BufferedReader;
  10. import java.io.InputStreamReader;
  11. import java.util.NoSuchElementException;
  12.  
  13. interface Stack<E> {
  14.     public boolean isEmpty();
  15.         //Returns true if the stack is empty
  16.  
  17.     public E peek();
  18.         //Returns an element from the top of the stack
  19.  
  20.     public void clear();
  21.         //Clears the whole stack
  22.  
  23.     public void push(E o);
  24.         //Adds an element on the top of the stack
  25.  
  26.     public E pop();
  27.         //Returns and removes the top element of the stack
  28. }
  29. class ArrayStack<E> implements Stack<E> {
  30.     private E[] elements;
  31.     private int stackSize;
  32.  
  33.     public ArrayStack(int stackSize) {
  34.         elements = (E[]) new Object[stackSize];
  35.         stackSize = 0;
  36.     }
  37.  
  38.     @Override
  39.     public boolean isEmpty() {
  40.         return (stackSize == 0);
  41.     }
  42.  
  43.     @Override
  44.     public E peek() {
  45.         if(stackSize == 0)
  46.             throw new NoSuchElementException();
  47.         return elements[stackSize - 1];
  48.     }
  49.  
  50.     @Override
  51.     public void clear() {
  52.         for (int i = 0; i < stackSize; i++) {
  53.             elements[i] = null;
  54.         }
  55.         stackSize = 0;
  56.     }
  57.  
  58.     @Override
  59.     public void push(E o) {
  60.         elements[stackSize++] = o;
  61.     }
  62.  
  63.     @Override
  64.     public E pop() {
  65.         if(stackSize == 0)
  66.             throw new NoSuchElementException();
  67.         E topmost = elements[--stackSize];
  68.         elements[stackSize] = null;
  69.         return topmost;
  70.     }
  71.  
  72.     public int size(){
  73.         return stackSize;
  74.     }
  75. }
  76. public class PostFixEvaluation {
  77.  
  78.     public static void evaluate(String expression){
  79.         ArrayStack<String> arrayStack = new ArrayStack<>(expression.length());
  80.         String[] string = expression.split("\\s+");
  81.         int value1, value2 = 0;
  82.         for (int i = 0; i < string.length; i++) {
  83.             arrayStack.push(string[i]);
  84.             switch (string[i]){
  85.                 case "+" : {
  86.                     arrayStack.pop();
  87.                     value2 = Integer.parseInt(arrayStack.pop());
  88.                     value1 = Integer.parseInt(arrayStack.pop());
  89.                     arrayStack.push(String.valueOf(value1 + value2));
  90.                     break;
  91.                 }
  92.                 case "-" : {
  93.                     arrayStack.pop();
  94.                     value2 = Integer.parseInt(arrayStack.pop());
  95.                     value1 = Integer.parseInt(arrayStack.pop());
  96.                     arrayStack.push(String.valueOf(value1 - value2));
  97.                     break;
  98.                 }
  99.                 case "*" : {
  100.                     arrayStack.pop();
  101.                     value2 = Integer.parseInt(arrayStack.pop());
  102.                     value1 = Integer.parseInt(arrayStack.pop());
  103.                     arrayStack.push(String.valueOf(value1 * value2));
  104.                     break;
  105.                 }
  106.                 case "/" : {
  107.                     arrayStack.pop();
  108.                     value2 = Integer.parseInt(arrayStack.pop());
  109.                     value1 = Integer.parseInt(arrayStack.pop());
  110.                     arrayStack.push(String.valueOf(value1 / value2));
  111.                     break;
  112.                 }
  113.             }
  114.         }
  115.         System.out.println(arrayStack.peek());
  116.     }
  117.  
  118.     public static void main(String[] args) throws Exception{
  119.  
  120.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  121.  
  122.         String expression = br.readLine();
  123.         char exp[] = expression.toCharArray();
  124.  
  125.         br.close();
  126.  
  127.         evaluate(expression);
  128.     }
  129. }
  130.  
Add Comment
Please, Sign In to add comment