当前位置:网站首页>Stack implementation calculator
Stack implementation calculator
2022-07-01 08:13:00 【The world is ordinary】
leetcode 224 Use stack to realize calculator .
Extend the , It's not just about adding and subtracting , Multiplication and division are also possible .
In addition, negative numbers can be realized for (-2) + 3 This operation of negative numbers .
Double stack
opv: Digital stack
opc: Symbol stack
Priority words :
- If the top of the stack is plus or minus , Current is addition and subtraction , You can calculate .
- If the top of the stack is multiplication and division , Currently, it is not an open parenthesis , You can calculate .
- If the top of the stack is an open bracket , Currently, if it is a right parenthesis , Just directly Once out of the stack , Otherwise, it doesn't count .
Dealing with negative numbers :
Negative numbers are not preceded by numbers , There is no right parenthesis , So what satisfies these two characteristics is negative numbers .
The processing of numbers :
Pay attention to the use of numbers in intermediate calculation long Make a strong turn . Otherwise, if it is INT_MAX Explosive int.
Treatment of left and right parentheses :
The right bracket is not put on the stack . The left bracket does not count .
Handling of spaces :
only Clear the digital counter cur, No calculation .
class Solution {
public:
int calculate(string s) {
stack<int>opv;
stack<char>opc;
auto cal = [&]() {
char ch = opc.top(); opc.pop();
int a = opv.top(); opv.pop();
int b = opv.top(); opv.pop();
if (ch == '+') opv.push(b + a);
else if (ch == '-') opv.push(b - a);
else if (ch == '*') opv.push(b * a);
else if (ch == '/') opv.push(b / a);
// printf("%d %c %d = %d\n", b, ch, a, opv.top());
};
auto check = [&](char ch) -> int {
char top = opc.top();
if (top == '+' || top == '-') {
if (ch == '+' || ch == '-' || ch == ')')
return 1;
}
else if (top == '*' || top == '/') {
if (ch != '(')
return 1;
}
else if (top == '(' && ch == ')') return 0;
return -1;
};
s += ' ';
int cur = -1;
opc.push('#');
char pre = '#';
for (char ch : s) {
if (isdigit(ch)) {
if (cur == -1) cur = 0;
cur = (long)cur * 10 + ch - '0'; // 2. Prevent explosion int
// printf("%d\n", cur);
}
else {
if (cur != -1) opv.push(cur);
cur = -1;
if (ch == ' ') continue; // Space
if (ch == '-' && (!isdigit(pre) && pre != ')')) { // negative
// printf("pre = %c\n", pre);
opv.push(0);
}
while (check(ch) > 0) cal();
if (check(ch) == 0) // Right bracket , The left bracket shows a , Not all of them
opc.pop();
if (ch != ')') // The right bracket is not put in
opc.push(ch);
}
if (pre != ' ') pre = ch;
}
while (opc.top() != '#') cal();
return opv.top();
}
};
边栏推荐
- How to troubleshoot SharePoint online map network drive failure?
- AArdio - 【问题】bass库回调时内存增长的问题
- CPU设计实战-第四章实践任务一简单CPU参考设计调试
- Analysis of slice capacity expansion mechanism
- Soft keyboard height error
- Utiliser Beef pour détourner le navigateur utilisateur
- Instead of houses, another kind of capital in China is rising
- 軟鍵盤高度報錯
- [question brushing] character statistics [0]
- [staff] key number (key number identification position | key number marking list | a major key identification principle | F, C, G position marking ascending | F major key identification principle | B
猜你喜欢

Latex table

Learn reptiles for a month and earn 6000 a month? Tell you the truth about the reptile, netizen: I wish I had known it earlier

Lm08 mesh series mesh inversion (fine)

038 network security JS

5大组合拳,解决校园6大难题,护航教育信息化建设

如何使用layui将数据库中的数据以表格的形式展现出来

使用beef劫持用户浏览器

LSTM of RNN
![[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)](/img/48/de19e8cc007b93a027a906d4d423b2.png)
[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)

CPU设计实战-第四章实践任务一简单CPU参考设计调试
随机推荐
【网站架构】一招搞定90%的分布式事务,实打实介绍数据库事务、分布式事务的工作原理应用场景
Airsim radar camera fusion to generate color point cloud
Programmer's regimen
Aardio - [problem] the problem of memory growth during the callback of bass Library
Thesis learning -- Analysis and Research on similarity query of hydrological time series
軟鍵盤高度報錯
[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)
P4 installation bmv2 detailed tutorial
Principle and process of embossing
5大组合拳,解决校园6大难题,护航教育信息化建设
038 network security JS
web254
String coordinates of number to excel
【入门】输入整型数组和排序标识,对其元素按照升序或降序进行排序
[getting started] input n integers and output the smallest K of them
程序员养生宝典
手工挖XSS漏洞
使用 setoolkit 伪造站点窃取用户信息
PostgreSQL source code learning (26) -- windows vscode remote debugging PostgreSQL on Linux
How to prevent the other party from saying that he has no money after winning the lawsuit?