Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The ArrayBasedStack class which is an array based implementation of the Stack class
- *
- * @author Quran Kimbrough
- * @version 1.0
- */
- public class ArrayBasedStack {
- private int size;
- private int[] stack;
- private int top;
- /**
- * The default constructor which takes the variable size of type int while initializing stack and top
- *
- * @param size the size of the ArrayBasedStack
- */
- public ArrayBasedStack(int size) {
- this.size = size;
- stack = new int[size];
- top = -1;
- }
- /**
- * A mutator method which displays the stack
- */
- public void display() {
- for (int i = 0; i <= top; i++) {
- System.out.print(stack[i] + " ");
- }
- System.out.println();
- }
- /**
- * An accessor method which determines if the stack is empty
- *
- * @return returns whether the stack is empty
- */
- public boolean isEmpty() {
- return top == -1;
- }
- /**
- * An accessor method which returns the top of the stack;
- *
- * @return the top of the stack
- */
- public int peek() {
- if (!isEmpty()) {
- return stack[top];
- } else {
- System.out.println("error: stack empty");
- }
- return -1;
- }
- /**
- * An accessor method which removes from the top of the stack and then returns the top of the stack
- *
- * @return the top of the stack that has been removed
- */
- public int pop() {
- if (!isEmpty()) {
- int x = stack[top];
- top--;
- return x;
- } else {
- System.out.println("error: stack empty");
- }
- return -1;
- }
- /**
- * A mutator method which pushes a value into the stack
- *
- * @param value the int to be pushed
- */
- public void push(int value) {
- if (top == size - 1) {
- System.out.println("error: stack full");
- } else {
- top++;
- stack[top] = value;
- }
- }
- }
- import java.util.Scanner;
- /**
- * The PostFix class which uses the ArrayBased list from LAB2 updated to contain functions relevant to the Stack class.
- *
- * @author QuranKimbrough
- * @version 1.0
- */
- public class PostFix {
- /**
- * An accessor method which returns the evaluated PostFix
- *
- * @param input the String to be evaluated for PostFix
- * @return returns the top of the stack
- */
- public static int evaluatePostFix(String input) {
- String[] inputArray = input.split("\\s+");
- int operatorCount = 0, operandCount = 0;
- for (String s : inputArray){
- if (s.equals("+") || s.equals("-") || s.equals("/") || s.equals("*")){
- operatorCount++;
- }else{
- operandCount++;
- }
- }
- if (operandCount == operatorCount + 1) {
- System.out.println("PostFix input is valid");
- }else{
- System.out.println("invalid PostFix input");
- return -1;
- }
- Scanner sc = new Scanner(input);
- ArrayBasedStack stack = new ArrayBasedStack(input.length());
- while (sc.hasNext()) {
- if (sc.hasNextInt()) {
- stack.push(sc.nextInt());
- continue;
- }
- int z = stack.pop();
- int y = stack.pop();
- char op = sc.next().charAt(0);
- if (op == '+')
- stack.push(y + z);
- else if (op == '-')
- stack.push(y - z);
- else if (op == '*')
- stack.push(y * z);
- else if (op == '/')
- stack.push(y / z);
- }
- sc.close();
- System.out.println(stack.peek());
- return stack.peek();
- }
- /**
- * The main method used to drive the PostFix class.
- *
- * @param args arguments
- */
- public static void main(String[] args) {
- evaluatePostFix("3 2 * 11 -");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement