# Help! URGENT.. i need to be able to evaluate PEMDAS expressions using stacks..

• 04-01-2009, 07:58 AM
a_aagain
Help! URGENT.. i need to be able to evaluate PEMDAS expressions using stacks..
i need to use java for this.. i must evaluate a PEMDAS expression using stacks..

The ff are valid expressions:

1. (40 + 5 * 2^2)*10+2 Ans: 602
2. 2^-2+1 and 2^(-2)+1 Ans: 1.25
3. 4 + 5 * 3 + -1 and 4 + 5 * 3 + (-1) Ans: 18
4. 2^2^3*10 and 2^(2^3)*10 Ans: 2560 *Exponentiation is
right-associative
5. (2^2)^3*10 Ans: 640
6. -4 + 2 - -8 * -3 and (-4) + 2 - (-8) * (-3) Ans: -18
7. 5 / -1 * (6 + 1) and (5) / (-1) * (6 + 1) Ans: -35
8. 10 - 5 - 2 - 1 and (((10-5)-2)-1) Ans: 2
*Subtraction is
left-associative
9. 3 / 3 / 3 * 100 and ((3 / 3) / 3) * 100) Ans: 33.33
*Division is left-associative
10. 5 - 3 * +2 Ans: -1

while the ff are invalid:

1. 5 + 3 % 2
2. (9 * 6 + 8)^2)
3. 5 +^ 3
4. 5 ^/ 3
5. 5 + ( * 4
6. a + 5
7. (5-7)(68)
8. 3 - 5 +

please can anyone help me..? i badly need it.. a lot is at stake if i won't be able to do this..
• 04-01-2009, 08:33 AM
pbrockway2
Quote:

a lot is at stake if i won't be able to do this..
But you evidently can't do this. The fact the millions of others can - including some reading this forum - does not in the least change the fact that you cannot.

If you find this state of affairs unpalatable, change it. Read your textbooks and notes and write some code. You may surprise yourself and be able to do the work that was assigned to you. But if not, you will at least have a question (possibly more) to ask.
• 04-01-2009, 08:40 AM
Eranga
First of all you should looking into stack implementation.
• 04-01-2009, 10:27 AM
mtyoung
so, what is your problem?

Code:

```(40 + 5 * 2^2)*10+2 Ans: 602  is valid 5 + 3 % 2  is invalid```
correct
• 04-01-2009, 12:25 PM
Eranga
I think he's looking at how the stack behave in Java. That's why I said, he should looking how the stack implemented, basically how it constructed. Anyway, we need more details about the question.