I feel really very stupid right now.
I am stuck on this problem and it sounding very very easy. I know it has to do with stack but the stupid multiplication and division are making it hard for me.
What can I do?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
2 | maomao90 | 163 |
4 | atcoder_official | 161 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
I feel really very stupid right now.
I am stuck on this problem and it sounding very very easy. I know it has to do with stack but the stupid multiplication and division are making it hard for me.
What can I do?
Название |
---|
Assuming all whitespace has been removed:
This can be easily optimized further.
Shunting-yard is based on a stack: https://en.wikipedia.org/wiki/Shunting-yard_algorithm
Thanks, but I am really struggling with this problem because of multiplication and division mixed with add/sub. Can you help me here?
The Shunting-yard algorithm should work. What about the mult/div are you having trouble with?
i think this could work
you have 2 stack: operatorStack and digitStack
scan all char in string
if digit, push it to digitStack
if operator and it has not less precedence than operator on top of operatorStack, push it to operatorStack
idea is whenever you have '*' or '/' on top of stack and the next operation is '+' or '-', you have to calculate the expression until top of stack is '+' or '-' (because you have to calculate all '*' and '/' before going to next operation), then push the result of calculation to digitStack
result will be calculation of operator and digit left on the stack after scan the string
sorry for my bad english
I think you need to look on
process_stack
functionThe following is yet another stack-based possible solution for computing the value of the arithmetic expression. It is assumed that expression has no syntax error, i.e. the stack should contain after parsing the expression an odd number of items whose types alternate consecutively between operand (non-negative integer) and operator ( '*', '+', '-' or '/' ), and the top item of the stack should be an operand.