Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package comp;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.Stack;
- //https://github.com/blakeembrey/code-problems/blob/master/solutions/java/StackMachine.java
- public class stack
- {
- public int maxSize; //max size of stack
- public int top; //top of stack
- public String arr[]; //array of elements
- public stack(int n) //constructor
- {
- maxSize = n;
- arr = new String[maxSize];
- top = 0;
- }
- public boolean empty() //check for empty
- {
- if(top == 0){
- return true;
- }
- else{
- return false;
- }
- }
- public void push(String str) //push new element on to the stack
- {
- if(top < maxSize)
- {
- arr[top] = str;
- top++;
- }
- else
- {
- System.out.println("Stack OVerflow");
- }
- }
- public String pop() //pop element off of the stack and return it
- {
- if(!empty())
- {
- String temp = this.peek();
- arr[top-1] = null;
- top--;
- return temp;
- }else{
- return null;
- }
- }
- public String peek() //view top element in the stack
- {
- if(!empty())
- {
- return arr[top-1];
- }
- else
- {
- return null;
- }
- }
- public boolean isNumeric(String str)
- {
- //return str.matches("-?\\d+(\\.\\d+)?"); //match a number with optional '-' and decimal.
- try
- {
- double d = Double.parseDouble(str);
- }
- catch(NumberFormatException nfe)
- {
- return false;
- }
- return true;
- }
- public int getSize()
- {
- return top;
- }
- public String execute()
- {
- //LinkedList<String> solution = new LinkedList<String>();
- //Stack solution = new Stack();
- stack solution = new stack(5);
- int temp;
- String result = "none";
- for(int i = 0; i <= maxSize-1; i++)
- {
- String s = this.pop(); //current item being looked at
- if(empty() == true && solution.empty())//no input to computeS
- {
- System.out.println("NO STACK"); //tell user error
- return "No Stack"; //return error code
- }
- else
- {
- if(isNumeric(s)) //if numeric add to the list
- {
- int a = Integer.parseInt(s);
- solution.push(s);
- }
- else if(solution.getSize() >= 2) //if two there are 2 elements in the list we can perform an operation
- {
- if(s == "+")//perform addition
- {
- //int x = solution.pop() + solution.pop();
- int x = Integer.parseInt(solution.pop()) + Integer.parseInt(solution.pop());
- String y = Integer.toString(x);
- solution.push(y);
- }
- else if(s == "-")//perform subtraction
- {
- int x = Integer.parseInt(solution.pop()) - Integer.parseInt(solution.pop());
- String y = Integer.toString(x);
- solution.push(y);
- /*int x = solution.pop() - solution.pop();
- solution.push(x);*/
- }
- else if(s == "/")//perform division
- {
- int x = Integer.parseInt(solution.pop()) / Integer.parseInt(solution.pop());
- String y = Integer.toString(x);
- solution.push(y);
- }
- else if(s == "*")//perform multiplication
- {
- int x = Integer.parseInt(solution.pop()) * Integer.parseInt(solution.pop());
- String y = Integer.toString(x);
- solution.push(y);
- }
- }
- }
- }
- result = solution.pop();
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement