In: Computer Science
USING C++
Study the scenario and complete the question(s) that follow:
Postfix using Stacks
The rules to convert an infix expression into an equivalent postfix expression are as follows:
Suppose infx represents the infix expression and 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 symbol, sym, from infx.
a. If sym is an operand, append sym to pfx.
b. If sym is (, push sym into the stack.
c. If sym is ), pop and append all of the symbols from the stack until the most recent left parentheses. Pop and discard the left parentheses.
d. If sym is an operator:
i. Pop and append all of the operators from the stack to pfx that are above the most recent left parentheses 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.
Question:
Write a program that converts an infix expression into an equivalent postfix expression. Design a class that stores the infix and postfix strings. The class must include The following operations:
getInfix: Stores the infix expression.
showInfix: Outputs the infix expression.
showPostfix: Outputs the postfix expression.
Some other operations that you might need are:
convertToPostfix: Converts the infix expression into a postfix 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 initialisation and dynamic memory deallocation.
Test your program on the following 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;
For each expression, your answer must be in the following form:
Infix Expression : A + B - C ;
Postfix Expression: A B + C –
UISING C++
AND
AND
AND
# include <iostream.h> # include <string.h> # include <stdlib.h> # include <conio.h> int top=-1; char Stack[100]={NULL}; void push(constchar); constchar pop( ); void infix_to_postfix(constchar *); int main( ) { clrscr( ); char Infix_expression[100]={NULL}; cout<<"\n\n Enter the Infix Expression : "; cin.getline(Infix_expression,80); infix_to_postfix(Infix_expression); getch( ); return 0; } /*************************************************************************///----------------------------- push(const char) ----------------------///*************************************************************************/void push(constchar Symbol) { if(top==99) cout<<"Error : Stack is full."<<endl; else { top++; Stack[top]=Symbol; } } /*************************************************************************///-------------------------------- pop( ) -----------------------------///*************************************************************************/constchar pop( ) { char Symbol=NULL; if(top==-1) cout<<"Error : Stack is empty."<<endl; else { Symbol=Stack[top]; Stack[top]=NULL; top--; } return Symbol; } /*************************************************************************///--------------------- infix_to_postfix(const char *) ----------------///*************************************************************************/void infix_to_postfix(constchar *Infix) { char Infix_expression[100]={NULL}; char Postfix_expression[100]={NULL}; strcpy(Infix_expression,"("); strcat(Infix_expression,Infix); strcat(Infix_expression,")"); char Symbol[5]={NULL}; char Temp[5]={NULL}; for(int count=0;count<strlen(Infix_expression);count++) { Symbol[0]=Infix_expression[count]; if(Symbol[0]=='(') push(Symbol[0]); elseif(Symbol[0]==')') { Symbol[0]=pop( ); while(Symbol[0]!='(') { strcat(Postfix_expression,Symbol); Symbol[0]=pop( ); } } elseif(Symbol[0]=='^' || Symbol[0]=='*' || Symbol[0]=='/' || Symbol[0]=='+' || Symbol[0]=='-') { if(Symbol[0]=='*' || Symbol[0]=='/') { Temp[0]=pop( ); while(Temp[0]=='^' || Temp[0]=='*' || Temp[0]=='/') { strcat(Postfix_expression,Temp); Temp[0]=pop( ); } push(Temp[0]); } elseif(Symbol[0]=='+' || Symbol[0]=='-') { Temp[0]=pop( ); while(Temp[0]!='(') { strcat(Postfix_expression,Temp); Temp[0]=pop( ); } push(Temp[0]); } push(Symbol[0]); } else strcat(Postfix_expression,Symbol); } cout<<"\n\n Postfix Expression : "<<Postfix_expression<<endl; }