1. Member
Join Date
Jun 2008
Posts
54
Rep Power
0

## 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. 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. Member
Join Date
Jun 2008
Posts
54
Rep Power
0
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. Do you have the algorithm for evaluating Postfix? Its probably in a textbook somewhere. We'll need that to write the code.

5. Member
Join Date
Jun 2008
Posts
54
Rep Power
0
i don't, that's my problem...

6. Member
Join Date
Sep 2008
Posts
31
Rep Power
0
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. Member
Join Date
Sep 2008
Posts
31
Rep Power
0
You can also refer to some book of data structures.Proper algorithm is given there.

8. Member
Join Date
Jun 2008
Posts
54
Rep Power
0
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. 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. Member
Join Date
Sep 2008
Posts
31
Rep Power
0
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.

#### Posting Permissions

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