Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We can use the postfix (Reverse Polish) calculator to evaluate infix expressions, once we convert an infix to its postfix form. To convert an infix

We can use the postfix ("Reverse Polish") calculator to evaluate infix expressions, once we convert an infix to its postfix form.

To convert an infix expression to postfix form, we scan the infix expression from left to right.

When we encounter an operand, we place it at the end of the new expression that we are creating Operands in an infix expression remain in the same order in the corresponding postfix expression.

When we encounter an operator, we must save it until we determine where in the output expression it belongs. For example, to convert the infix expression a + b, we append a to the initially empty output expression, save +, and append b to the output expression. We now need to retrieve the + and put it at the end of the output expression to get the postfix expression a b +. Retrieving the operator saved most recently is easy if we have saved it in a stack. In this example, we saved the operator until we processed its second operand.

In general, we hold the operator in a stack at least until we compare its precedence with that of the next operator. For example, to convert the expression a + b * c, we append a to the output expression, push + onto a stack, and then append b to the output. What we do now depends on the relative precedences of the next operator, *, and the + at the top of the stack. Since * has a greater precedence than +, b is not the additions second operand. Instead, the addition waits for the result of the multiplication. Thus, we push * onto the stack and append c to the output expression.

Having reached the end of the input expression, we now pop each operator from the stack and append it to the end of the output expression, getting the postfix expression a b c * +. The figure shown here illustrates these steps. The stack is shown horizontally; the leftmost element is at the bottom of the stack.

What if two successive operators have the same precedence? For example, consider the expression a - b + c. When we encounter the +, the stack will contain the operator - and the incomplete postfix expression will be ab. The subtraction operator belongs to the operands a and b, so we pop the stack and append - to the end of the expression ab. Since the stack is empty, we push the + onto the stack. We then append c to the result, and finally we pop the stack and append the +. The result is a b - c +.

image text in transcribed

Converting an infix expression to postfix form: a - b + c Next Character in Infix Expression Postfix Form Operator Stack (bottom to top) a b ab -c ab - c+

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands On Database

Authors: Steve Conger

1st Edition

013610827X, 978-0136108276

More Books

Students also viewed these Databases questions