0

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!

Natantantan
  • 47
  • 2
  • 8

3 Answers3

3

It is a classic problem. If you notice languages themselves implement this. You might want to read shunting yard algo https://en.wikipedia.org/wiki/Shunting-yard_algorithm

Hamed Moghaddam
  • 564
  • 3
  • 8
  • Good answer, but probably should be a comment since answers that consist basically of only a link are generally frowned upon. – ajb Dec 03 '14 at 02:28
  • Thanks. Good to know. Did not mean that in anyway. Just wanted to give a higher level answer to Natantantan. That is what I like to get when I ask questions. – Hamed Moghaddam Dec 03 '14 at 02:35
  • I read that short wiki page and I didn't really understand it. This is for my intro to CSE class so maybe its beyond my scope? – Natantantan Dec 03 '14 at 02:39
  • Sorry I did not know it is for class. General problem solves by defining order of operators and then move through the expression and run the algo which is in that page. Your problem is easier a bit if it is only / * and (). You can easily find Java implementation of it online. But because it is homework it is nice if you have time give it some try. Simple solution for you is that only be able to figure out when parentheses operators gets closed – Hamed Moghaddam Dec 03 '14 at 02:52
  • basically start reading parentheses. sum up as many ( that you see and whenever you see a ) gets to the last ( and do the calculation. keeps the number and now you have a shorter expression to go. You can write the program recursively as well – Hamed Moghaddam Dec 03 '14 at 02:55
  • @Natantantan I was hoping you could scroll down to "Detailed Example" and get an idea from that. Your problem may be simpler than the example if everything is parenthesized and you don't have to worry about things like `A+B*C`. But even with that simplification it doesn't seem like a simple problem for an intro class. Do you know what a stack is--have you learned that much? – ajb Dec 03 '14 at 03:27
0

Assuming I had total control over the input. I would just find/replace the letters in the formula with their number value and then eval that string via ScriptEngine as outlined here. There's certainly danger with this and you have to be careful, but if that's possible then this seems like a way easier solution where the computer is actually doing the hard work.

Community
  • 1
  • 1
Jack
  • 20,735
  • 11
  • 48
  • 48
0

What about creating a postFix expression using stack and then calculating it as there will not be any paranthesis in postfix expression.

Ouney
  • 1,164
  • 1
  • 10
  • 22