当前位置:网站首页>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();
}
};
边栏推荐
- Airsim雷达相机融合生成彩色点云
- 【入门】提取不重复的整数
- postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
- Lm08 mesh series mesh inversion (fine)
- 使用 setoolkit 伪造站点窃取用户信息
- Source code analysis of open source API gateway APIs IX
- Access报表实现小计功能
- 0 basic introduction to single chip microcomputer: how to use digital multimeter and precautions
- Significance and measures of source code encryption
- [question brushing] character statistics [0]
猜你喜欢

Gdip - hatchbrush pattern table

Latex formula code

Connect timed out of database connection
![[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 (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)

The Windows C disk is full

Gui Gui programming (XV) - use scale to control font size changes

Significance and measures of source code encryption

QT -- 1. QT connection database

Vhost kick & call principle

LSTM of RNN
随机推荐
手工挖XSS漏洞
[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
getInputStream() has already been called for this request
Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure
go通用动态重试机制解决方案的实现与封装
Five combination boxing, solving six difficult problems on campus and escorting the construction of educational informatization
The difference between interceptors and filters
How to check ad user information?
[getting started] input n integers and output the smallest K of them
Koltin35, headline Android interview algorithm
【力扣10天SQL入门】Day10 控制流
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
Learn the knowledge you need to know about the communication protocol I2C bus
Access report realizes subtotal function
5大组合拳,解决校园6大难题,护航教育信息化建设
The Windows C disk is full
使用 setoolkit 伪造站点窃取用户信息
Utiliser Beef pour détourner le navigateur utilisateur
Transaction method call @transactional