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?
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
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?
Name |
---|
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.