您的位置:首页 > 其它

中缀表达式转为后缀表达式以及后缀表达式的计算

2008-11-06 23:18 495 查看
//Infix2Postfix.h

class Infix2Postfix
{//Infix expression to a postfix(suffix) expression
public:
Infix2Postfix(){};//default constructor
Infix2Postfix(const string& infixExp):infix(infixExp){};
void setInfixExp(const string& infixExp){infix=infixExp;}
string postfixExp();//strike and return postfix expression
~Infix2Postfix(){};
private:
string infix;//infix expression for the conversion
string postfix;//postfix
stack<string> stk;//stack used to store operator
map<string,int> oper_prio;//Used to store the priority of operator
void set_priority();//set operator's priority
};

//Infix2Postfix.cpp

#include "Infix2Postfix.h"
void Infix2Postfix::set_priority()
{
oper_prio["#"]=1;
oper_prio["("]=2;
oper_prio["+"]=3;
oper_prio["-"]=3;
oper_prio["*"]=4;
oper_prio["/"]=4;
oper_prio["%"]=4;
oper_prio["^"]=5;
oper_prio[")"]=6;
}

string Infix2Postfix::postfixExp()
{
postfix="";
set_priority();
stk.push("#");
int i=0;
string input,topstk;
for(;i<infix.size();)
{//Get stack top of operator stack
topstk=stk.top();
input=infix.substr(i,1);//Get current input character
if(!oper_prio[input])
{//If current input character is not operator
postfix+=input;
}
else
{
if(oper_prio[input]>oper_prio[topstk])
{
if(input.compare(")")==0)
{
while(topstk.compare("(")!=0)
{
postfix+=topstk;
stk.pop();
topstk=stk.top();
}
stk.pop();
}
else
stk.push(input);
}
else
{
if(input.compare("(")!=0)
{
postfix+=topstk;
stk.pop();
continue;
}
stk.push(input);
}
}
++i;
}
topstk=stk.top();
while(topstk.compare("#")!=0)
{
postfix+=topstk;
stk.pop();
topstk=stk.top();
}
return postfix;
}

//PostFixEval.h

class PostFixEval
{// 计算后缀表达式的值
public:
PostFixEval(){};
void setPostFixExp(const string& postfixExp){postfix=postfixExp;};
int evaluate();
~PostFixEval(){};
private:
string postfix;
stack<int> stk;
void getOperands(int &left,int &right);
int compute(int left,int right,char op) const;
bool isOpeartor(char ch) const;
};

//PostFixEval.cpp

#include "PostFixEval.h"
#include <cctype>
#include <iostream>
using namespace std;
int PostFixEval::evaluate()
{
int i,left,right,expValue;
char ch;
for(i=0;i<postfix.length();i++)
{
ch=postfix[i];
if(isdigit(ch))
stk.push(ch-'0');
else if(isOpeartor(ch))
{
getOperands(left,right);
stk.push(compute(left,right,ch));
}
}
expValue=stk.top();
stk.pop();
return expValue;
}

void PostFixEval::getOperands(int &left, int &right)
{
right=stk.top();
stk.pop();
left=stk.top();
stk.pop();
}

int PostFixEval::compute(int left, int right, char op) const
{
int value;
switch(op)
{
case '+':
value=left+right;
break;
case '-':
value=left-right;
break;
case '*':
value=left*right;
break;
case '/':
if(right==0)
{cout<<"出现除0错误"<<endl;break;}
value=left/right;
break;
case '%':
if(right==0)
{cout<<"出现除0错误"<<endl;break;}
value=left%right;
break;
case '^':
if(left==0&&right==0)
{
cout<<"出现未定义的0^0错误"<<endl;
break;
}
value=1;
while(right>0)
{
value*=left;
right--;
}
break;
}
return value;
}

bool PostFixEval::isOpeartor(char ch) const
{
return ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||ch=='^';
}

//main

int _tmain(int argc, _TCHAR* argv[])
{

Infix2Postfix iexp;
string infix,postfix;
PostFixEval pexp;
cout<<"Please input a infix expression:";
cin>>infix;
while(infix.compare("q")!=0)
{
cout<<"The infix expression you input is: "<<infix<<endl;
iexp.setInfixExp(infix);
postfix=iexp.postfixExp();
cout<<"The corresponding postfix is "<<postfix<<endl;
pexp.setPostFixExp(postfix);
cout<<"The value of expression is "<<pexp.evaluate()<<endl<<endl;
cout<<"Please input another again"<<endl;
cin>>infix;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: