in c++ please help me witht his Write a program to convert an inx expression int
ID: 3692982 • Letter: I
Question
in c++ please help me witht his Write a program to convert an inx expression into an equivalent postx expression. (Inx to Postx) Write a program that converts an inx expression into an equivalent postx expression. The rules to convert an inx expression into an equivalent postx expression are as follows: Suppose infx represents the inx expression and pfx represents the postx expression. The rules to convert infx into pfx are as follows: a. Initialize pfx to an empty expression and also initialize the stack. b. Get the next symbol, sym, from infx. If sym is an operand, append sym to pfx. If sym is (, push sym into the stack. 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. If sym is an operator: · 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. · Push sym onto the stack. c. 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 inx and postx strings. The class must include the following operations: * getInx: Stores the inx expression. * showInx: Outputs the inx expression. * showPostx: Outputs the postx expression. Some other operations that you might need are: * convertToPostx: Converts the inx expression into a postx expression. The resulting postx expression is stored in pfx. * precedence: Determines the precedence between two operators. If the rst 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 expressions: a. A + B - C; b. (A + B ) * C; c. (A + B) * (C - D); d. A + ((B + C) * (E - F) - G) / (H - I); e. A + B * (C + D ) - E / F * G + H; For each expression, your answer must be in the following form: Inx Expression: A + B - C; Postx Expression: A B + C —————x———— End ————x————– in c++
Explanation / Answer
Hi,
CODE:'
#include<iostream.h>
#include<conio.h>
class stck
{
public:
char stck_array[50];
int top;
stck()
{
top=-1;
}
void push(char smbl)
{
if(full())
{
cout<<" Stack overflow: ";
}
else
{
top=top+1;
stck_array[top]=smbl;
}
}
char pop()
{
if(empty())
return('#'); // Return value '#' indicates stck is empty
else
return(stck_array[top--]);
}
int empty()
{
if(top==-1)
return(1);
else
return(0);
}
int full()
{
if(top==49)
return(1);
else
return(0);
}
};
class Expression
{
char infix[50];
char postfix[50];
public:
void read()
{
cout<<" Enter an infix espression:";
cin>>infix;
}
int white_space(char smbl)
{
if(smbl==' ' || smbl==' ' || smbl=='')
return 1;
else
return 0;
}
void cnvrtPosfix()
{
stck s;
int l,precedence,p;
char entry1,entry2;
p=0;
for(int i=0;infix[i]!='';i++)
{
entry1=infix[i];
if(!white_space(entry1))
{
switch(entry1)
{
case '(':
s.push(entry1);
break;
case ')':
while((entry2=s.pop())!='(')
postfix[p++]=entry2;
break;
case '+':
case '-':
case '*':
case '/':
if(!s.empty())
{
precedence=prec(entry1);
entry2=s.pop();
while(precedence<=prec(entry2))
{
postfix[p++]=entry2;
if(!s.empty())
entry2=s.pop();
else
break;
}
if(precedence>prec(entry2))
s.push(entry2);
}
s.push(entry1);
break;
default:
postfix[p++]=entry1;
break;
}
}
}
while(!s.empty()) //while stck is not empty
postfix[p++]=s.pop();
postfix[p]='';
cout<<" The postfix espression is: "<<postfix<<endl;
}
int prec(char smbl)
{
switch(smbl)
{
case '/': return(4); // Precedence of / is 4
case '*': return(3); // Precedence of * is 3
case '+': return(2); // Precedence of + is 2
case '-': return(1); // Precedence of - is 1
case '(': return(0); // Precedence of ( is 0
default: return(-1);
}
}
};
void main()
{
char choice='y';
Expression espr;
while(choice=='y')
{
espr.read();
espr.cnvrtPosfix();
cout<<" Do you want to continue ? (y/n): ";
cin>>choice;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.