As advertised before, HW2 is to implement infix to postfix conversion in C/C++.
ID: 3771748 • Letter: A
Question
As advertised before, HW2 is to implement infix to postfix conversion in C/C++. You can directly use your stack class that you implemented in Lab 8. Please search the algorithm how to convert infix to postfix using stack and implement it. You should submit a document that describes the conversion algorithm that you used as well as the source code. In the program, you have to ask a user to type infix expression, then the program will display its postfix expression. How to submit: 1. Please submit source code and a document that describes the conversion algorithm to eCollege.
Explanation / Answer
Answer:
# include <iostream>
# include <string.h>
# include <cstdlib>
using namespace std;
//structure declaration
struct nodeV
{
//variable declarations
char dataSce;
nodeV *next;
};
//node value specifications
nodeV *topVal=NULL;
nodeV *bottomVal=NULL;
nodeV *entryVal;
nodeV *last_entryOf;
nodeV *second_last_entryOf;
//method declaration
void pushMethod(const char);
const char popMethod( );
void infix_to_postfixConversion(const char *);
//method main
int main( )
{
//variable declaration
char Infix_expressionOf[100]={NULL};
cout<<" Enter the Infix Expression : ";
cin>>Infix_expressionOf;
infix_to_postfixConversion(Infix_expressionOf);
system("pause");
return 0;
}
//defining the method push
void pushMethod(const char SymbolVal)
{
entryVal=new nodeV;
if(bottomVal==NULL)
{
entryVal->dataSce=SymbolVal;
entryVal->next=NULL;
bottomVal=entryVal;
topVal=entryVal;
}
else
{
entryVal->dataSce=SymbolVal;
entryVal->next=NULL;
topVal->next=entryVal;
topVal=entryVal;
}
}
//defining the method pop
const char popMethod( )
{
char SymbolVal=NULL;
if(bottomVal==NULL)
cout<<" *** Error : Stack is empty. "<<endl;
else
{
for(last_entryOf=bottomVal;last_entryOf->next!=NULL;
last_entryOf=last_entryOf->next)
second_last_entryOf=last_entryOf;
if(topVal==bottomVal)
bottomVal=NULL;
SymbolVal=topVal->dataSce;
delete topVal;
topVal=second_last_entryOf;
topVal->next=NULL;
}
return SymbolVal;
}
//defining the conversion method
void infix_to_postfixConversion(const char *InfixCal)
{
char Infix_expressionValue[100]={NULL};
char Postfix_expressionValue[100]={NULL};
strcpy(Infix_expressionValue,"(");
strcat(Infix_expressionValue,InfixCal);
strcat(Infix_expressionValue,")");
char SymbolVal[5]={NULL};
char Tempo[5]={NULL};
for(int count1=0;count1<<strlen(Infix_expressionValue);count1++)
{
SymbolVal[0]=Infix_expressionValue[count1];
if(SymbolVal[0]=='(')
pushMethod(SymbolVal[0]);
else if(SymbolVal[0]==')')
{
SymbolVal[0]=popMethod( );
while(SymbolVal[0]!='(')
{
strcat(Postfix_expressionValue,SymbolVal);
SymbolVal[0]=popMethod( );
}
}
else if(SymbolVal[0]=='^' || SymbolVal[0]=='*' || SymbolVal[0]=='/'
|| SymbolVal[0]=='+' || SymbolVal[0]=='-')
{
if(SymbolVal[0]=='*' || SymbolVal[0]=='/')
{
Tempo[0]=popMethod( );
while(Tempo[0]=='^' || Tempo[0]=='*' || Tempo[0]=='/')
{
strcat(Postfix_expressionValue,Tempo);
Tempo[0]=popMethod( );
}
pushMethod(Tempo[0]);
}
else if(SymbolVal[0]=='+' || SymbolVal[0]=='-')
{
Tempo[0]=popMethod( );
while(Tempo[0]!='(')
{
strcat(Postfix_expressionValue,Tempo);
Tempo[0]=popMethod( );
}
pushMethod(Tempo[0]);
}
pushMethod(SymbolVal[0]);
}
else
strcat(Postfix_expressionValue,SymbolVal);
}
cout<<" Postfix Expression : "<<Postfix_expressionValue;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.