2 Replies - 7271 Views - Last Post: 26 January 2012 - 02:00 AM Rate Topic: -----

#1 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 12-October 11

Using Stacks when creating a calculator

Posted 24 January 2012 - 11:52 AM

Basically, I have four classes ListNode, List, Stack, and RPN. I'm creating a reverse polish notation calculator and have become stuck. I have got as far as the RPN class where I'm calculating the value of user inputted values. In the RVN class I'm using the StringTokenizer to split the values up. Anyway the problem I'm having is in the RPN class where if a certain operater is inputted it should pop 2 numbers from the stack and apply that operator and push the result onto the stack. As you can see I have tried to do this but it is not working and assuming my logic is wrong somewhere. I get a error message saying void type is not allowed here. I know what this means so can someone send me on the right track should I change that particular method in the stack class or change what I have done in the RPN class?

import java.util.StringTokenizer;
/**
 * 
 * 
 * @author Mark Goddard
 * @version 24/01/2012
 */
public class RPN
{
    // instance variables - replace the example below with your own
    private Stack myStack;

    /**
     * Constructor for objects of class RPN
     */
    public RPN()
    {
        // initialise instance variables
        myStack = new Stack();
    }

    /**
     * 
     * 
     * @param  
     * @return     
     */
    public void process()
    {
        System.out.println("Please enter data: ");
        String splitString = Genio.getString();
        String operator, token;
        int operand, i;
  
        StringTokenizer tokenizor = new  StringTokenizer(splitString, ",");
        i=0;
        System.out.println("Test data = " + splitString);
        while (tokenizor.hasMoreElements())
        {
            token = tokenizor.nextToken();
            if (token.equals("+"))
            {
                myStack.push(myStack.pop() + myStack.pop());
            }
            else if (token.equals("-"))
            {
                myStack.push(myStack.pop() - myStack.pop());
            }
            else if (token.equals("*"))
            {
                myStack.push(myStack.pop() * myStack.pop());
            }
            else
            {
                try
                {
                    operand = Integer.parseInt(token);
                    myStack.push(operand);
                    System.out.println("Operand: " + operand);
                }
                catch (NumberFormatException e)
                {
                    System.out.println("Warning, error in test data");
                }   
            }
        }
    }
}




/**
 * 
 * 
 * @author Mark Goddard
 * @version 23/01/2012
 */
public class Stack
{
    // instance variables - replace the example below with your own
    private List myList;
    /**
     * Constructor for objects of class Stack
     */
    public Stack()
    {
        // initialise instance variables
        myList = new List();
    }

    /**
     * Adds a character to be pushed at top of stack
     * 
     * @param
     * @return     
     */
    public void push(int num)
    {
        myList.addToList(num);
    }
    
    /**
     * If the stack is not empty, it will make
     * a not of that character and delete it
     * @param
     * @return     
     */
    public void pop()
    {
        myList.removeFromList();
    }
    
    /**
     * Checks to see whether the stack is empty or not
     * 
     * @param
     * @return     
     */
    public void isStackEmpty()
    {
        myList.isListEmpty();
    }
    
    /**
     * Checks to see whether the stack is empty or not
     * 
     * @param
     * @return     
     */
    public void testStackPrint()
    {
        myList.testPrintList();
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Using Stacks when creating a calculator

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Using Stacks when creating a calculator

Posted 24 January 2012 - 12:24 PM

View Postcrapmyster, on 24 January 2012 - 01:52 PM, said:

Anyway the problem I'm having is in the RPN class where if a certain operater is inputted it should pop 2 numbers from the stack and apply that operator and push the result onto the stack.

Not really true
When an operator is inputed it should pop a value from the stack an apply that operator to that poped value and the value present on the led display.
The result will be pushed back to the stack only when a new valid character will be entered because if it is an operator that is entered it is applies to the value on the led display and the next value on the stack
If stack is
1
2
3
4
led 5
+ will do 5 + 4 led will be 9 (9 not on the stack)
+ will do 9 + 3 led will be 12 (12 not on the stack)
stack
1
2

Happy coding

http://www.dreaminco...rpn-calculator/

This post has been edited by pbl: 24 January 2012 - 12:26 PM

Was This Post Helpful? 2
  • +
  • -

#3 crapmyster  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 12-October 11

Re: Using Stacks when creating a calculator

Posted 26 January 2012 - 02:00 AM

Hey,

Sorry for not being clear, but I'm only wanting to add, subtract and multiply two numbers at the moment. Trying to keep it nice and simple. I'm still confused as to how implement it still. Is my code good especially this part which I'm stuck on as a error appears when I use the complier 'void type is not valid here' and I'm wondering which way I should go about it. Thanks

myStack.push(myStack.pop() + myStack.pop());

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1