当前位置:网站首页>P1449 后缀表达式
P1449 后缀表达式
2022-08-03 22:51:00 【Recursi】
题目描述
所谓后缀表达式是指这样的一个表达式:式中不再引用括号,运算符号放在两个运算对象之后,所有计算按运算符号出现的顺序,严格地由左而右新进行(不用考虑运算符的优先级)。
如: 3*(5-2)+7 \texttt{3*(5-2)+7} 3*(5-2)+7 对应的后缀表达式为: 3.5.2.-*[email protected] \texttt{3.5.2.-*[email protected]} 3.5.2.-*[email protected]。在该式中,@
为表达式的结束符号。.
为操作数的结束符号。
输入格式
输入一行一个字符串 s s s,表示后缀表达式。
输出格式
输出一个整数,表示表达式的值。
样例 #1
样例输入 #1
3.5.2.-*[email protected]
样例输出 #1
16
提示
数据保证, 1 ≤ ∣ s ∣ ≤ 50 1 \leq |s| \leq 50 1≤∣s∣≤50,答案和计算过程中的每一个值的绝对值不超过 1 0 9 10^9 109。
/* * @Description: To iterate is human, to recurse divine. * @Autor: Recursion * @Date: 2022-08-02 18:17:15 * @LastEditTime: 2022-08-02 18:39:47 */
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 1e9 + 10;
const int N = 1e6;
string s;
int a,b;
int n,m;
stack<int> q;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> s;
for(int i = 0;i < s.length();i++){
if(s[i] == '@')
break;
else if(s[i] == '.'){
q.push(a);
a = 0;
b = 0;
}
else if(s[i] >= '0' && s[i] <= '9'){
a = b * 10 + s[i] - '0';
b = a;
}
else{
if(s[i] == '+'){
n = q.top();
q.pop();
m = q.top();
q.pop();
q.push(m + n);
}
if(s[i] == '-'){
n = q.top();
q.pop();
m = q.top();
q.pop();
q.push(m - n);
}
if(s[i] == '*'){
n = q.top();
q.pop();
m = q.top();
q.pop();
q.push(m * n);
}
if(s[i] == '/'){
n = q.top();
q.pop();
m = q.top();
q.pop();
q.push(m / n);
}
}
}
cout << q.top() << endl;
return 0;
}
边栏推荐
- Take an example of a web worker
- How many way of calling a function?
- Golang Chapter 2: Program Structure
- 目标检测技术研究现状及发展趋势
- [b01lers2020]Life on Mars
- The sword refers to the offer question 22 - the Kth node from the bottom in the linked list
- utils timer
- Cloud platform construction solutions
- 嵌入式系统:GPIO
- navicat 连接 mongodb 报错[13][Unauthorized] command listDatabases requires authentication
猜你喜欢
2019年10月SQL注入的两倍
Conditional Statements for Shell Programming
关于IDO预售系统开发技术讲解丨浅谈IDO预售合约系统开发原理分析
LabVIEW代码生成错误 61056
ML's yellowbrick: A case of interpretability (threshold map) for LoR logistic regression model using yellowbrick based on whether Titanic was rescued or not based on the two-class prediction dataset
2022-08-02 mysql/stonedb slow SQL-Q18 - memory usage surge analysis
七夕活动浪漫上线,别让网络拖慢和小姐姐的开黑时间
嵌入式系统:时钟
走迷宫 BFS
for loop exercises
随机推荐
Fluorescein-PEG-CLS,胆固醇-聚乙二醇-荧光素科研试剂
静态文件快速建站
【day6】类与对象、封装、构造方法
Create function report error, prompting DECLARE definition syntax problem
Cisco ike2 IPSec configuration
UVa 10003 - Cutting Sticks(白书,区间DP)
start with connect by 实现递归查询
直播预告 | 构建业务智联,快速拥抱财务数字化转型
嵌入式系统:概述
【bug】汇总Elipse项目中代码中文乱码解决方法!
With 4 years of work experience, the 5 communication methods between multi-threads can't be said, can you believe it?
最小化安装debian11
Embedded Systems: GPIO
Golang第二章:程序结构
How to write a database document management tool based on WPF (2)
start with connect by implements recursive query
PowerMockup 4.3.4::::Crack
Testng listener
使用tf.image.resize() 和tf.image.resize_with_pad()调整图像大小
Analysys Analysis: The transaction scale of China's online retail B2C market in Q2 2022 will reach 2,344.47 billion yuan