I need a complete working code for a solution to this proble
I need a complete working code for a solution to this problem using visual studio 2015:
C++ Programming (7th Edition)
Chapter 17, Problem 10PE
Solution
Ans:
#include <iostream>
using namespace std;
class InfixToPostfix
{
private:char *p;//for infix expression
char *q;//for postfix expression
char *stack; //stack array
int top; //top
public: InfixToPostfix();//constructor
void getInfix();//ro read infix expression
void showInfix();//to display infix expression
void showPostfix();//to display postfix expression
void push(char ele);//to push elements into the stack
char pop();//to pop elements from stack
void convertToPostfix();//to convert infix to postfix
int precedence(char);//to find precedence of an operator
~InfixToPostfix();//destructor
};
InfixToPostfix::InfixToPostfix(void)
{
top=-1;
p=new char[100];
q=new char[100];
stack=new char[10];
}
void InfixToPostfix::getInfix(){
cout<<\"enter infix expression:\";
cin>>p;
}
void InfixToPostfix::showInfix(){
cout<<\"\ Given Infix Expression: \"<<p<<\"\ \";
}
void InfixToPostfix::convertToPostfix(){
int i,j;
char ch;//to get next symbol
j=0;
for(i=0;*(p+i)!=\'\\0\';i++)
{
if(isalnum(*(p+i)))//if it is an operand
{
q[j]=*(p+i);
j++;
}
else if(*(p+i)==\'(\')//if it is a left brace
push(*(p+i));
else if(*(p+i)==\')\')//if it is a right brace
{
while((ch=pop())!=\'(\')
{
q[j]=ch;j++;
}
}//end of else if
else //if it is an operator
{
while(precedence(stack[top])>=precedence(*(p+i)) && top!=-1)
{
ch=pop();
q[j]=ch;j++;
}//end of while
push(*(p+i));//push next symbol into the stack
} //end of else
}//end of for loop
while(top!=-1)
{
ch=pop();
q[j]=ch;j++;
}
q[j]=\'\\0\';
}//end of convertToPostfix()
void InfixToPostfix::showPostfix() //to display postfix expression
{
cout<<\"Postfix Expression: \"<<q;
}
void InfixToPostfix::push(char ele) //push function
{
if(top==9)
{
cout<<\"overflow\";
}
else
{
top++;stack[top]=ele;
}
}//end of push function
char InfixToPostfix::pop() //pop function
{
if(top==-1)
{
cout<<\"underflow\";return -1;
}
else
{
char ch=stack[top];
top--;
return ch;
}
} //end of pop()
int InfixToPostfix::precedence(char ele) //to find precedence
{
switch(ele)
{
case \'+\':
case \'-\':return 1;
case \'*\':
case \'/\':return 2;
default: return 0;
}
} //end of precedence
InfixToPostfix :: ~InfixToPostfix()//destructor
{
delete []p;
delete []q;
delete []stack;
}
int main()
{
InfixToPostfix r;
r.getInfix();
r.showInfix();
r.convertToPostfix();
r.showPostfix();
return 0;
}//end of main


