当前位置:网站首页>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();
}
};
边栏推荐
- Teach you how to apply for domestic trademark online step by step
- Access report realizes subtotal function
- 軟鍵盤高度報錯
- EDA open source simulation tool verilator beginner 6: debugging examples
- PostgreSQL source code learning (26) -- windows vscode remote debugging PostgreSQL on Linux
- 图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
- 7-26 word length (input and output in the loop)
- 【入门】输入n个整数,输出其中最小的k个
- Significance and measures of source code encryption
- Use threejs simple Web3D effect
猜你喜欢

Significance and measures of source code encryption

PostgreSQL source code learning (26) -- windows vscode remote debugging PostgreSQL on Linux
![[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 (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)

【入门】提取不重复的整数

P4 installation bmv2 detailed tutorial

【入门】输入n个整数,输出其中最小的k个

Soft keyboard height error

OJ输入输出练习

web254

軟鍵盤高度報錯
随机推荐
【入门】提取不重复的整数
web254
XX attack - reflective XSS attack hijacking user browser
凸印的印刷原理及工艺介绍
Array: question brushing record
Cyclic neural network
XX攻击——反射型 XSS 攻击劫持用户浏览器
Scala语言学习-07-构造器
uni 热更新
【入门】取近似值
力扣每日一题-第31天-1790.仅执行一次字符串交换能否使两个字符串相等
Gui Gui programming (XV) - use scale to control font size changes
On June 30, 2022, the record of provincial competition + national competition of Bluebridge
[redis] it takes you through redis installation and connection at one go
EDA开源仿真工具verilator入门6:调试实例
LSTM of RNN
Thesis learning -- Analysis and Research on similarity query of hydrological time series
Cmake I two ways to compile source files
SQL number injection and character injection
QT -- 1. QT connection database