Write C++ code to convert a given infix expression to postfix expression.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 11SA
icon
Related questions
Question

Write C++ code to convert a given infix expression to postfix expression.

The steps and table below shows how infix to postfix conversion works in

practice .

1. Search the Infix string from left to right.

2. Initialize a vacant stack.

3. If the scanned character is an operand add it to the Postfix string.

4. When the scanned character is an operator and if the stack is empty push the

character to stack.

 

5. If a scanned character is an Operator and the stack is not empty, compare the

precedence of the character with element on top of the stack.

6. When top Stack has higher precedence over the scanned character pop the stack

else push the scanned character to stack. Repeat this procedure till the stack is not

empty and top Stack has precedence over the character.

7. Reiterate 4 and 5 steps till all characters are scanned.

8. After all characters are scanned, we have to add any character that the stack may

have to the Postfix string.

9. When stack is not empty add top Stack to Postfix string and Pop the stack.

Infix to Postfix Conversion
Infix Expression: (A/(B-C)*D+E)
Symbol Scanned
Stack
Output
A
A
A
A
B
АВ
(/-
AB
ABC
ABC-
(*
ABC-/
ABC-/D
ABC-/D*
ABC-/D*E
D
(*
(+
E
(+
Empty
ABC-/D*E+
Postfix Expression: ABC-/D*E+
Transcribed Image Text:Infix to Postfix Conversion Infix Expression: (A/(B-C)*D+E) Symbol Scanned Stack Output A A A A B АВ (/- AB ABC ABC- (* ABC-/ ABC-/D ABC-/D* ABC-/D*E D (* (+ E (+ Empty ABC-/D*E+ Postfix Expression: ABC-/D*E+
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr