Help I'm stuck on a reverse polish calculation

Discussion in 'Java' started by ron, Jun 30, 2003.

  1. ron

    ron Guest

    have been stuck on this for several days now.
    I am trying to create a reverse polish calculator and I'm stuck at an
    intermediate stage.
    This is what I know I have to do (just not sure how to do it to much of a
    newbie :( )
    I'm trying to call method, isOp, in the int method, evaluate. While in INT
    evaluate method and in a while(hasNextToken) loop. Calling isOp on each
    token, nextToken.

    If it returns False, convert to (Integer) and push the token into a Stack.
    True, you will pop the two tokens, convert them to (Integer) and store them
    into x and y respectively. Do the Arithmetic operation and then push it back
    to the stack.

    I'm pretty sure the IsOp test is good just can't figure out the first part.


    I am trying to get the following code to work:
    ----------------------
    public class Evaluator {//tokList contains a list of tokens in postfix
    notation.
    //evaluate the expression and return the result
    int x, y,resu;
    int i,j=0;
    public int evaluate (ArrayList tokList) {
    while (tokList.hasNextToken()){
    isOp (s);
    (tokList.nextToken());

    Stack eval =new Stack();

    if (false)
    eval.push(new Integer(s));
    else
    { y=((Integer) eval.pop()).intValue();
    x=((Integer) eval.pop()).intValue();
    switch (s)
    {
    case '+':
    resu=x+y;
    break;
    case '-':
    resu=x-y;
    break;
    case '*':
    resu=x*y;
    break;
    case '/':
    resu=x/y;
    break;
    default:
    resu=0;
    }
    eval.push(new Integer(resu));

    }
    }
    resu= ((Integer) eval.pop()).intValue();
    return resu;
    }
    }

    // Check whether s represents an operator
    private boolean isOp(String s) {
    return( s.equals( " + " ) || s.equals( " -- " ) || s.equals( " / " ) ||
    s.equals( " * " ) );

    } //isOp


    private boolean hprecedence(String a, String b) {
    return !a.equals("(") && (b.equals("+") || b.equals("-")
    || a.equals("*") || a.equals("/"));
    } //precedence

    }

    ----------------------

    Any help would be greatly appreciated, I'm told it's quite simple once you
    know how but I don't know....
     
    ron, Jun 30, 2003
    #1
    1. Advertising

  2. ron wrote:

    <snip>
    > I am trying to get the following code to work:

    <snip>

    Try getting it to compile first.

    Also, please indent properly if you'd like us to be able to read the
    code in order to help you. This will also greatly help you to find the
    obvious mistake.

    Stewart.

    --
    My e-mail is valid but not my primary mailbox. Please keep replies on
    on the 'group where everyone may benefit.
     
    Stewart Gordon, Jul 2, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. schapopa

    OuterHTML - loosing polish fonts

    schapopa, Jan 24, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    338
    schapopa
    Jan 24, 2005
  2. Replies:
    2
    Views:
    748
    Oliver Wong
    Feb 6, 2006
  3. dogbite
    Replies:
    4
    Views:
    694
    osmium
    Oct 10, 2003
  4. Steve Lambert

    Reverse polish

    Steve Lambert, Dec 19, 2004, in forum: C Programming
    Replies:
    11
    Views:
    639
    infobahn
    Dec 30, 2004
  5. Reverse Polish Ruby

    , Oct 20, 2006, in forum: Ruby
    Replies:
    0
    Views:
    109
Loading...

Share This Page