当前位置:网站首页>1331:【例1-2】后缀表达式的值
1331:【例1-2】后缀表达式的值
2022-07-08 00:57:00 【一位13岁的编程爱好者】
题目
1331:【例1-2】后缀表达式的值
时间限制: 10 ms 内存限制: 65536 KB
【题目描述】
从键盘读入一个后缀表达式(字符串),只含有0-9组成的运算数及加(+)、减(—)、乘(*)、除(/)四种运算符。每个运算数之间用一个空格隔开,不需要判断给你的表达式是否合法。以@作为结束标志。
比如,16–9*(4+3)转换成后缀表达式为:16□9□4□3□+*–,在字符数组A中的形式为:
栈中的变化情况:
运行结果:-47
提示:输入字符串长度小于250,参与运算的整数及结果之绝对值均在264范围内,如有除法保证能整除。
【输入】
一个后缀表达式。
【输出】
一个后缀表达式的值。
【输入样例】
16 9 4 3 +*[email protected]
【输出样例】
-47
题目分析:开一个类型为long long的栈,遇到数字就压栈,遇到符号放出栈顶两个元素,计算后再压栈即可。最后栈顶元素即为所求。
C++代码
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<long long> st;
char ch;
long long n = 0;
while ((ch = getchar()) != '@')
{
if (ch >= '0' && ch <= '9')
n = n * 10 + ch - '0';
else if (ch == ' ')
{
st.push(n);
n = 0;
}
else
{
long long a = st.top();
st.pop();
long long b = st.top();
st.pop();
//注意,由于栈先进后出的特性,减法和除法时一定要把b放到前面!
if (ch == '+')
st.push(b + a);
else if (ch == '-')
st.push(b - a);
else if (ch == '*')
st.push(b * a);
else if (ch == '/')
st.push(b / a);
}
}
cout << st.top();
return 0;
}
边栏推荐
- Give some suggestions to friends who are just getting started or preparing to change careers as network engineers
- 【每日一题】736. Lisp 语法解析
- JVM memory and garbage collection-3-runtime data area / method area
- Force buckle 4_ 412. Fizz Buzz
- JVM memory and garbage collection -4-string
- Height of life
- nmap工具介紹及常用命令
- leetcode 869. Reordered Power of 2 | 869. Reorder to a power of 2 (state compression)
- 力扣6_1342. 将数字变成 0 的操作次数
- Key points of data link layer and network layer protocol
猜你喜欢
I don't know. The real interest rate of Huabai installment is so high
burpsuite
For friends who are not fat at all, nature tells you the reason: it is a genetic mutation
leetcode 865. Smallest Subtree with all the Deepest Nodes | 865.具有所有最深节点的最小子树(树的BFS,parent反向索引map)
Monthly observation of internet medical field in May 2022
Thread deadlock -- conditions for deadlock generation
文盘Rust -- 给程序加个日志
关于TXE和TC标志位的小知识
Wechat applet uniapp page cannot jump: "navigateto:fail can not navigateto a tabbar page“
《通信软件开发与应用》课程结业报告
随机推荐
Vim 字符串替换
Analysis ideas after discovering that the on duty equipment is attacked
力争做到国内赛事应办尽办,国家体育总局明确安全有序恢复线下体育赛事
Anan's judgment
How does the bull bear cycle and encryption evolve in the future? Look at Sequoia Capital
XMeter Newsletter 2022-06|企业版 v3.2.3 发布,错误日志与测试报告图表优化
leetcode 865. Smallest Subtree with all the Deepest Nodes | 865. The smallest subtree with all the deepest nodes (BFs of the tree, parent reverse index map)
[knowledge map paper] Devine: a generative anti imitation learning framework for knowledge map reasoning
The bank needs to build the middle office capability of the intelligent customer service module to drive the upgrade of the whole scene intelligent customer service
Ml self realization / logistic regression / binary classification
How to use diffusion models for interpolation—— Principle analysis and code practice
burpsuite
JVM memory and garbage collection-3-direct memory
burpsuite
云原生应用开发之 gRPC 入门
Semantic segmentation | learning record (5) FCN network structure officially implemented by pytoch
Force buckle 6_ 1342. Number of operations to change a number to 0
QT -- create QT program
Neural network and deep learning-5-perceptron-pytorch
Leetcode question brushing record | 485_ Maximum number of consecutive ones