Advertisement
ivana_andreevska

Postfix Notacija

Nov 22nd, 2021
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. package kolokviumska;
  2. import java.util.NoSuchElementException;
  3. import java.util.Scanner;
  4.  
  5. interface Stack<E> {
  6.     public boolean isEmpty ();
  7.     public E peek ();
  8.     public void clear ();
  9.     public void push (E x);
  10.     public E pop ();
  11. }
  12.  
  13.  class ArrayStack<E> implements Stack<E> {
  14.  
  15.     // Stekot e pretstaven na sledniot nacin:
  16.     //depth e dlabochinata na stekot, a
  17.     // elems[0...depth-1] se negovite elementi.
  18.     private E[] elems;
  19.     private int depth;
  20.  
  21.     @SuppressWarnings("unchecked")
  22.     public ArrayStack (int maxDepth) {
  23.         // Konstrukcija na nov, prazen stek.
  24.         elems = (E[]) new Object[maxDepth];
  25.         depth = 0;
  26.     }
  27.  
  28.  
  29.     public boolean isEmpty () {
  30.         // Vrakja true ako i samo ako stekot e prazen.
  31.         return (depth == 0);
  32.     }
  33.  
  34.  
  35.     public E peek () {
  36.         // Go vrakja elementot na vrvot od stekot.
  37.         if (depth == 0)
  38.             throw new NoSuchElementException();
  39.         return elems[depth-1];
  40.     }
  41.  
  42.  
  43.     public void clear () {
  44.         // Go prazni stekot.
  45.         for (int i = 0; i < depth; i++)  elems[i] = null;
  46.         depth = 0;
  47.     }
  48.  
  49.  
  50.     public void push (E x) {
  51.         // Go dodava x na vrvot na stekot.
  52.         elems[depth++] = x;
  53.     }
  54.  
  55.  
  56.     public E pop () {
  57.         // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  58.         if (depth == 0)
  59.             throw new NoSuchElementException();
  60.         E topmost = elems[--depth];
  61.         elems[depth] = null;
  62.         return topmost;
  63.     }
  64. }
  65.  
  66. public class Postfix {
  67.  
  68.     public static int resultPostfix(char[] izraz)
  69.     {
  70.         ArrayStack<Integer> stekce=new ArrayStack<>(110);
  71.         int number=0;
  72.  
  73.         for(int i=0;i<izraz.length;i++)
  74.         {
  75.             if(Character.isDigit(izraz[i]) && Character.isDigit(izraz[i]))
  76.             {
  77.                 number=number*10 + Character.getNumericValue(izraz[i]);
  78.             }
  79.             if(Character.isDigit(izraz[i]) && !Character.isDigit(izraz[i]))
  80.             {
  81.                 number=number*10 + Character.getNumericValue(izraz[i]);
  82.                 stekce.push(number);
  83.                 number=0;
  84.             }
  85.             if(izraz[i]=='+' || izraz[i]=='-' || izraz[i]=='*' || izraz[i]=='/')
  86.             {
  87.                 int operandA=stekce.pop();
  88.                 int operandB=stekce.pop();
  89.  
  90.                 if(izraz[i]=='+')
  91.                 {
  92.                     stekce.push(operandA+operandB);
  93.                 }
  94.                 else if(izraz[i]=='-')
  95.                 {
  96.                     stekce.push(operandB-operandA);
  97.                 }
  98.                 else if(izraz[i]=='*')
  99.                 {
  100.                     stekce.push(operandA*operandB);
  101.                 }
  102.                 else
  103.                     stekce.push(operandB/operandA);
  104.             }
  105.         }
  106.         return stekce.pop();
  107.     }
  108.  
  109.     public static void main(String[] args) {
  110.         Scanner s=new Scanner(System.in);
  111.         String s1=s.nextLine();
  112.         char[]c=s1.toCharArray();
  113.         int result=Postfix.resultPostfix(c);
  114.         System.out.println(result);
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement