Results 1 to 10 of 10
  1. #1
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default Postfix-Notation

    Hey,
    it is me, again...
    this time i have to write a Postfix-Notation by using stacks.

    I have to calculate (3+4) * 5 and this is in Postfix Notation 3 4 + 5 *
    i kind of know how I have to do it... but i have problems to write it.

    Just thought that I will write down my ideas and maybe someone can help me to put it into proper Java-Code, that would be really nice:

    1) first i need to put the 3 4 + 5 * into an array. so lets say it is already in the array arithm[0] = 3
    arithm[1] = 4
    arithm[2] = +
    arithm[3] = 5
    arithm[4] = *

    2) i need to do a for loop i think with

    for(int i = 0; i < arithm.length; i++)

    3) now i need a condition, so that it is putting the numbers on a stack, but only the first two... so i did the class stack already with the methods push (putting a number on the stack, and pop, giving out the last element)

    if(arithm[i] != operandor)
    {
    push(arithm[i])
    }

    i actually don't know exactly how to write this condition...

    4) now i don't know how to carry on...

    it would be lovely if anyone could help me...

    little_polarbear

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    write a Postfix-Notation by using stacks
    What does that mean exacly?
    Are you supposed to 1)parse a normal expression into Postfix notation? Your step 1) appears to have done this.
    Or given a postfix expression, are you to 2)evalutate it? ie give the answer: 35

    You need to find the algorithm to solve the evaluation problem and then we can help you write the code to work it.

    Java has a Stack class with push and pop methods. Can you use that?

  3. #3
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default

    hey,
    yes i have to evaluate it, so that i am getting here the result 35.... but i have to write the code so, that i can do it with any postfix expression

    little_polarbear

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Do you have the algorithm for evaluating Postfix? Its probably in a textbook somewhere. We'll need that to write the code.

  5. #5
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default

    i don't, that's my problem...

  6. #6
    khajalid is offline Member
    Join Date
    Sep 2008
    Posts
    31
    Rep Power
    0

    Default

    I think i have the solution.
    To evaluate from postfix you just need to push the operand into stack.when operator comes push first operand from satck name it A,then second name it B.
    Now, perform B operator A.This will definitely work.

  7. #7
    khajalid is offline Member
    Join Date
    Sep 2008
    Posts
    31
    Rep Power
    0

    Default

    You can also refer to some book of data structures.Proper algorithm is given there.

  8. #8
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default

    hey,
    thank u so much... that's actually what i ment... i am just not really good in expressing it, sry...
    but how can i write it in java, that the programm is realising that it is a operandor or a operator...

    little_polarbear

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    To recognize if a String is an operator, have a collection of all the operators and check if that string is in the collection.

  10. #10
    khajalid is offline Member
    Join Date
    Sep 2008
    Posts
    31
    Rep Power
    0

    Default

    Just take each no in strings and compare with its ASCII value if it is between 48 and 57 then it is operand else it is operator.

Similar Threads

  1. Replies: 1
    Last Post: 07-05-2008, 04:08 PM
  2. Replies: 0
    Last Post: 04-15-2008, 08:36 PM
  3. How to parse postfix arithmetic expressions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:36 PM
  4. Big Oh Notation and Heaps
    By gibsonrocker800 in forum Advanced Java
    Replies: 8
    Last Post: 01-25-2008, 11:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •