Question: This is infix to postfix program. My request is to put comments on the codes. The purpose is so I can understand the code better.
This is infix to postfix program. My request is to put comments on the codes. The purpose is so I can understand the code better. Thanks
#include
#include
using namespace std;
class Postfix
{
private:
char *in;
char *p;
char *st;
int t;
public:
Postfix();
void getinfix();
void showinfix();
void showpostfix();
void a(char e);
char b();
void convertToPostfix();
int pre(char);
~Postfix();
};
Postfix::Postfix(void)
{
t=-1;
in=new char[50];
p=new char[50];
st=new char[10];
}
void Postfix::getinfix()
{
cout<<"infix expression :";
cin>>in;
}
void Postfix::showinfix()
{
cout< } void Postfix::convertToPostfix() { int i,j; char ch; j=0; for(i=0;*(in+i)!='\0';i++) { if(isalnum(*(in+i))) { p[j]=*(in+i); j++; } else if(*(in+i)=='(') a(*(in+i)); else if(*(in+i)==')') { while((ch=b())!='(') { p[j]=ch; j++; } } else { while(pre(*(in+i))<=pre(st[t]) && t!=-1) { ch=b(); p[j]=ch;j++; } a(*(in+i)); } } while(t!=-1) { ch=b(); p[j]=ch;j++; } p[j]='\0'; } void Postfix::showpostfix() { cout<<"Postfix Expression: "< } void Postfix::a(char e) { if(t==9) { cout<<"overflow"; } else { t++; st[t]=e; } } char Postfix::b() { if(t==-1) { cout<<"underflow";return -1; } else { char ch=st[t]; t--; return ch; } } int Postfix::pre(char e) { switch(e) { case '+': case '-':return 1; case '*': case '/':return 2; default: return 0; } } Postfix :: ~Postfix() { delete []in; delete []p; delete []st; } int main() { Postfix obj; obj.getinfix(); obj.convertToPostfix(); obj.showinfix(); obj.showpostfix(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
