C++中缀表达式转换后缀表达式
2013-11-23 23:14
555 查看
#include <iostream> #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(); } } int main() { inToPostfix(); return 0; }
相关文章推荐
- 汇编/C/C++/MFC/JAVA/C# 进阶群103197177
- typeid详解
- c++ STL string类的查找、替换和删除操作
- C语言学习之认识exit()函数
- C语言里面堆栈的区别
- 【转载】12个有趣的C语言面试题
- c++ stl string类的基本用法
- 如何在Visual Studio2010上创建一个 C语言程序?!
- 第一个MFC程序编译有感
- 第一个MFC程序编译有感
- C语言把内存的文件保存到硬盘时如何把文件名由用户通过屏幕自己设置,并且保存到指定的路径
- c++ stl学习 传统C字符串用法
- C++ - 类模板(class template) 详解 及 代码
- C++ - 类模板(class template) 详解 及 代码
- 谈谈C语言指针那些陈年旧事
- 随机函数的生成 竞猜数字游戏
- C++打开特定编码格式的文件(utf-8)
- 如何在fedora上编译c或c++文件
- C++代码优化方法总结
- 发现一个学习C++很有用的博客