Reverse Polish Notation——标程
//
Copyright by abilitytao
// All right not reserved!!
#include < process.h >
#include < iostream.h >
#include < conio.h >
#include < stdlib.h >
#include < math.h >
#include < stack >
#include < cstring >
#include < ctype.h >
using namespace std;
struct formula
{
char numormark[100];
} ;
/**/ ///////////////////////////////////////////////////////////
/////////////////////定义problem类/////////////////////////
///////////////////////////////////////////////////////////
class Aeasyproblem
{
private:
int formulalen;
char temp[1000];
formula in[1000];
formula pos[1000];
public:
void inputtheformula();
void intoin();//用于将中缀表达式放入结构体数组,从而实现数字与运算符的分离;
void intopos();//用于将中缀表达式转化成后缀表达式;
void showthepos();
} ;
void Aeasyproblem::inputtheformula()
{
cin>>temp;
}
void Aeasyproblem::intoin() // 这是用于解析输入的字符串的函数,将算式中的每个个体存入自己声明的结构体中;
{
formulalen=0;
int templen;
int temlen;
char tem[100];
templen=strlen(temp);
int i;
int j;
j=0;
for(i=0;i<templen;i+=temlen)
{
if(isdigit(temp[i]))
{
sscanf(&temp[i],"%[^-^+^*^/^(^)]",tem);
temlen=strlen(tem);
strcpy(in[j].numormark,tem);
formulalen++;
j++;
}
else
{
temlen=1;
in[j].numormark[0]=temp[i];
in[j].numormark[1]='\0';
formulalen++;
j++;
}
}
}
void Aeasyproblem::intopos() // 这是用于将中缀表达式转化成后缀表达式的函数
{
/**//////////////////'(' ')''+''-''*''/''='
static int isp[7]={ 0,19,12,12,13,13,0};
static int icp[7]={20,19,12,12,13,13,0};
int precedence_sta;
int precedence_token;
stack<formula>sta;
int i;
int j;
char token[100];
formula start;
strcpy(start.numormark,"\0");
sta.push(start);
j=0;
for(i=0;i<formulalen;i++)
{
strcpy(token,in[i].numormark);
if(strcmp(token,"\0")==0)
break;
if(isdigit(token[0]))
{
strcpy(pos[j].numormark,token);
j++;
}
else if(strcmp(token,")")==0)
{
while(strcmp(sta.top().numormark ,"(")!=0)
{
strcpy(pos[j].numormark,sta.top().numormark);
j++;
sta.pop();
}
sta.pop();
}
else
{
if(strcmp(sta.top().numormark,"(")==0)
precedence_sta=0;
if(strcmp(sta.top().numormark,")")==0)
precedence_sta=19;
if(strcmp(sta.top().numormark,"+")==0)
precedence_sta=12;
if(strcmp(sta.top().numormark,"-")==0)
precedence_sta=12;
if(strcmp(sta.top().numormark,"*")==0)
precedence_sta=13;
if(strcmp(sta.top().numormark,"/")==0)
precedence_sta=13;
if(strcmp(sta.top().numormark,"\0")==0)
precedence_sta=0;
if(strcmp(token,"(")==0)
precedence_token=20;
if(strcmp(token,")")==0)
precedence_token=19;
if(strcmp(token,"+")==0)
precedence_token=12;
if(strcmp(token,"-")==0)
precedence_token=12;
if(strcmp(token,"*")==0)
precedence_token=13;
if(strcmp(token,"/")==0)
precedence_token=13;
if(strcmp(token,"\0")==0)
precedence_token=0;
while(precedence_sta>=precedence_token)
{
pos[j]=sta.top();
j++;
sta.pop();
if(strcmp(sta.top().numormark,"(")==0)
precedence_sta=0;
if(strcmp(sta.top().numormark,")")==0)
precedence_sta=19;
if(strcmp(sta.top().numormark,"+")==0)
precedence_sta=12;
if(strcmp(sta.top().numormark,"-")==0)
precedence_sta=12;
if(strcmp(sta.top().numormark,"*")==0)
precedence_sta=13;
if(strcmp(sta.top().numormark,"/")==0)
precedence_sta=13;
if(strcmp(sta.top().numormark,"\0")==0)
precedence_sta=0;
if(strcmp(token,"(")==0)
precedence_token=20;
if(strcmp(token,")")==0)
precedence_token=19;
if(strcmp(token,"+")==0)
precedence_token=12;
if(strcmp(token,"-")==0)
precedence_token=12;
if(strcmp(token,"*")==0)
precedence_token=13;
if(strcmp(token,"/")==0)
precedence_token=13;
if(strcmp(token,"\0")==0)
precedence_token=0;
}
strcpy(start.numormark,token);
sta.push(start);
}
}
while(strcpy(token,sta.top().numormark))
{
if(strcmp(token,"\0")==0)
break;
pos[j]=sta.top();
j++;
sta.pop();
}
strcpy(pos[j].numormark,"\0");
}
void Aeasyproblem::showthepos()
{
int i;
for(i=0;strcmp(pos[i].numormark,"\0")!=0;i++)
{
cout<<pos[i].numormark<<' ';
}
cout<<'\b'<<endl<<endl;
}
/**/ ///////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////Problem类定义完成/////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
int main ()
{
Aeasyproblem justforfun;
while(1)
{
justforfun.inputtheformula();
justforfun.intoin();
justforfun.intopos();
justforfun.showthepos();
}
return 0;
}
// All right not reserved!!
#include < process.h >
#include < iostream.h >
#include < conio.h >
#include < stdlib.h >
#include < math.h >
#include < stack >
#include < cstring >
#include < ctype.h >
using namespace std;
struct formula
{
char numormark[100];
} ;
/**/ ///////////////////////////////////////////////////////////
/////////////////////定义problem类/////////////////////////
///////////////////////////////////////////////////////////
class Aeasyproblem
{
private:
int formulalen;
char temp[1000];
formula in[1000];
formula pos[1000];
public:
void inputtheformula();
void intoin();//用于将中缀表达式放入结构体数组,从而实现数字与运算符的分离;
void intopos();//用于将中缀表达式转化成后缀表达式;
void showthepos();
} ;
void Aeasyproblem::inputtheformula()
{
cin>>temp;
}
void Aeasyproblem::intoin() // 这是用于解析输入的字符串的函数,将算式中的每个个体存入自己声明的结构体中;
{
formulalen=0;
int templen;
int temlen;
char tem[100];
templen=strlen(temp);
int i;
int j;
j=0;
for(i=0;i<templen;i+=temlen)
{
if(isdigit(temp[i]))
{
sscanf(&temp[i],"%[^-^+^*^/^(^)]",tem);
temlen=strlen(tem);
strcpy(in[j].numormark,tem);
formulalen++;
j++;
}
else
{
temlen=1;
in[j].numormark[0]=temp[i];
in[j].numormark[1]='\0';
formulalen++;
j++;
}
}
}
void Aeasyproblem::intopos() // 这是用于将中缀表达式转化成后缀表达式的函数
{
/**//////////////////'(' ')''+''-''*''/''='
static int isp[7]={ 0,19,12,12,13,13,0};
static int icp[7]={20,19,12,12,13,13,0};
int precedence_sta;
int precedence_token;
stack<formula>sta;
int i;
int j;
char token[100];
formula start;
strcpy(start.numormark,"\0");
sta.push(start);
j=0;
for(i=0;i<formulalen;i++)
{
strcpy(token,in[i].numormark);
if(strcmp(token,"\0")==0)
break;
if(isdigit(token[0]))
{
strcpy(pos[j].numormark,token);
j++;
}
else if(strcmp(token,")")==0)
{
while(strcmp(sta.top().numormark ,"(")!=0)
{
strcpy(pos[j].numormark,sta.top().numormark);
j++;
sta.pop();
}
sta.pop();
}
else
{
if(strcmp(sta.top().numormark,"(")==0)
precedence_sta=0;
if(strcmp(sta.top().numormark,")")==0)
precedence_sta=19;
if(strcmp(sta.top().numormark,"+")==0)
precedence_sta=12;
if(strcmp(sta.top().numormark,"-")==0)
precedence_sta=12;
if(strcmp(sta.top().numormark,"*")==0)
precedence_sta=13;
if(strcmp(sta.top().numormark,"/")==0)
precedence_sta=13;
if(strcmp(sta.top().numormark,"\0")==0)
precedence_sta=0;
if(strcmp(token,"(")==0)
precedence_token=20;
if(strcmp(token,")")==0)
precedence_token=19;
if(strcmp(token,"+")==0)
precedence_token=12;
if(strcmp(token,"-")==0)
precedence_token=12;
if(strcmp(token,"*")==0)
precedence_token=13;
if(strcmp(token,"/")==0)
precedence_token=13;
if(strcmp(token,"\0")==0)
precedence_token=0;
while(precedence_sta>=precedence_token)
{
pos[j]=sta.top();
j++;
sta.pop();
if(strcmp(sta.top().numormark,"(")==0)
precedence_sta=0;
if(strcmp(sta.top().numormark,")")==0)
precedence_sta=19;
if(strcmp(sta.top().numormark,"+")==0)
precedence_sta=12;
if(strcmp(sta.top().numormark,"-")==0)
precedence_sta=12;
if(strcmp(sta.top().numormark,"*")==0)
precedence_sta=13;
if(strcmp(sta.top().numormark,"/")==0)
precedence_sta=13;
if(strcmp(sta.top().numormark,"\0")==0)
precedence_sta=0;
if(strcmp(token,"(")==0)
precedence_token=20;
if(strcmp(token,")")==0)
precedence_token=19;
if(strcmp(token,"+")==0)
precedence_token=12;
if(strcmp(token,"-")==0)
precedence_token=12;
if(strcmp(token,"*")==0)
precedence_token=13;
if(strcmp(token,"/")==0)
precedence_token=13;
if(strcmp(token,"\0")==0)
precedence_token=0;
}
strcpy(start.numormark,token);
sta.push(start);
}
}
while(strcpy(token,sta.top().numormark))
{
if(strcmp(token,"\0")==0)
break;
pos[j]=sta.top();
j++;
sta.pop();
}
strcpy(pos[j].numormark,"\0");
}
void Aeasyproblem::showthepos()
{
int i;
for(i=0;strcmp(pos[i].numormark,"\0")!=0;i++)
{
cout<<pos[i].numormark<<' ';
}
cout<<'\b'<<endl<<endl;
}
/**/ ///////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////Problem类定义完成/////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
int main ()
{
Aeasyproblem justforfun;
while(1)
{
justforfun.inputtheformula();
justforfun.intoin();
justforfun.intopos();
justforfun.showthepos();
}
return 0;
}