Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. /**
  2.  * The ArrayBasedStack class which is an array based implementation of the Stack class
  3.  *
  4.  * @author Quran Kimbrough
  5.  * @version 1.0
  6.  */
  7. public class ArrayBasedStack {
  8.     private int size;
  9.     private int[] stack;
  10.     private int top;
  11.  
  12.     /**
  13.      * The default constructor which takes the variable size of type int while initializing stack and top
  14.      *
  15.      * @param size the size of the ArrayBasedStack
  16.      */
  17.     public ArrayBasedStack(int size) {
  18.         this.size = size;
  19.         stack = new int[size];
  20.         top = -1;
  21.     }
  22.  
  23.     /**
  24.      * A mutator method which displays the stack
  25.      */
  26.     public void display() {
  27.  
  28.         for (int i = 0; i <= top; i++) {
  29.             System.out.print(stack[i] + " ");
  30.         }
  31.         System.out.println();
  32.     }
  33.  
  34.     /**
  35.      * An accessor method which determines if the stack is empty
  36.      *
  37.      * @return returns whether the stack is empty
  38.      */
  39.     public boolean isEmpty() {
  40.         return top == -1;
  41.     }
  42.  
  43.     /**
  44.      * An accessor method which returns the top of the stack;
  45.      *
  46.      * @return the top of the stack
  47.      */
  48.     public int peek() {
  49.         if (!isEmpty()) {
  50.             return stack[top];
  51.         } else {
  52.             System.out.println("error: stack empty");
  53.         }
  54.         return -1;
  55.     }
  56.  
  57.     /**
  58.      * An accessor method which removes from the top of the stack and then returns the top of the stack
  59.      *
  60.      * @return the top of the stack that has been removed
  61.      */
  62.     public int pop() {
  63.         if (!isEmpty()) {
  64.             int x = stack[top];
  65.             top--;
  66.             return x;
  67.         } else {
  68.             System.out.println("error: stack empty");
  69.         }
  70.         return -1;
  71.     }
  72.  
  73.     /**
  74.      * A mutator method which pushes a value into the stack
  75.      *
  76.      * @param value the int to be pushed
  77.      */
  78.     public void push(int value) {
  79.         if (top == size - 1) {
  80.             System.out.println("error: stack full");
  81.         } else {
  82.             top++;
  83.             stack[top] = value;
  84.         }
  85.     }
  86. }
  87.  
  88. import java.util.Scanner;
  89.  
  90. /**
  91.  * The PostFix class which uses the ArrayBased list from LAB2 updated to contain functions relevant to the Stack class.
  92.  *
  93.  * @author QuranKimbrough
  94.  * @version 1.0
  95.  */
  96. public class PostFix {
  97.  
  98.     /**
  99.      * An accessor method which returns the evaluated PostFix
  100.      *
  101.      * @param input the String to be evaluated for PostFix
  102.      * @return returns the top of the stack
  103.      */
  104.     public static int evaluatePostFix(String input) {
  105.         String[] inputArray = input.split("\\s+");
  106.         int operatorCount = 0, operandCount = 0;
  107.         for (String s : inputArray){
  108.             if (s.equals("+") || s.equals("-") || s.equals("/") || s.equals("*")){
  109.                 operatorCount++;
  110.             }else{
  111.                 operandCount++;
  112.             }
  113.         }
  114.         if (operandCount == operatorCount + 1) {
  115.             System.out.println("PostFix input is valid");
  116.         }else{
  117.             System.out.println("invalid PostFix input");
  118.             return -1;
  119.         }
  120.         Scanner sc = new Scanner(input);
  121.         ArrayBasedStack stack = new ArrayBasedStack(input.length());
  122.  
  123.         while (sc.hasNext()) {
  124.             if (sc.hasNextInt()) {
  125.                 stack.push(sc.nextInt());
  126.                 continue;
  127.             }
  128.             int z = stack.pop();
  129.             int y = stack.pop();
  130.             char op = sc.next().charAt(0);
  131.             if (op == '+')
  132.                 stack.push(y + z);
  133.             else if (op == '-')
  134.                 stack.push(y - z);
  135.             else if (op == '*')
  136.                 stack.push(y * z);
  137.             else if (op == '/')
  138.                 stack.push(y / z);
  139.         }
  140.         sc.close();
  141.         System.out.println(stack.peek());
  142.         return stack.peek();
  143.     }
  144.  
  145.    
  146.     /**
  147.      * The main method used to drive the PostFix class.
  148.      *
  149.      * @param args arguments
  150.      */
  151.     public static void main(String[] args) {
  152.         evaluatePostFix("3 2 * 11 -");
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement