Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: evaluating postfix expressions using stack

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default evaluating postfix expressions using stack

    Hi, I am supposed to write a program that will evaluate postfix expressions using a linked list stack. The user will input the expression on the command line and I have to check if each part of the input is either a operand or an operator. I've gotten the program to compile, but it won't do anything. I think I made a mistake with checking the user input. Any advice is greatly appreciated! Thank you!

    import java.util.*;
     
    public class Calc<T> implements Stack<T>
    {
    	private int position;
    	private Node<T> head;
    	public Calc()
    	{
    		position = 0;
    		head = null;
    	}
     
    	public int size()
    	{
    		return position;
    	}
    	public boolean isEmpty()
    	{
    		return size() == 0;
    	}
     
    	public void push(T item)
    	{
    		if(head == null){
    			Node<T> newNode;
    			newNode = new Node<T>(item);
     
    			newNode.setNext(head);
    			head = newNode;
    			position++;
    		}else{
    			Node<T> newNode;
    			newNode = new Node<T>(item);
     
    			newNode.setNext(head);
    			head = newNode;
    			position++;
    		}
     
    	}
    	public T pop() throws StackEmptyException
    	{
    		if(isEmpty())
    		{
    			throw new StackEmptyException("Stack empty");
    		}
    		T val = head.getItem();
    		remove();
    		return val;
    	}
    	public T peek() throws StackEmptyException
    	{
    		if(isEmpty()){
    			throw new StackEmptyException("Stack empty");
    		}
    		return head.getItem();
    	}
    	public void makeEmpty()
    	{
    		head = null;
    		position = 0;
    	}
    	public void remove()
    	{
    		if(position == 1)
    		{
    			head = null;
    			position = 0;
    		}else{
    			head = head.getNext();
    			position--;
    		}
    	}
    	public static void add(Stack<Integer> s){
    		try{
    			int n = s.pop();
    			int m = s.pop();
    			n =+ m;
    			s.push(n);
    		}catch(StackEmptyException e)
    		{
    		}
    	}
    	public static void sub(Stack<Integer> s){
    		try{	
    			int n = s.pop();
    			int m = s.pop();
    			n = n - m;
    			s.push(n);
    		}catch(StackEmptyException e)
    		{
    		}
    	}
    	public static void divide(Stack<Integer> s){
    		try{
    			int n = s.pop();
    			int m = s.pop();
    			n = n/m;
    			s.push(n);
    		}catch(StackEmptyException e)
    		{
    		}
    	}
    	public static void times(Stack<Integer> s){
    		try{
    			int n = s.pop();
    			int m = s.pop();
    			n =n*m;
    			s.push(n);
    		}catch(StackEmptyException e)
    		{
    		}
    	}
    	public static void main(String[]args){
    		Scanner input = new Scanner(System.in);
    		Stack<Integer> s1 = new Calc<Integer>();
    		while(input.hasNext()){
    				if(input.next().equals('+')){
    					add(s1);
    				}else if(input.next().equals('-')){
    					sub(s1);
    				}else if(input.next().equals('/')){
    					divide(s1);
    				}else if(input.next().equals('x')){
    					times(s1);
    				}else{
    					s1.push(input.nextInt());
    				}
    			}
     
    		System.out.println(s1);
    	}	}

    interface Stack
    public interface Stack<T>
    {
    	int size();
    	boolean isEmpty();
    	void push (T element);
    	T pop() throws StackEmptyException;
    	T peek() throws StackEmptyException;
    	void makeEmpty();
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: evaluating postfix expressions using stack

    I recommend you step through your program with a debugger. Where does the program deviate from what you'd expect it to do? At the very least, put in a ton of print statements to gauge what's happening.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    lieles (March 16th, 2011)

  4. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: evaluating postfix expressions using stack

    Quote Originally Posted by KevinWorkman View Post
    I recommend you step through your program with a debugger. Where does the program deviate from what you'd expect it to do? At the very least, put in a ton of print statements to gauge what's happening.
    Debugging with System.out.println
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: evaluating postfix expressions using stack

    So I've gone through and found where my program is going wrong. It is when it is checking whether the next character in the input is a operator or not. I am really quite confused why this is happening. Do I need to change it from an integer stack to a string stack?

  6. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: evaluating postfix expressions using stack

    You need to be more specific. How is it "going wrong"? Your best bet is to provide an SSCCE that demonstrates the problem as specifically as possible- perhaps even a simple class with a main function that contains just a couple lines.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] Help with prefix and postfix(increment&decrement)
    By Lokesh in forum Object Oriented Programming
    Replies: 1
    Last Post: February 12th, 2011, 10:20 AM
  2. Evaluating Expressions
    By javapenguin in forum What's Wrong With My Code?
    Replies: 19
    Last Post: November 16th, 2010, 08:30 PM
  3. VM re-evaluating a string
    By Johannes in forum Java Theory & Questions
    Replies: 6
    Last Post: September 20th, 2010, 04:44 PM
  4. convert infix to postfix
    By tina G in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 11th, 2010, 01:46 AM
  5. Evaluating to negative zero
    By helloworld922 in forum Java Theory & Questions
    Replies: 6
    Last Post: June 25th, 2009, 02:34 PM