Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1. package comp;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.LinkedList;
  5. import java.util.Stack;
  6.  
  7. //https://github.com/blakeembrey/code-problems/blob/master/solutions/java/StackMachine.java
  8.  
  9. public class stack
  10. {
  11.     public int maxSize; //max size of stack
  12.     public int top; //top of stack
  13.     public String arr[]; //array of elements
  14.    
  15.     public stack(int n) //constructor
  16.     {
  17.         maxSize = n;
  18.        
  19.         arr = new String[maxSize];
  20.         top = 0;
  21.     }
  22.    
  23.     public boolean empty() //check for empty
  24.     {
  25.         if(top == 0){
  26.             return true;
  27.         }
  28.         else{
  29.             return false;
  30.         }
  31.     }
  32.    
  33.     public void push(String str) //push new element on to the stack
  34.     {
  35.         if(top < maxSize)
  36.         {
  37.             arr[top] = str;
  38.             top++;
  39.         }
  40.         else
  41.         {
  42.             System.out.println("Stack OVerflow");
  43.         }
  44.        
  45.     }
  46.     public String pop() //pop element off of the stack and return it
  47.     {
  48.         if(!empty())
  49.         {
  50.             String temp = this.peek();
  51.             arr[top-1] = null;
  52.             top--;
  53.             return temp;
  54.            
  55.         }else{
  56.             return null;
  57.         }
  58.     }
  59.    
  60.     public String peek() //view top element in the stack
  61.     {
  62.         if(!empty())
  63.         {
  64.             return arr[top-1];
  65.         }
  66.         else
  67.         {
  68.             return null;
  69.         }
  70.     }
  71.    
  72.     public boolean isNumeric(String str)  
  73.     {  
  74.         //return str.matches("-?\\d+(\\.\\d+)?");  //match a number with optional '-' and decimal.     
  75.          try  
  76.          {  
  77.            double d = Double.parseDouble(str);  
  78.          }  
  79.          catch(NumberFormatException nfe)  
  80.          {  
  81.            return false;  
  82.          }  
  83.          return true;
  84.     }
  85.    
  86.     public int getSize()
  87.     {
  88.         return top;
  89.     }
  90.    
  91.     public String execute()
  92.     {
  93.         //LinkedList<String> solution = new LinkedList<String>();
  94.         //Stack solution = new Stack();
  95.         stack solution = new stack(5);
  96.        
  97.         int temp;
  98.         String result = "none";
  99.        
  100.         for(int i = 0; i <= maxSize-1; i++)
  101.         {
  102.             String s = this.pop(); //current item being looked at
  103.            
  104.             if(empty() == true && solution.empty())//no input to computeS
  105.             {
  106.                 System.out.println("NO STACK"); //tell user error
  107.                 return "No Stack"; //return error code
  108.             }
  109.             else
  110.             {
  111.                 if(isNumeric(s)) //if numeric add to the list
  112.                 {
  113.                     int a = Integer.parseInt(s);
  114.                     solution.push(s);
  115.                 }
  116.                 else if(solution.getSize() >= 2) //if two there are 2 elements in the list we can perform an operation
  117.                 {
  118.                     if(s == "+")//perform addition
  119.                     {
  120.                         //int x = solution.pop() + solution.pop();                     
  121.                         int x = Integer.parseInt(solution.pop()) + Integer.parseInt(solution.pop());
  122.                        
  123.                         String y = Integer.toString(x);
  124.                         solution.push(y);
  125.  
  126.                     }
  127.                     else if(s == "-")//perform subtraction
  128.                     {
  129.                         int x = Integer.parseInt(solution.pop()) - Integer.parseInt(solution.pop());
  130.                        
  131.                         String y = Integer.toString(x);
  132.                         solution.push(y);
  133.                        
  134.                         /*int x = solution.pop() - solution.pop();
  135.                         solution.push(x);*/
  136.                     }
  137.                     else if(s == "/")//perform division
  138.                     {
  139.                         int x = Integer.parseInt(solution.pop()) / Integer.parseInt(solution.pop());
  140.                        
  141.                         String y = Integer.toString(x);
  142.                         solution.push(y);
  143.                     }
  144.                     else if(s == "*")//perform multiplication
  145.                     {
  146.                         int x = Integer.parseInt(solution.pop()) * Integer.parseInt(solution.pop());
  147.                        
  148.                         String y = Integer.toString(x);
  149.                         solution.push(y);
  150.                     }
  151.                 }
  152.             }  
  153.         }
  154.         result = solution.pop();
  155.         return result;
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement