输入中缀或后缀表达式,输出值
来源:互联网 发布:淘宝一元拍手机在哪 编辑:程序博客网 时间:2024/06/05 00:56
输入中缀或后缀表达式,输出值。
思路: 如果是中缀表达式,则先转化成后缀表达式。定义两个栈,一个操作符栈,根据优先级决定是否入栈,另一个栈,将操作数和运算符转化成后缀式存储,进行计算。
如果是后缀表达式,直接进行运算。
#include <iostream>#include <string>#include <stack>#include <vector>#include <cstdlib>using namespace std;stack<char> st;//存储操作符int priority(char a)//运算符优先级{switch(a){case '+':case '-':return 1;break;case '*':case '/':return 2;break;case '(':return 0;break;case ')':return 3;break;}return -1;}vector<char> s;//后序存储栈void change(string str){int i=0;while(i!=str.size()){if(str[i]>='0'&&str[i]<='9')//数字入栈s.push_back(str[i]);else {if(str[i]=='(')//第一个为左括号入操作符栈st.push(str[i]);else if(st.size()==0)//运算过程中栈中无字符st.push(str[i]);else if(priority(st.top())<priority(str[i]))//优先级的比较{if(str[i]==')'){while(st.top()!='(')//先将括号里面的运算符入栈{char b=st.top();st.pop();s.push_back(b);}st.pop();//左括号出栈}elsest.push(str[i]);}else{while(priority(st.top())>=priority(str[i])){if(st.top()=='('){st.pop();continue;}char b=st.top();st.pop();s.push_back(b);if(st.size()==0){st.push(str[i]);break;}}}}++i;}while(!st.empty()){char t=st.top();st.pop();s.push_back(t);}}int fun(string str){stack<char> temp;int result=0;int m,n;for(int i=0;i<s.size();++i){if(isdigit(s[i]))//是数字{n=s[i]-'0';temp.push(n);}else {m=temp.top();//运算符temp.pop();switch(s[i]){case '+':result=temp.top()+m;break;case '-':result=temp.top()-m;break;case '*':result=temp.top()*m;break;case '/':result=temp.top()/m;break;}temp.pop();temp.push(result);}}return result;}int fun1(string str){stack<char> temp;int result=0;int m,n;for(int i=0;i<str.size();++i){if(isdigit(str[i])){n=str[i]-'0';temp.push(n);}else {m=temp.top();temp.pop();switch(str[i]){case '+':result=temp.top()+m;break;case '-':result=temp.top()-m;break;case '*':result=temp.top()*m;break;case '/':result=temp.top()/m;break;}temp.pop();temp.push(result);}}return result;}int main(){while(1){int key;while(1){cout<<"请输入:1.中缀表达式;2.后缀表达式;3.退出"<<endl;cin>>key;string str;if(key==1){cout<<"请输入中缀表达式:"<<endl;cin>>str;change(str);cout<<"运算结果:"<<fun(str)<<endl;}else if(key==2){cout<<"请输入后缀表达式:"<<endl;cin>>str;cout<<"运算结果:"<<fun1(str)<<endl;}else return 0;}}return 0;}
输入表达式,输出前序遍历
输入:a+b*(c-d)-e/f
输出:-+a*b-cd/ef
#include <iostream>#include <stack>#include <vector>#include <string>using namespace std;typedef struct no{ char data; struct no *lchild,*rchild;}*node;stack<char> st;int priority(char a){ switch(a) { case '+': case '-': return 1; break; case '*': case '/': return 2; break; case '(': return 0; break; case ')': return 3; break; } return -1;}vector<char> s;string change(string str)//转为后缀表达式,即后续遍历{ int i=0; string post=""; while(i!=str.size()) { if(str[i]>='a'&&str[i]<='z') s.push_back(str[i]); else { if(str[i]=='(') { st.push(str[i]); }else if(st.size()==0)st.push(str[i]);else if(priority(st.top())<priority(str[i])){if(str[i]==')'){while(st.top()!='('){char b=st.top();st.pop();s.push_back(b);}st.pop();}else st.push(str[i]);}else{while(priority(st.top())>=priority(str[i])){if(st.top()=='('){st.pop();continue;}char c=st.top();st.pop();s.push_back(c);if(st.size()==0){st.push(str[i]);break;}}} } ++i; } while(!st.empty()){ char t=st.top(); st.pop(); s.push_back(t); } for(i=0;i<s.size();++i) { //cout<<s[i]<<" "; post+=s[i]; } return post;}node create(string post)///根据后序遍历,建树{ node tree; stack<node>st; for(int i=0; i<post.length(); i++){ if(post[i]!='+'&&post[i]!='-'&&post[i]!='/'&&post[i]!='*')///一定是叶子结点{ tree=new no(); tree->data=post[i]; tree->lchild=tree->rchild=NULL; } else///非叶子结点,赋值其左右子孩子{ tree=new no(); tree->data=post[i]; tree->rchild=st.top(); st.pop(); tree->lchild=st.top(); st.pop(); } st.push(tree); } return st.top();}void pre(node &sa){ if(sa!=NULL){ cout<<sa->data; pre(sa->lchild); pre(sa->rchild); }}int main(){ cout<<"请输入中缀表达式:"<<endl; string inorder,postorder; node head; head=new no(); cin>>inorder; //cout<<in<<endl; cout<<"转换为后缀表达式为:"<<endl; postorder=change(inorder); cout<<postorder<<endl; //cout<<"构建表达式树"<<endl; head=create(postorder); cout<<"前序遍历:"<<endl; pre(head); cout<<endl;return 0;}/*a+b*(c-d)-e/f*/
0 0
- 输入中缀或后缀表达式,输出值
- 中缀表达式转换成前缀或后缀表达式
- 【中缀表达式转后缀表达式&&输出计算结果】
- 中缀表达式转后缀表达式并输出计算结果
- 中缀表达式转后缀表达式,并计算后缀表达式值
- 中缀表达式改后缀并计算值
- 中缀表达式转后缀并计算值
- 中缀转后缀表达式
- 中缀,前缀,后缀表达式
- 前缀、中缀、后缀表达式
- 前缀、中缀、后缀表达式
- 表达式 中缀 后缀 转换
- 前缀、中缀、后缀表达式
- 前缀、中缀、后缀表达式
- 中缀,前缀,后缀表达式
- 前缀中缀后缀表达式
- 前缀,中缀,后缀表达式
- 前缀,中缀,后缀表达式
- 1077. Kuchiguse
- webstorm对github 使用
- 肿瘤诊断(三维BFS)
- 17.2.17 关键字 注释 标识符 变量
- Unit4二进制存储、运算符、引用数据类型初步认识
- 输入中缀或后缀表达式,输出值
- 【Linux】CentOS 7通过Firewall开放防火墙端口
- 复制构造函数 与 赋值函数 的区别
- 12个最应该使用的Linux服务器OS(下)
- 1078. Hashing
- Java String 相关的面试题
- 1079. Total Sales of Supply Chain
- 《MySQL必知必会》学习笔记十五(触发器)------掌握部分
- 关于C++中"!"的非主流写法