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

//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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值