博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ STL stack(栈)
阅读量:4594 次
发布时间:2019-06-09

本文共 937 字,大约阅读时间需要 3 分钟。

#include <stack> //STL -- stack 头文件

stack <int> s; //定义栈
s.pop(); //从栈顶删除元素;
s.push(); //从栈顶入栈
s.top(); //取栈顶元素,但不删除

例:逆波兰式

 

#include <iostream>

#include <cstdio>
#include <algorithm>
#include <stack>
//#define ISA_XR
using namespace std;
stack <int> s;
int main()
{
#ifdef ISA_XR
freopen("Reverse_Polish_notation.in","r",stdin);
freopen("Reverse_Polish_notation.out","w",stdout);
#endif
string in;
while( cin>>in ) //读入。。。
{
//读入为符号 则取栈顶两元素进行相应运算
if(in[0] == '+')
{
int a = s.top();s.pop();
int b = s.top();s.pop(); //取栈顶2各元素,赋给a,b,删除两元素
s.push(a + b); //运算。。
}
else if(in[0] == '-')
{
int a = s.top();s.pop();
int b = s.top();s.pop();
s.push(b - a);
}
else if(in[0] == '*')
{
int a = s.top();s.pop();
int b = s.top();s.pop();
s.push(a * b);
}
else s.push(atoi(in.c_str())); //如果读入为数 则入栈
}
cout<<s.top(); //输出栈顶元素---最终运算结果;
#ifdef ISA_XR
fclose(stdin);
fclose(stdout);
#endif
return 0;
}

 

转载于:https://www.cnblogs.com/xrisa/p/11185494.html

你可能感兴趣的文章
[原]openstack-kilo--issue(十六) instance can't get ip 虚拟机不能得到ip(1)
查看>>
面向对象(二)
查看>>
滑动的scrollowview的导航渐变
查看>>
[国嵌攻略][088][多线程同步]
查看>>
Ichars制作数据统计图
查看>>
NET程序之小试牛刀
查看>>
Java中的String,StringBuilder,StringBuffer三者的区别
查看>>
JavaWeb--自定义标签4--带父标签的自定义标签
查看>>
FreeBSD 下sac101.6a编译失败解决办法
查看>>
python之list
查看>>
vs 配色方案
查看>>
大熊君说说JS与设计模式之------单例模式Singleton()
查看>>
ROC曲线【转】
查看>>
jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关...
查看>>
python字符串内建函数
查看>>
第一章 javascript简介
查看>>
CentOS6.3升级GCC到GCC4.8.2
查看>>
WebService工作原理及传输安全问题
查看>>
字符数组与字符指针的差别与联系
查看>>
将ipv4地址转换为不同格式(python程序)
查看>>