Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A reverse polish notation (RPN) calculator uses a stack. The calculator works as

ID: 3873428 • Letter: A

Question

A reverse polish notation (RPN) calculator uses a stack. The calculator works as follows: Entering a number puts it on top of the stack and entering an operation op causes the topmost number x and second topmost number y to be popped off the stack and the number y op x to be pushed on the stack. Thus an operation zopy is entered as ryop. For example 1 +2 is computed with "1 2 +" and (2 * 3)-4 is computed with "2 3 * 4-” How do you calculate the following expressions with a RPN calculator? a) 1+3+5-7 d) (3+4) (20 (3* 4+2)) c) 3 (2+4 3)

Explanation / Answer

Hi,
The working of the calculator is given, there are 2 cases while parsing the input,if input is
1. operator- pop top two elements and perform the operation and push back result to stack
2. number- push it to stack
hence using this, we can calculate the given expressions
a, 1+3+5-7, here since no brackets, the order of evaluation doesnt matter, hence we can parse1 3 + 5 + 7 -

b. (6-3)*2+1- here we have to follow the order of brackets hence 6 3 - 2 * 1 + i.e fist subtract then multiply by 2 and finally add 1
c. 3*(2+4*3)- similar to above problem, first the inner brackets need to be solved, 3 2 4 3 * + *
d. (3+4) *(20-(3*4+2))- smilarly here, first the 3+4 bracket, then 3*4+2 bracket then the two big brackets hence
3 4 + 20 3 4 * 2 + - *
e. 5*(3+4)-(2*(2+2*(1+2)))-
this is a little more complex, but well bracketed, hence we keep adding numbers until we get the operator to resolve them according to brackets indicated ,hence
5 3 4 + * 2 2 2 1 2 + * + * -

Thumbs up if this was helpful, otherwsie let me know in comments

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote