DamSi

Untitled

Nov 6th, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.NoSuchElementException;
  4. import java.io.IOException;
  5. interface Stack<E> {
  6.  
  7.     // Elementi na stekot se objekti od proizvolen tip.
  8.  
  9.     // Metodi za pristap:
  10.  
  11.     public boolean isEmpty ();
  12.         // Vrakja true ako i samo ako stekot e prazen.
  13.  
  14.     public E peek ();
  15.         // Go vrakja elementot na vrvot od stekot.
  16.  
  17.     // Metodi za transformacija:
  18.  
  19.     public void clear ();
  20.         // Go prazni stekot.
  21.  
  22.     public void push (E x);
  23.         // Go dodava x na vrvot na stekot.
  24.  
  25.     public E pop ();
  26.         // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  27. }
  28. class ArrayStack<E> implements Stack<E> {
  29.  
  30.     // Stekot e pretstaven na sledniot nacin:
  31.     //depth e dlabochinata na stekot, a
  32.     // elems[0...depth-1] se negovite elementi.
  33.     private E[] elems;
  34.     private int depth;
  35.  
  36.     @SuppressWarnings("unchecked")
  37.     public ArrayStack (int maxDepth) {
  38.         // Konstrukcija na nov, prazen stek.
  39.         elems = (E[]) new Object[maxDepth];
  40.         depth = 0;
  41.     }
  42.  
  43.  
  44.     public boolean isEmpty () {
  45.         // Vrakja true ako i samo ako stekot e prazen.
  46.         return (depth == 0);
  47.     }
  48.  
  49.  
  50.     public E peek () {
  51.         // Go vrakja elementot na vrvot od stekot.
  52.         if (depth == 0)
  53.             throw new NoSuchElementException();
  54.         return elems[depth-1];
  55.     }
  56.  
  57.  
  58.     public void clear () {
  59.         // Go prazni stekot.
  60.         for (int i = 0; i < depth; i++)  elems[i] = null;
  61.         depth = 0;
  62.     }
  63.  
  64.  
  65.     public void push (E x) {
  66.         // Go dodava x na vrvot na stekot.
  67.         elems[depth++] = x;
  68.     }
  69.  
  70.  
  71.     public E pop () {
  72.         // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  73.         if (depth == 0)
  74.             throw new NoSuchElementException();
  75.         E topmost = elems[--depth];
  76.         elems[depth] = null;
  77.         return topmost;
  78.     }
  79. }
  80. public class PostFixEvaluation {
  81.  
  82.     /**
  83.      * @param args the command line arguments
  84.      */
  85.     public static void main(String[] args) throws IOException {
  86.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  87.  
  88.         String expression = br.readLine();
  89.         String exp[] = expression.split(" ");
  90.         int rezultat = postFixEvaluation(exp);
  91.         System.out.println(rezultat);
  92.         br.close();
  93.     }
  94.  
  95.     static boolean isOperator(String operator) {
  96.         switch (operator) {
  97.             case "+":
  98.                 return true;
  99.             case "-":
  100.                 return true;
  101.             case "*":
  102.                 return true;
  103.             case "/":
  104.                 return true;
  105.             default:
  106.                 return false;
  107.         }
  108.     }
  109.  
  110.     static boolean isOperand(String operand) {
  111.         for (int i = 0; i < operand.toCharArray().length; ++i) {
  112.             if (!Character.isDigit(operand.charAt(i))) {
  113.                 return false;
  114.             }
  115.         }
  116.         return true;
  117.     }
  118.  
  119.     static int postFixEvaluation(String[] phrase) {
  120.         ArrayStack<Integer> magacin = new ArrayStack<>(1000);
  121.         int rezultat = 0;
  122.  
  123.         for (int i = 0; i < phrase.length; ++i) {
  124.             if (isOperand(phrase[i])) {
  125.                 magacin.push(Integer.parseInt(phrase[i]));
  126.             } else {
  127.                 if (isOperator(phrase[i])) {
  128.                     int broj = magacin.pop();
  129.                     switch (phrase[i]) {
  130.                         case "+":
  131.                             rezultat = Math.abs(broj + magacin.pop());
  132.                             break;
  133.                         case "-":
  134.                             rezultat = Math.abs(broj - magacin.pop());
  135.                             break;
  136.                         case "*":
  137.                             rezultat = Math.abs(broj * magacin.pop());
  138.                             break;
  139.                         case "/":
  140.                             if (broj == 0 || magacin.pop() == 0) {
  141.                                 //delenje so 0
  142.                                 rezultat = 0;
  143.                             } else {
  144.                                 rezultat = Math.abs(magacin.pop()/broj);
  145.                                 break;
  146.                             }
  147.                     }
  148.                 }
  149.                 magacin.push(rezultat);
  150.             }
  151.         }
  152.        
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment