Results 1 to 10 of 10
Thread: PostfixNotation
 09082008, 08:02 PM #1Member
 Join Date
 Jun 2008
 Posts
 54
 Rep Power
 0
PostfixNotation
Hey,
it is me, again...
this time i have to write a PostfixNotation 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 JavaCode, 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
 09082008, 09:01 PM #2write a PostfixNotation by using stacks
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?
 09092008, 09:29 AM #3Member
 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
 09092008, 01:47 PM #4
Do you have the algorithm for evaluating Postfix? Its probably in a textbook somewhere. We'll need that to write the code.
 09092008, 02:40 PM #5Member
 Join Date
 Jun 2008
 Posts
 54
 Rep Power
 0
i don't, that's my problem...
 09092008, 03:54 PM #6Member
 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.
 09092008, 03:56 PM #7Member
 Join Date
 Sep 2008
 Posts
 31
 Rep Power
 0
You can also refer to some book of data structures.Proper algorithm is given there.
 09092008, 03:56 PM #8Member
 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
 09092008, 04:19 PM #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.
 09092008, 04:24 PM #10Member
 Join Date
 Sep 2008
 Posts
 31
 Rep Power
 0
Similar Threads

any can help me? About MDAS and PEMDAS rules and InfixPrefix, InfixPostfix rules
By darlineth in forum New To JavaReplies: 1Last Post: 07052008, 03:08 PM 
How to convert infix arithmetic expressions to postfix
By Java Tip in forum java.langReplies: 0Last Post: 04152008, 07:36 PM 
How to parse postfix arithmetic expressions
By Java Tip in forum java.langReplies: 0Last Post: 04152008, 07:36 PM 
Big Oh Notation and Heaps
By gibsonrocker800 in forum Advanced JavaReplies: 8Last Post: 01252008, 11:06 PM
Bookmarks