逆波兰算法 C++——String及Stack实现
2018-01-22 21:29
148 查看
#include "cstdio" #include "iostream" #include "stack" #include "string" #include "sstream" using namespace std; stack<float> n; stack<string> f; int main(){ string s,buf; float a,b; float temp; while(getline(cin,s)){ stringstream ss(s); while(ss >> buf){ // cout << buf; if(buf[0] == '+'){ b = n.top(); // cout << b <<endl; n.pop(); a = n.top(); // cout << a <<endl; n.pop(); n.push(a+b); // cout << n.top()<<endl; } else if(buf[0] == '-'){ b = n.top(); n.pop(); a = n.top(); n.pop(); n.push(a-b); } else if(buf[0] == '*'){ b = n.top(); n.pop(); a = n.top(); n.pop(); n.push(a*b); } else { stringstream toFloat; toFloat << buf; toFloat >> temp; // cout << temp <<endl; n.push(temp); } } cout <<n.top() << endl; } }
相关文章推荐
- 逆波兰算法,实现一个四则运算计算器
- Reverse反转算法+斐波那契数列递归+Reverse反转单链表算法--C++实现
- 匈牙利算法的C++实现
- Fast Compressive Tracking(快速压缩跟踪)算法的C++代码实现
- [数据结构与算法]栈Stack的多种实现
- C++实现二叉树的镜像操作(18)---《那些奇怪的算法》
- linux c/c++ 后台开发常用算法之:md5算法c++版实现
- C++基本算法收集及代码实现
- 海量数据处理系列----C++中Bitmap算法的实现
- 快速图像旋转算法的c++实现
- C/C++:各种基本算法实现小结(三)—— 树与二叉树
- c++中栈的基本实现stack
- 编译原理(七) 算符优先分析法(构造算符优先关系表算法及C++实现)
- 数据结构与算法之递推算法 C++与PHP实现
- c++实现快速选择算法
- 算法代码实现之二分法查找,C/C++实现
- C/C++日常学习总结(第十篇)string的实现&文件流操作
- C++的Stack模板的简单实现
- C++ - 类似"vector"容器(存储string) 的 实现
- 数据结构与算法——优先队列类的C++实现(二叉堆)