将中缀表达式转换成后缀表达式
2014-12-18 14:14
155 查看
#include<iostream> #include<cstring> #include<cmath> #include<cstdlib> #include<stack> using namespace std; void inToPostfix() { stack<char> s; char token; cin>>token; while(token != '=') { if(token >= 'a' && token <= 'z') { cout<<token<<" "; } else { switch(token) { case ')': { while(!s.empty() && s.top()!= '(') { cout<<s.top()<<" "; s.pop(); } s.pop(); break; } case '(': s.push(token); break; case '^': { while(!s.empty() && !(s.top() == '^' || s.top() == '(')) //'('一定要找到')'才能输出 { cout<<s.top(); s.pop(); } s.push(token); break; } case '*': case '/': { while(!s.empty() && !(s.top() == '+' || s.top() == '-' || s.top() == '(')) { cout<<s.top(); s.pop(); } s.push(token); break; } case '+': case '-': { while(!s.empty() && !(s.top() == '(')) { cout<<s.top()<<" "; s.pop(); } s.push(token); break; } } } cin>>token; } while(!s.empty()) { cout<<s.top()<<" "; s.pop(); } cout<<endl; } int main() { inToPostfix(); return 0; }
相关文章推荐
- 编写一个程序将后缀表达式转换成中缀表达式
- 栈的应用:后缀表达式到中缀表达式的转换
- 中缀表达式转换为后缀表达式
- 利用栈将中缀表达式转换成后缀表达式
- Java数据结构06----中缀表达式转换为后缀表达式
- 前缀表达式、中缀表达式、后缀表达式及转换实现
- 栈运用之——中缀表达式到后缀表达式的转换及运算
- 栈和队列6|中缀表达式转换为后缀表达式 - 数据结构和算法28
- 栈和队列6|中缀表达式转换为后缀表达式 – 数据结构和算法28
- 栈 实现 中缀表达式 转换成 后缀表达式 并 计算
- 中缀表达式转换成后缀表达式的实现(VB)
- 中缀表达式转换为后缀表达式
- 中缀表达式转换为后缀表达式(C)
- 栈的应用实例——中缀表达式转换为后缀表达式
- 中缀表达式直接转换为表达式二叉树、前缀表达式、后缀表达式,表达式求值
- 中缀表达式转换为后缀表达式的算法
- 后缀表达式 (1) ——中缀表达式与后缀表达式的手动转换
- 将中缀表达式转换为后缀表达式
- 中缀表达式转换成前缀表达式和后缀表达式
- 中缀表达式转换成后缀表达式的规则