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

1. Member
Join Date
Apr 2009
Posts
1
Rep Power
0

## 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..

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
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.

3. Senior Member
Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
9

Java Code:
```(40 + 5 * 2^2)*10+2 Ans: 602  is valid
5 + 3 % 2  is invalid```
correct
Last edited by mtyoung; 04-01-2009 at 11:28 AM.

4. 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.

#### Posting Permissions

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