当前位置:网站首页>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();
}
};
边栏推荐
- Li Kou daily question - day 31 -1790 Can a string exchange be performed only once to make two strings equal
- How outlook puts together messages with the same discussion
- slice扩容机制分析
- 【Redis】一气呵成,带你了解Redis安装与连接
- Anddroid 文本合成语音TTS实现
- Airsim雷达相机融合生成彩色点云
- Chinese font Gan: zi2zi
- sqlalchemy创建MySQL_Table
- Analysis of slice capacity expansion mechanism
- 数字转excel的字符串坐标
猜你喜欢

How to troubleshoot SharePoint online map network drive failure?

0 basic introduction to single chip microcomputer: how to use digital multimeter and precautions

web254

Instead of houses, another kind of capital in China is rising

Gdip - hatchBrush图案表

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

Soft keyboard height error

源代码加密的意义和措施

Five combination boxing, solving six difficult problems on campus and escorting the construction of educational informatization

Significance and measures of source code encryption
随机推荐
ContentType所有类型对比
【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion
Rumtime 1200 upgrade: London upgrade support, pledge function update and more
力扣每日一题-第31天-1790.仅执行一次字符串交换能否使两个字符串相等
XX attack - reflective XSS attack hijacking user browser
Thesis learning -- Analysis and Research on similarity query of hydrological time series
seaborn clustermap矩阵添加颜色块
Scala语言学习-07-构造器
EDA open source simulation tool verilator beginner 6: debugging examples
[untitled]
Two expressions of string
QT -- 1. QT connection database
【入门】输入n个整数,输出其中最小的k个
Erreur de hauteur du clavier souple
Aardio - [problem] the problem of memory growth during the callback of bass Library
Sqlalchemy creating MySQL_ Table
使用beef劫持用戶瀏覽器
Implementation and encapsulation of go universal dynamic retry mechanism
Anddroid text to speech TTS implementation
[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order