(Infix to Postfix) The program should convert an infix expression to an equivalent postfix expression but does not because statements are missing from the function convertToPostfix. Use the following...


(Infix to Postfix) The program should convert an infix expression to an equivalent postfix expression but does not because statements are missing from the function convertToPostfix. Use the following conversion rules to help you restore the missing statements.


The rules to convert an infix expression into an equivalent postfix expression are as follows:

Suppose infx represents the infix expression an pfx represents the postfix expression. The rules to convert infx into pfx are as follows:




  1. Initialize pfx to an empty expression and also initialize the stack.




  2. Get the next sym, from infx.




    1. If sym is an operand, append sym to pfx.




    2. If sym is (, push sym into the stack




    3. If sym is ), pop and append all the symbols from the


      stack until the most recent left parenthesis. Pop and


      discard the left parenthesis.




    4. If sym is an operator:


      i. Pop and append all the operators from the stack to pfx that are above the most recent left parenthesis and have precedence greater than or equal to sym.


      ii. Push sym onto the stack






  3. After processing infx, some operators might be left in the


    stack. Pop and append to pfx everything from the stack.




In this program, you will consider the following (binary) arithmetic operators: +, -, *, and /. You may assume that the expressions you will process are error free.


Design a class that stores the infx and post fix strings. The class must include the following operatins:


• getInfix-storestheinfixexpression• showInfix-outputstheinfixexpression• showPostfix–outputsthepostfixexpression


Other operations that you might need are the following:

convertToPostifx – converts the infix expression into a postifx expression. The resulting postfix expression is stored in pfx.


precedence – determines the precedence between two operators. If the first operator is of higher or equal precedence than the second operator, it returns the value true; otherwise, it returns the value false.Include the constructors and destructors for automatic initialization and dynamic memory deallocation.


Test your program on the following five expressions:

A + B – C;(A + B) * C;(A + B) * (C – D);(A + ((B + C) * (E – F) – G) / (H – I); A + B * (C + D) – E / F * G + H;

Nov 14, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here