What we have to do here is parsing some given string into a numerical calculation.
Here are few catches:
Plus and minus signs are like check points.
The operator before a number determines what should be done with it to its previous sum:
100-40/2/5 ->
40 gets subtracted from the previous sum 0 -> -40
Then previous sum divided by 2 yields -> -20
Same for 5 -> -4
At the end, it is added to the total sum, which was 100 -> 100+(-4)=96
For parentheses we call to function again, in a case like this:
Resolves: #229
Algorithm:
What we have to do here is parsing some given string into a numerical calculation. Here are few catches:
Plus and minus signs are like check points.
The operator before a number determines what should be done with it to its previous sum:
100-40/2/5
-> 40 gets subtracted from the previous sum 0 -> -40 Then previous sum divided by 2 yields -> -20 Same for 5 -> -4 At the end, it is added to the total sum, which was 100 -> 100+(-4)=96calculate(e1*(e2+(e3)))
What we should do is:Which is pretty easy to implement using recursion, with closing parentheses terminating the recursion.
So, we need to keep track of the total sum, current operator and the current previous sum to implement these.