当前位置:网站首页>Expression evaluation (stack)
Expression evaluation (stack)
2022-07-24 19:55:00 【Goldinger】
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <stack>
using namespace std;
stack<char> op;
stack<int> num;
void eval()
{
auto b = num.top(); num.pop();
auto a = num.top(); num.pop();
auto c = op.top(); op.pop();
int x;
if (c == '+') x = a + b;
else if (c == '-') x = a - b;
else if (c == '*') x = a * b;
else x = a / b;
num.push(x);
}
int main()
{
string s;
cin >> s;
unordered_map<char, int> pr{
{
'+', 1}, {
'-', 1}, {
'*', 2}, {
'/', 2}};
for (int i = 0; i < s.size(); i ++ )
{
if (isdigit(s[i]))
{
int j = i, x = 0;
while (j < s.size() && isdigit(s[j]))
x = x * 10 + s[j ++ ] - '0';
num.push(x);
i = j - 1;
}
else if (s[i] == '(') op.push(s[i]);
else if (s[i] == ')')
{
while (op.top() != '(') eval();
op.pop();
}
else
{
while (op.size() && op.top() != '(' && pr[op.top()] >= pr[s[i]])
eval();
op.push(s[i]);
}
}
while (op.size()) eval();
cout << num.top() << endl;
return 0;
}
/* author :yxc link :https://www.acwing.com/activity/content/code/content/1431326/ source :AcWing The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .*/
Infix expression to suffix expression
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <stack>
using namespace std;
stack<char> op;
void eval()
{
auto c = op.top(); op.pop();
cout << c << ' ';
}
int main()
{
string s;
cin >> s;
unordered_map<char, int> pr{
{
'+', 1}, {
'-', 1}, {
'*', 2}, {
'/', 2}};
for (int i = 0; i < s.size(); i ++ )
{
if (isdigit(s[i]))
{
int j = i, x = 0;
while (j < s.size() && isdigit(s[j]))
x = x * 10 + s[j ++ ] - '0';
cout << x << ' ';
i = j - 1;
}
else if (s[i] == '(') op.push(s[i]);
else if (s[i] == ')')
{
while (op.top() != '(') eval();
op.pop();
}
else
{
while (op.size() && op.top() != '(' && pr[op.top()] >= pr[s[i]])
eval();
op.push(s[i]);
}
}
while (op.size()) eval();
return 0;
}
/* author :yxc link :https://www.acwing.com/activity/content/code/content/1431326/ source :AcWing The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .*/
边栏推荐
- Database index: index is not a panacea
- 微服务架构 | 服务监控与隔离 - [Sentinel] TBC...
- Leetcode652 finding duplicate subtrees
- 871. Sum of divisors
- Todolist case
- Leetcode402 remove K digits
- Implementation of OA office system based on JSP
- Getaverse, a distant bridge to Web3
- Unity code imports packages through package manager
- How to convert the world coordinates of unity's camera into view matrix
猜你喜欢

Flink Window&Time 原理

The ark compiler is coming. What about APK reinforcement

day 2

Implementation of OA office system based on JSP

Analysis of the basic concept of digital warehouse

Maya coffee machine modeling

Pay close attention! List of the latest agenda of 2022 open atom open source Summit

Hold the C pointer

Thymeleaf application notes

Original reverse compensation and size end
随机推荐
Day 4 (item 1: household income and expenditure records)
Analysis and Simulation of strlen function
Xiaomi 12s ultra products are so powerful, but foreigners can't buy Lei Jun: first concentrate on the Chinese market
Is the incremental update of SQL Server database identified according to the where clause? Can't do stream update? Each table should
Analysis of the basic concept of digital warehouse
Hide the middle digit of ID number
Environment preparation of Nacos configuration center
Pay close attention! List of the latest agenda of 2022 open atom open source Summit
Monotone stack and monotone queue (linear complexity optimization)
Decorator of function
Sword finger offer 42. maximum sum of continuous subarrays
Database index: index is not a panacea
Elastomer simulation (elasticity)
Thinking of @requestbody caused by hi and hello requests
Bypass using the upper limit of the maximum number of regular backtracking
English translation Chinese common dirty words
Sword finger offer 53 - I. find the number I in the sorted array
Sword finger offer 49. ugly number
Classic interview questions of interface testing: what is the difference between session, cookie and token?
Codeforces round 580 (Div. 2) c- almost equal [Law]