aleffelixf

NPR with Stack Java

Oct 29th, 2021 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Stack<T> {
  4.  
  5.     private int stackPos;
  6.     private T[] stack;
  7.  
  8.     public Stack(int size) {
  9.         this.stackPos = -1;
  10.         this.stack = (T[]) new Object[size];
  11.     }
  12.  
  13.     public void push(T value) {
  14.         if (this.stackPos < this.stack.length - 1) {
  15.             this.stack[++stackPos] = value;
  16.         }
  17.     }
  18.  
  19.     public T pop() {
  20.         if (this.isEmpty()) {
  21.             return null;
  22.         }
  23.         return this.stack[stackPos--];
  24.     }
  25.  
  26.     public int length() {
  27.         if (this.isEmpty()) {
  28.             return 0;
  29.         }
  30.         return this.stackPos + 1;
  31.     }
  32.  
  33.     public T head() {
  34.         if (this.isEmpty()) {
  35.             return null;
  36.         }
  37.         return this.stack[stackPos];
  38.     }
  39.  
  40.     public boolean isEmpty() {
  41.         return this.stackPos == -1;
  42.     }
  43.  
  44.     public void print() {
  45.         if(this.isEmpty()) {
  46.             System.out.println("Pilha vazia..");  
  47.         }else{  
  48.             for(int i = stackPos; i >= 0; i--){  
  49.                 System.out.println(stack[i]);  
  50.             }  
  51.         }  
  52.     }
  53.  
  54. }
  55.  
  56. public class NPR {
  57.  
  58.     public static int operate(String operation, int left, int right) {
  59.         switch (operation) {
  60.             case "+": return left + right;
  61.             case "-": return left - right;
  62.             case "*": return left * right;
  63.             case "/":
  64.                 if (right == 0) {
  65.                     System.out.println("ERR: Não é possível dividir por zero");
  66.                     return left;
  67.                 }
  68.                 return left / right;
  69.             default: return left;
  70.         }
  71.     }
  72.  
  73.     public static int parseOperation(String operation, Stack<Integer> stack) {
  74.         int result = (stack.isEmpty()) ? 0 : stack.pop();
  75.         if (!stack.isEmpty()) {
  76.             result = operate(operation, stack.pop(), result);
  77.         }
  78.         return result;
  79.     }
  80.  
  81.     public static boolean isInteger(String input) {
  82.         if (input == null) return false;
  83.  
  84.         try {
  85.             Integer.parseInt(input);
  86.         } catch (NumberFormatException ex) {
  87.             return false;
  88.         }
  89.         return true;
  90.     }
  91.  
  92.     public static boolean isOperation(String input) {
  93.         if (input == null) return false;
  94.  
  95.         return input.equals("+") || input.equals("-") || input.equals("*") || input.equals("/");
  96.     }
  97.  
  98.     public static void main(String[] args) {
  99.         Scanner reader = new Scanner(System.in);
  100.         Stack<Integer> stack = new Stack<Integer>(30);
  101.  
  102.         System.out.println("Digite a operação (s para sair)");
  103.         String input = "";
  104.  
  105.         while (!input.equals("s")) {
  106.             input = reader.nextLine();
  107.             if (isInteger(input)) {
  108.                 stack.push(Integer.parseInt(input));
  109.             } else if (isOperation(input)) {
  110.                 int current = parseOperation(input, stack);
  111.                 if (stack.length() == 0) {
  112.                     System.out.println(">> " + current);
  113.                 } else {
  114.                     System.out.println("> " + current);
  115.                 }
  116.                 stack.push(current);
  117.             }
  118.         }
  119.  
  120.         reader.close();
  121.     }
  122.  
  123. }
Add Comment
Please, Sign In to add comment