Question

In: Computer Science

Show, step by step, how the stack-based algorithm will transform the expression (1 + 2) *...

Show, step by step, how the stack-based algorithm will transform the expression (1 + 2) * (7 − 2) into a postfix expression, and then how a second stack-based algorithm will compute the value of this postfix expression.

Solutions

Expert Solution

Answer:

Token #1 Evaluation

Infix: (1+2)*(7-2)

Since ( is an opening parenthesis, push it to the top of the stack.

Postfix:

Stack:

(

Token #2 Evaluation

Infix: (1+2)*(7-2)

Since 1 is an operand, append it to the postfix expression.

Postfix: 1

Stack:

(

Token #3 Evaluation

Infix: (1+2)*(7-2)

Since + is an operator and has greater precedence than the ( on the top of the stack, push + to the top of the stack.

Postfix: 1

Stack:

+
(

Token #4 Evaluation

Infix: (1+2)*(7-2)

Since 2 is an operand, append it to the postfix expression.

Postfix: 1 2

Stack:

+
(

Token #5 Evaluation

Infix: (1+2)*(7-2)

Since the ) is a closing parenthesis, pop each operator from the stack one at a time and append to the postfix expression. Keep popping from the stack until an opening parenthesis is encountered.

Pop + from the top of the stack and append to the postfix expression.

Postfix: 1 2 +

Stack:

(

Since we are done with the expression inside the current parenthesis, pop the opening parenthesis from the top of the stack and discard.

Postfix: 1 2 +

Stack:

Token #6 Evaluation

Infix: (1+2)*(7-2)

Since the * is an operator and the stack is empty, push the * to the stack.

Postfix: 1 2 +

Stack:

*

Token #7 Evaluation

Infix: (1+2)*(7-2)

Since ( is an opening parenthesis, push it to the top of the stack.

Postfix: 1 2 +

Stack:

(
*

Token #8 Evaluation

Infix: (1+2)*(7-2)

Since 7 is an operand, append it to the postfix expression.

Postfix: 1 2 + 7

Stack:

(
*

Token #9 Evaluation

Infix: (1+2)*(7-2)

Since - is an operator and has greater precedence than the ( on the top of the stack, push - to the top of the stack.

Postfix: 1 2 + 7

Stack:

-
(
*

Token #10 Evaluation

Infix: (1+2)*(7-2)

Since 2 is an operand, append it to the postfix expression.

Postfix: 1 2 + 7 2

Stack:

-
(
*

Token #11 Evaluation

Infix: (1+2)*(7-2)

Since the ) is a closing parenthesis, pop each operator from the stack one at a time and append to the postfix expression. Keep popping from the stack until an opening parenthesis is encountered.

Pop - from the top of the stack and append to the postfix expression.

Postfix: 1 2 + 7 2 -

Stack:

(
*

Since we are done with the expression inside the current parenthesis, pop the opening parenthesis from the top of the stack and discard.

Postfix: 1 2 + 7 2 -

Stack:

*

All 11 Token Evaluations Completed

Since we are done processing the infix expression, pop the remaining operator from top of the stack and append it to the postfix expression.

Pop * from the stack and append it to the postfix expression.

Postfix: 1 2 + 7 2 - *

Stack:

Final Conversion:

Infix: (1+2)*(7-2)
to
Postfix: 1 2 + 7 2 - *

//if you have any doubt please comment ....


Related Solutions

GIVEN THE FOLLOWING PSOSTFIX EXPRESSION, SHOW TO USE A STACK TO EVALUATE ITS FINAL VALUE. SHOW...
GIVEN THE FOLLOWING PSOSTFIX EXPRESSION, SHOW TO USE A STACK TO EVALUATE ITS FINAL VALUE. SHOW STACK CONFIGURATION DETAILS 20 3 18 8 - * 10 + +10 12 - /
Find is the final result of evaluating the following postfix expression using a stack. Show each...
Find is the final result of evaluating the following postfix expression using a stack. Show each push and pop operation. 85 5 / 4 * 5   6 +   10    5 -   * +
1 Draw a stack diagram to show how the following code is executed and write the...
1 Draw a stack diagram to show how the following code is executed and write the generated output. def sequence(a, b, c): if a < b < c: return a + b + c if a >= b: return sequence(a - 1, b, c) if a >= c: return sequence(c, b, a) if b >= c: return sequence(c, b, a + 2) return 0 print(sequence(10, 10, 10)) 2 Draw a stack diagram to show how the following code is executed...
Starting from a symbolic expression of the Second Law of Thermodynamics, show how arguments based purely...
Starting from a symbolic expression of the Second Law of Thermodynamics, show how arguments based purely upon considerations of entropy lead to the formalism which we call Gibbs
Group Project Step 1: Select any four sorting algorithm and two searching algorithms Step 2: Understand...
Group Project Step 1: Select any four sorting algorithm and two searching algorithms Step 2: Understand the logic of all the algorithms Step 3: Create java program and use your sorting/searching source codes and integrate it into your main java project. Step 4: Create a separate java class for each algorithm Step 5: Create a random function that generates at least 100000 random integer numbers from 1 to 1 million(No need to print out or store the numbers) Step 6:...
(show step by step calculation and formula, no excel or fina calculator) Based on the below...
(show step by step calculation and formula, no excel or fina calculator) Based on the below cash flows, HappyBear Ltd. uses the NPV decision rule. At a required return of 12 percent, should the firm accept this project? What if the required return is 30 percent? Year Cash Flow 0 -$32,000 1 16,000 2 20,000 3 17,000
Step 1: Capturing the input The first step is to write a functionGetInput()that inputs the expression...
Step 1: Capturing the input The first step is to write a functionGetInput()that inputs the expression from the keyboard and returns the tokens---the operands and operators---in a queue. You must write this function. To make the input easier to process, the operands and operators will be separated by one or more spaces, and the expression will be followed by #. For example, here’s a valid input to your program: 6 4 15 * – # Back in Codio, modify “main.cpp”...
What is TimSort and how does it work? TimSort example with step by step explanation. Algorithm...
What is TimSort and how does it work? TimSort example with step by step explanation. Algorithm analysis of Timsort with python implementation, including pseudo code and T(n) time complexity analysis
1) How i measure the chromatography step by step? 2) How i measure the Spectrscopy step...
1) How i measure the chromatography step by step? 2) How i measure the Spectrscopy step by step?
1) Use insertion sort to sort: 25, 17, 31, 13, 2. Show your work step-by-step. 2)...
1) Use insertion sort to sort: 25, 17, 31, 13, 2. Show your work step-by-step. 2) Use Euclidean algorithm to find gcd(248, 198) 3) (ABCDEF)16 to binary, octal and decimal
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT