中缀 后缀表达式求值
来源:互联网 发布:js广告 编辑:程序博客网 时间:2024/05/21 07:12
一.中缀转后缀
1.遇到操作数:直接输出(添加到后缀表达式中)
2.栈为空时,遇到运算符,直接入栈
3.遇到左括号:将其入栈
4.遇到右括号:执行出栈操作,并将出栈的元素输出,直到弹出栈的是左括号,左括号不输出。
5.遇到其他运算符:例如加减乘除:弹出所有优先级大于或者等于该运算符的栈顶元素,然后将该运算符入栈。
6.最终将栈中的元素依次出栈,输出。
二.由后缀表达式得到表达式树
三.对表达式树的根节点调用cal()虚函数就能算出表达式的值
代码
#include<iostream>#include<string>#include<map>#include<stack>using namespace std;map<char, int> m{{'*', 3}, {'/', 3},{'+', 4}, {'-', 4},{'(', 99}};//定义优先级 class node{public:virtual double cal() const = 0;virtual ~node () {}}; class numnode : public node{public:numnode(double d):number(d){}virtual double cal() const;private:double number;};class binarynode : public node{protected:node* _left;node* _right;public:binarynode(node* left, node* right):_left(left),_right(right){}~binarynode();};class addnode : public binarynode{public:addnode(node* left, node* right):binarynode(left, right){};virtual double cal() const;};class mutinode : public binarynode{public:mutinode(node* left, node* right):binarynode(left, right){};virtual double cal() const;};inline double numnode::cal() const{return number;}binarynode::~binarynode() {delete _left;delete _right;}double addnode::cal() const{return _left->cal() + _right->cal();}double mutinode::cal() const{return _left->cal() * _right->cal();}string suffix(const string& str) //得到后缀表达式{stack<char> sta;string ret;for(int i=0; i<str.size(); i++){if( str[i]=='.' || (str[i]>='0' && str[i]<='9') ){ret += str[i];}else{if(ret.back() != ' ')ret += " " ;if( sta.empty() || str[i]=='(' )sta.push(str[i]);else if(str[i] == ')'){while(sta.top()!='('){ret.push_back(sta.top());ret.push_back(' '); sta.pop();}sta.pop();}else {while( !sta.empty() && m[str[i]]>=m[sta.top()] ){ret = ret + sta.top()+" ";sta.pop();}sta.push(str[i]);}}//cout<<i+1<<" "<<ret<<endl;}ret += " ";//确保每个数后边都有个空格 while(!sta.empty()){ret = ret + sta.top()+" ";sta.pop();}return ret;}node* suffix_tree(const string& str)//生成后缀表达式树{stack<node*> sta;string temp;node* left;node* right;for(int i=0; i<str.size(); i++){//cout<<i<<endl;if( str[i]=='.' || (str[i]>='0' && str[i]<='9') ){temp += str[i];//cout<<"temp"<<temp<<endl;}else if(str[i]==' ' && str[i-1]>='0' && str[i-1]<='9' ){double d = stod(temp);node *pnode = new numnode(d);sta.push(pnode);temp = "";}else if(str[i]==' ')continue;else switch(str[i]){case '+':right = sta.top();sta.pop();left = sta.top();sta.pop();sta.push(new addnode(left,right));break;case '*':right = sta.top();sta.pop();left = sta.top();sta.pop();sta.push(new mutinode(left,right));break;}}return sta.top();}int main(){string temp = "11.0+22*33+(44.0*55+66)*77";cout<<"11.0+22*33+(44.0*55+66)*77的后缀表达式:";//11.0 22 33 * + 44.0 55 * 66 + 77 * +string sur = suffix(temp);cout<<sur<<endl;node* root = suffix_tree(sur);double x = root->cal();cout << x << endl;//192159delete root;}
一.中缀转后缀
二.由后缀直接得到结果
#include<iostream>#include<string>#include<map>#include<stack>using namespace std;map<char, int> m{{'*', 3}, {'/', 3},{'+', 4}, {'-', 4},{'(', 99}};string suffix(const string& str){stack<char> sta;string ret;for(int i=0; i<str.size(); i++){if(str[i]>='0' && str[i]<='9'){ret += str[i];}else{if(ret.back() != ' ')ret += " " ;if( sta.empty() || str[i]=='(' )sta.push(str[i]);else if(str[i] == ')'){while(sta.top()!='('){ret = ret + sta.top()+" ";sta.pop();}sta.pop();}else {while( !sta.empty() && m[str[i]]>=m[sta.top()] ){ret = ret + sta.top()+" ";sta.pop();}sta.push(str[i]);}}//cout<<i+1<<" "<<ret<<endl;}ret += " ";//确保每个数后边都有个空格 while(!sta.empty()){ret = ret + sta.top()+" ";sta.pop();}return ret;}int cal(const string &str){stack<int> sta;string temp;int left = 0;int right = 0;for(int i=0; i<str.size(); i++){//cout<<i<<endl;if(str[i]>='0' && str[i]<='9'){temp += str[i];//cout<<"temp"<<temp<<endl;}else if(str[i]==' ' && str[i-1]>='0' && str[i-1]<='9' ){sta.push(stoi(temp));temp = "";}else if(str[i]==' ')continue;else switch(str[i]){case '+':right = sta.top();//cout<<"right"<<right<<endl;sta.pop();left = sta.top();//cout<<"left"<<left<<endl;sta.pop();sta.push(left+right);//cout<<"jia"<<sta.top() <<endl;break;case '*':right = sta.top();//cout<<"right"<<right<<endl;sta.pop();left = sta.top();//cout<<"left"<<left<<endl;sta.pop();sta.push(left*right);//cout<<"cheng"<<sta.top() <<endl;break;}}return sta.top();}int main(){string temp = "11+22*33+(44*55+66)*77";cout<<"11+22*33+(44*55+66)*77的后缀表达式:";//11 22 33 *+ 44 55 *66 + 77 *+string sur = suffix(temp);cout<<sur<<endl;cout<<"表达式的和:";int sum = cal(sur);cout<<sum<<endl;}
参考:http://www.cnblogs.com/Solstice/archive/2012/07/06/learncpp.html
http://www.relisoft.com/book/lang/poly/3tree.html
阅读全文
0 0
- 中缀 后缀表达式求值
- 中缀表达式求值、后缀表达式求值、中缀转后缀、前缀
- 表达式求值(中缀变后缀再求值)
- 中缀转后缀 和 后缀表达式求值
- 【表达式求值】中缀表达式转变为后缀表达式
- NYOJ 35 表达式求值 【中缀转后缀+后缀表达式求值】
- 表达式求值(中缀转后缀及后缀表达式求值)
- 中缀表达式转后缀表达式并求值
- 中缀表达式转后缀表达式求值
- 中缀表达式转后缀表达式并求值
- 中缀表达式转后缀表达式求值
- 中缀表达式转后缀表达式并求值
- 中缀表达式变后缀表达式并求值
- 中缀表达式转后缀表达式并求值
- 中缀表达式转后缀表达式 求值
- 后缀表达式与中缀表达式求值
- 中缀和后缀表达式求值算法
- 表达式求值(前缀、中缀、后缀)
- 重构-改善既有代码的设计2010版 读书笔记--步骤变化原因及记录一
- 2017QZOI滚粗记
- lintcode 二叉查找树总结
- Java IO之字节流
- Python下三级菜单实现
- 中缀 后缀表达式求值
- 第十届ACM省赛-F Binary to Prime
- 网络基础知识-面试
- 基于tensorflow的AlexNet实现
- JDBC基础
- 如何才能高效的自学?
- 选择排序
- 《深入理解java虚拟机》学习笔记5——Java Class类文件结构
- CentOS 6.5 中文输入法设置