📄 infix to postfix.cpp
字号:
/*************************************************************************/
/*************************************************************************
A C++ Program to convert an Infix Expression into a Postfix Expression.
*************************************************************************/
/*************************************************************************/
/*************************************************************************
By :
Muhammad Tahir Shahzad [ MTS ]
B.C.S Honours [ 2000-04 ]
Government College University Lahore
Pakistan
E-mail : mtshome@wol.net.pk
Web-Site : www.mts-home.cjb.net [ www.wol.net.pk/mtshome ]
www.mtshome.cjb.net [ www.geocities.com/mtahirshahzad ]
*************************************************************************/
/*************************************************************************/
/*************************************************************************/
//--------------------------- Header Files ----------------------------//
/*************************************************************************/
/*************************************************************************/
# include <iostream.h>
# include <string.h>
# include <stdlib.h>
# include <conio.h>
/*************************************************************************/
/*************************************************************************/
//-------------------------- Global Variables -------------------------//
/*************************************************************************/
/*************************************************************************/
int top=-1;
char Stack[100]={NULL};
/*************************************************************************/
/*************************************************************************/
//------------------------ Function Prototypes ------------------------//
/*************************************************************************/
/*************************************************************************/
void push(const char);
const char pop( );
void infix_to_postfix(const char *);
/*************************************************************************/
/*************************************************************************/
//------------------------------ main( ) ------------------------------//
/*************************************************************************/
/*************************************************************************/
int main( )
{
clrscr( );
char Infix_expression[100]={NULL};
cout<<"\n\n Enter the Infix Expression : ";
cin.getline(Infix_expression,80);
infix_to_postfix(Infix_expression);
getch( );
return 0;
}
/*************************************************************************/
/*************************************************************************/
//------------------------ Function Definitions -----------------------//
/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
//----------------------------- push(const char) ----------------------//
/*************************************************************************/
void push(const char Symbol)
{
if(top==99)
cout<<"Error : Stack is full."<<endl;
else
{
top++;
Stack[top]=Symbol;
}
}
/*************************************************************************/
//-------------------------------- pop( ) -----------------------------//
/*************************************************************************/
const char pop( )
{
char Symbol=NULL;
if(top==-1)
cout<<"Error : Stack is empty."<<endl;
else
{
Symbol=Stack[top];
Stack[top]=NULL;
top--;
}
return Symbol;
}
/*************************************************************************/
//--------------------- infix_to_postfix(const char *) ----------------//
/*************************************************************************/
void infix_to_postfix(const char *Infix)
{
char Infix_expression[100]={NULL};
char Postfix_expression[100]={NULL};
strcpy(Infix_expression,"(");
strcat(Infix_expression,Infix);
strcat(Infix_expression,")");
char Symbol[5]={NULL};
char Temp[5]={NULL};
for(int count=0;count<strlen(Infix_expression);count++)
{
Symbol[0]=Infix_expression[count];
if(Symbol[0]=='(')
push(Symbol[0]);
else if(Symbol[0]==')')
{
Symbol[0]=pop( );
while(Symbol[0]!='(')
{
strcat(Postfix_expression,Symbol);
Symbol[0]=pop( );
}
}
else if(Symbol[0]=='^' || Symbol[0]=='*' || Symbol[0]=='/'
|| Symbol[0]=='+' || Symbol[0]=='-')
{
if(Symbol[0]=='*' || Symbol[0]=='/')
{
Temp[0]=pop( );
while(Temp[0]=='^' || Temp[0]=='*' || Temp[0]=='/')
{
strcat(Postfix_expression,Temp);
Temp[0]=pop( );
}
push(Temp[0]);
}
else if(Symbol[0]=='+' || Symbol[0]=='-')
{
Temp[0]=pop( );
while(Temp[0]!='(')
{
strcat(Postfix_expression,Temp);
Temp[0]=pop( );
}
push(Temp[0]);
}
push(Symbol[0]);
}
else
strcat(Postfix_expression,Symbol);
}
cout<<"\n\n Postfix Expression : "<<Postfix_expression<<endl;
}
/*************************************************************************/
/*************************************************************************/
//----------------------------- THE END -------------------------------//
/*************************************************************************/
/*************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -