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 | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 155 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
10 | djm03178 | 152 |
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.