Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

programming language c++ Input/Output An error free in occur between any two symbols in an expression. A symbol may be an opera letter), an operator(+,,or

programming language c++ image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Input/Output An error free in occur between any two symbols in an expression. A symbol may be an opera letter), an operator(+,,or ), a left parenthesis, or a right parenthesis. fix arithmetic expression is given from keyboard. An arbitrary number of blanks may nd (a single upper-case Page 1 of 5 Sample Input/Output: Enter infix expression: A+ B-C The postfix expression is: AB+C- Enter infix expression: ((A+B)/ (C-D)+E)/(F+G) The postfix expression is: AB+CD-E+FG+/ Discussion In converting infix expressions to postfix notation, the following fact should be taken into consideration: in infix form the order of applying operators is governed by the possible appearance of parentheses and the operator precedence relations; however, in postfix form the order is simply the "natural" order i.e., the order of appearance from left to right. Accordingly, subexpressions within innermost parentheses must first be converted to postfix, so that they can then be treated as single operands. In this fashion, parentheses can be successively eliminated until the entire expression has been converted. The last pair of parentheses to be opened within a group of nested parentheses encloses the first subexpression within that group to be transformed. This last-in, first-out behavior should immediately suggest the use of a stack. Your program may utilize any of the operations in the Stack ADT Algorithm: InfixToPostfix Input: Infix expression I, a temporary empty stack S Output: Postfix expression P (which is initially empty) corresponding to the Infix expression I 1. for each symbol in I from left to right do 2. if the symbol is an operand then 3. Append the symbol to P 4. else-If the'symbol is an operator then 5. Pop every operator on S that is above the most recently scanned left parenthesis and has precedence higher than or equal to that of the new operator symbol, and then append to P 6. Push the new operator symbol ontoS 7. else-If the symbol is an opening (left) parenthesis then 8. Push the symbol onto S 9. else-If the symbol is a closing (right) parenthesis then 10. all operators down to the most recently scanned left parenthesis are be popped from S and appended to P 11. discard this pair of parentheses 12. end if 13. end for 14. Pop all operators from S and append to P Data Structure You may use any stack implementation. Page 2 of 5 Examples Here are two examples to help you understand how the algorithm works. Each line on the following table demonstrates the state of the postfix string and the stack when the corresponding next infix symbol is scanned. Example 1: Input expression is A+B C/D-E Symbol Postfix Expression Temporary Stack A B A B ABC ABC ABC D ABC D/+ "D/+E ABC DIE- Example 2: Input expression is (A+B (C-D))/E Symbol Postfix Expression Temporary Stack A B A B A B ABC ABC ABCD ABCD ABCD-*+ ABCD- ABCD-*+E

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

Students also viewed these Databases questions