当前位置:网站首页>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;
}
边栏推荐
- UVa 1025 - A Spy in the Metro(白书)
- Summary bug 】 【 Elipse garbled solution project code in Chinese!
- Another MySQL masterpiece published by Glacier (send the book at the end of the article)!!
- 一个函数有多少种调用方式?
- Create function report error, prompting DECLARE definition syntax problem
- 藏宝计划TreasureProject(TPC)系统模式开发技术原理
- encapsulation, package, access modifier, static variable
- utlis thread pool
- 网络基础学习系列四(网络层,数据链路层和一些其他重要协议或技术)
- With the rise of concepts such as metaverse and web3.0, many digital forms such as digital people and digital scenes have begun to appear.
猜你喜欢
随机推荐
Makefile
软测人每个阶段的薪资待遇,快来康康你能拿多少?
Shell编程的条件语句
With 4 years of work experience, the 5 communication methods between multi-threads can't be said, can you believe it?
2022-08-03 oracle执行慢SQL-Q17对比
Quickly build a website with static files
直播预告 | 构建业务智联,快速拥抱财务数字化转型
关于IDO预售系统开发技术讲解丨浅谈IDO预售合约系统开发原理分析
Diazo Biotin-PEG3-DBCO | Diazo Compound Modified Biotin-Tripolyethylene Glycol-Dibenzocyclooctyne
The salary of soft testers at each stage, come to Kangkang, how much can you get?
node连接mysql数据库报错:Client does not support authentication protocol requested by server
complete binary tree problem
The sword refers to the offer question 22 - the Kth node from the bottom in the linked list
物联网新零售模式,引领购物新潮流
Embedded Systems: Clocks
软件测试内卷严重,如何提升自己的竞争力呢?
嵌入式系统:时钟
伴随着元宇宙、web3.0等概念的兴起,数字人、数字场景等诸多数字化的形态开始出现
start with connect by 实现递归查询
云计算国内外发展现状