# Postfix-Notation

• 09-08-2008, 08:02 PM
little_polarbear
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
• 09-08-2008, 09:01 PM
Norm
Quote:

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?
• 09-09-2008, 09:29 AM
little_polarbear
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
• 09-09-2008, 01:47 PM
Norm
Do you have the algorithm for evaluating Postfix? Its probably in a textbook somewhere. We'll need that to write the code.
• 09-09-2008, 02:40 PM
little_polarbear
i don't, that's my problem...
• 09-09-2008, 03:54 PM
khajalid
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.
• 09-09-2008, 03:56 PM
khajalid
You can also refer to some book of data structures.Proper algorithm is given there.
• 09-09-2008, 03:56 PM
little_polarbear
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
• 09-09-2008, 04:19 PM
Norm
To recognize if a String is an operator, have a collection of all the operators and check if that string is in the collection.
• 09-09-2008, 04:24 PM
khajalid
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.