Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is infix to postfix program. My request is to put comments on the codes. Th

ID: 3738109 • Letter: T

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. Thanks

#include <iostream>

#include<ctype.h>

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<<endl<<" Given Infix Expression: "<<in<<endl;

}

void Postfix::convertToPostfix()

{

int i,j;

char ch;

j=0;

for(i=0;*(in+i)!='';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]='';

}

void Postfix::showpostfix()

{

cout<<"Postfix Expression: "<<p<<endl;

}

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;

}

Explanation / Answer

// C++ code with comments

#include <iostream>

#include<ctype.h>

using namespace std;

class Postfix

{

private:

// private variables

// in is the char pointer for infix  

char *in;

// p is the char pointer for postfix  

char *p;

// st is the intermediate char pointer for conversion

char *st;

// temporary variable

int t;

// public methods

public:

// constructor

Postfix();

// function to get infix expression

void getinfix();

// function to display infix expression

void showinfix();

// function to display postfix expression

void showpostfix();

void a(char e);

char b();

// function to convert infix to postfix expression

void convertToPostfix();

int pre(char);

~Postfix();

};

// constructor initializing variables

Postfix::Postfix(void)

{

// temp variable with -1 and other array with array elements 50

t=-1;

in=new char[50];

p=new char[50];

st=new char[10];

}

// function to get infix expression from user

void Postfix::getinfix()

{

cout<<"infix expression :";

cin>>in;

}

// function to display infix expression to user

void Postfix::showinfix()

{

cout<<endl<<" Given Infix Expression: "<<in<<endl;

}

// function to convert infix to postfix

void Postfix::convertToPostfix()

{

int i,j;

char ch;

j=0;

// iterate over the infix expressio using pointer

for(i=0;*(in+i)!='';i++)

{

// check if infix character is special character

if(isalnum(*(in+i)))

{

// add it to postfix

p[j]=*(in+i);

j++;

}

// else if open bracket is found go further

else if(*(in+i)=='(')

a(*(in+i));

// else if close bracket is found

else if(*(in+i)==')')

{

// check for open bracket using function b

while((ch=b())!='(')

{

// add it to postfix expression

p[j]=ch;

j++;

}

}

// else

else

{

// check if char at index in+i in pre is less than t idx in st of pre and t is not -1

while(pre(*(in+i))<=pre(st[t]) && t!=-1)

{

// find ch using method b

ch=b();

// add it to postifx

p[j]=ch;j++;

}

a(*(in+i));

}

}

// while temp is not -1

while(t!=-1)

{

// find ch using method b

ch=b();

// add it to postifx

p[j]=ch;j++;

}

p[j]='';

}

// function to show user postfix expression

void Postfix::showpostfix()

{

cout<<"Postfix Expression: "<<p<<endl;

}

// functipn to check overlflow

void Postfix::a(char e)

{

if(t==9)

{

cout<<"overflow";

}

else

{

t++;

st[t]=e;

}

}

// function check underflow

char Postfix::b()

{

if(t==-1)

{

cout<<"underflow";return -1;

}

else

{

char ch=st[t];

t--;

return ch;

}

}

// function to check operators

int Postfix::pre(char e)

{

switch(e)

{

case '+':

case '-':return 1;

case '*':

case '/':return 2;

default: return 0;

}

}

// destructor to delete all character arrays

Postfix :: ~Postfix()

{

delete []in;

delete []p;

delete []st;

}

// main function get infix from user, convert to postfix and display to user

int main()

{

Postfix obj;

obj.getinfix();

obj.convertToPostfix();

obj.showinfix();

obj.showpostfix();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote