博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[河南省ACM省赛-第四届] 表达式求值(nyoj 305)
阅读量:7122 次
发布时间:2019-06-28

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

栈的模拟应用:

 

#include
#include
#include
#include
#include
#include
#include
using namespace std;string getPostfixExp(string s){ stack
sta;// d n x string exp; int len = s.size(); for(int i=0; i
= '0' && s[i] <= '9'){ num += s[i++]; } if(exp[0] != NULL)//后缀表达式的第一个一定是数字,用此来避免首位空格 exp += " "; exp += num; i--; } } return exp;}int calculate(string post){ stack
sta; int len = post.size(); for(int i=0; i
= 'a') { //如果是运算符 int a = sta.top(); sta.pop(); int b = sta.top(); sta.pop(); if(ch == 'd') sta.push(a+b); else if(ch == 'x') sta.push(max(a, b)); else if(ch == 'n') sta.push(min(a, b)); } else if(ch != ' ') { //如果为数字 int num = 0; while(post[i] >= '0' && post[i] <= '9') num = num*10+post[i++]-'0'; sta.push(num); i--; } } return sta.top(); }int main(){ freopen("d:\\in.txt", "r", stdin); string s; int t; cin>>t; while(t--) { cin>>s; string post = getPostfixExp(s); cout<
<

转载于:https://www.cnblogs.com/huwtylv/p/4385306.html

你可能感兴趣的文章
PorterDuffXfermode的用法
查看>>
gitlab邮件服务器配置
查看>>
电脑共享不了
查看>>
无差异同步“rsync”
查看>>
linux系统上的命令
查看>>
从EMC Atmos开始了解对象存储
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
Thinkphp5开发OA办公系统-人事管理模块
查看>>
attr的format类型
查看>>
Maven多环境打包
查看>>
我的友情链接
查看>>
PhpStorm 头部注释、类注释和函数注释的设置
查看>>
我的友情链接
查看>>
Selenium2(webdirver)入门之环境搭建(Java版)
查看>>
MySQL入门-10:子查询与联结表
查看>>
VMware三个版本workstation、server、esxi的区别
查看>>
Nginx 安装及调优
查看>>
hbase shell基本操作命令详解
查看>>
网络编程学习——数据链路访问
查看>>