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 7 of 7

Thread: EVALUATION OF POSTFIX NOTATION

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default EVALUATION OF POSTFIX NOTATION

    class StackObj
    {
    private Object[] theStack;
    private int maxSize;
    private int top;

    public StackObj(int s)
    {
    maxSize = s;
    theStack = new Object[maxSize];
    top = -1;
    }
    public void push(Object elem)
    {
    top++;
    theStack[top] = elem;
    }
    public Object pop()
    {
    Object result = theStack[top];
    top--;
    return result;
    }
    public Object top()
    {
    return theStack[top];
    }
    public boolean isFull()
    {
    return (top == (maxSize-1) );
    }
    public boolean isEmpty()
    {
    return (top == -1);
    }
    public int size()
    {
    return (top+1);
    }
    }

    Ok so this is a class called STACKOBJ that is a stack of OBJECTS...
    THIS IS THE CODE...
    [CENTER]public static int evaluatepostfix(String S){
    		StackObj temp = new StackObj(S.length());
    		for(int i = 0; i < S.length(); i++){
    			if(((Integer.parseInt(S.charAt(i) + "")) >= 1) && ((Integer.parseInt(S.charAt(i) + "")) <= 9)){
    				temp.push(new Integer (Integer.parseInt((S.charAt(i)+"")))); 
    				break;
    			}else{
    				int op1 = ((Integer)temp.pop()).intValue();
    				int op2 = ((Integer)temp.pop()).intValue();
    				switch (S.charAt(i)){
    					case '+': temp.push(new Integer ((op2) + (op1))); break;
    					case '-': temp.push(new Integer ((op2) - (op1))); break;
    					case '*': temp.push(new Integer ((op2) * (op1))); break;
    					case '/': temp.push(new Integer ((op2) / (op1))); break;	
    				}
    			}
    		}
    		return ((Integer)temp.pop()).intValue();
    	}[/CENTER]
    Apparently if I try to run System.out.print(evaluatepostfix("123*+9-")); the output is 1 :S
    HELP???


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: EVALUATION OF POSTFIX NOTATION

    if(((Integer.parseInt(S.charAt(i) + "")) >= 1) && ((Integer.parseInt(S.charAt(i) + "")) <= 9)){
        temp.push(new Integer (Integer.parseInt((S.charAt(i)+"")))); 
        break; // ?????
    Think very carefully what that statement does. When you fix that you will have plenty of other problems to fix.
    Improving the world one idiot at a time!

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

    Medo Almasry (November 7th, 2011)

  4. #3
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: EVALUATION OF POSTFIX NOTATION

    all this means is that if the character is between 1 and 9 push it into the stack...

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: EVALUATION OF POSTFIX NOTATION

    No. I'm talking about the break statement. What does that do?
    Improving the world one idiot at a time!

  6. #5
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: EVALUATION OF POSTFIX NOTATION

    I guess this is what happens when you spend hours watching the big bang theory -.-
    THANKS ALOT MAN
    I missed out that one

  7. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: EVALUATION OF POSTFIX NOTATION

    Your next assignment should you choose to accept is to write a game for Rock, Paper, Scissors, Lizard, Spock.
    Improving the world one idiot at a time!

  8. #7
    Member
    Join Date
    Jul 2011
    Posts
    38
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: EVALUATION OF POSTFIX NOTATION

    I shall respectfully reject the offer because right now am working on databases (stacks, queues, linked lists, trees)...
    and regarding games, next semester I shall be taking GUI and most probably I will be doing Entanglement *its a game*...
    Thanks again for the offer and if you have a source for questions that could make me better... a website or a book... that would be great...
    If you are wondering, I'm a computer Programmer "YET TO BE :p" but am just having a burnout here...

Similar Threads

  1. POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 23rd, 2011, 12:16 AM
  2. Evaluation of operators in expressions.
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 7
    Last Post: September 23rd, 2011, 07:23 PM
  3. Java:Evaluation of Mathematical Expression only from left to right only.
    By deepakl_2000 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 15th, 2011, 07:35 AM
  4. Parsing Scientific Notation from String
    By aussiemcgr in forum Java Theory & Questions
    Replies: 7
    Last Post: December 14th, 2010, 09:45 AM
  5. Compute the frequency count and big-oh notation for a certain code segment
    By maykel_trinidad in forum Java Theory & Questions
    Replies: 3
    Last Post: November 13th, 2009, 10:23 AM