Lets say I have four numbers A,B,C,D.
There are four mathematical ways to arrange them with an operator in between. For my example I will use the division operator but the operators could be anything.
((A/B)/C)/D
A/(B/(C/D))
(A/(B/C))/D
A/((B/C)/D)
(A/B)/(C/D)
If the numbers were 1,2,3,4 the results would be .0416, .375, .375, 6, .666 respectively.
So if the string was
So if I was to replace A,B,C, and D with any numbers and the division operator with any operators how can I parse it so it gives me the correct result?
The way I did it was using a switch statement and having the case be which operator is at certain String indexes and that works but only for the first one where parentheses don't really matter. I could create 5 different parse methods for each of the five different combinations but it seems like there would be a much better way and it seems like that my way would get extremely clunky because I would have to use nested switch statements.
For example if the string is
012345678910
"A/((B/C)/D))
I would apply a switch statement for char at 5. Then do the operation with B and C and store that variable into lets say X.Then a switch statement at char 8, apply to D, store in Y, switch at 1, apply, print result. Then repeat those 40ish lines of codes 5 times changing some stuff around.
Is that the best way or is there a better way?
Thanks!