Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

Explicit precedence with parentheses

Duran Harris
Ranch Hand
Posts: 608
Hi all,

I'm busy trying to code a calculator and I started with what I deemed to be the most difficult bit...precedence.
My idea is to have a recursive function that will analyze a string and return the expression with the highest precedence.I can then use this method in any calculate methods I write at a later stage.(Will also have to figure out how to remove brackets etc..)
Now what I am trying to figure out is with regards to using parentheses.I am trying to define the condition that marks a certain expression as having the highest priority.So far I have written different utility methods to assist me like methods to calulate how many opening/closing brackets precede/succeed a given char in a string.
What should my final test look like??

Would a subString starting from the char having the most preceding opening brackets and ending on the char having the most succeeding closing brackets be the innermost expression??No...it wouldn't...could someone assist me in defining the criteria for the innermost expression??
For example:

How would I determine the expression with the highest priority in:
((3x-12)+(2+(4-6))+2)
or
(2x6)-(2x3)

Sorry, my math is atrocious!

fred rosenberger
lowercase baba
Bartender
Posts: 12180
34
i don't know for sure, but... i think you need to assign precedence to the OPERATOR, not the character right after the paren... if you take this expression:

((3x-12)+(2+(4-6))+2)

start parsing, and count how many levels deep you are

now, do the operation with the highest priority. If there is more than one with equal depth, you need to follow the basic rules of the order of operations - exponents first, then mult. and div. in order left to right, then add/sub in order from left to right.

our deepest operator is 3, so we do that first

4 - 6 = -2

now do everything that is two levels deep, then one, then 0.

Duran Harris
Ranch Hand
Posts: 608
Oh my gosh....its a lot simpler than I thought...Thank you very much!Will start from scratch in a few days.Will let you know how it goes!

Duran Harris
Ranch Hand
Posts: 608
Busy busy busy...

Duran Harris
Ranch Hand
Posts: 608
Nice...I got shown how to implement this as a stack!Feel like a bit of a retard though considering I spent the whole day trying to get it right and someone else implemented it in 10min!

fred rosenberger
lowercase baba
Bartender
Posts: 12180
34
don't feel dumb. doing it 'the hard way' you still learned a lot. Then, seeing the 'easy' way you learned even more. it's never a waste to try and figure it out yourself first!!!

Fred Hamilton
Ranch Hand
Posts: 684
Duran Harris wrote:Nice...I got shown how to implement this as a stack!Feel like a bit of a retard though considering I spent the whole day trying to get it right and someone else implemented it in 10min!

Ha , that's nothing. I've been known to spend an entire weekend unsuccesfully trying to get implement a dozen lines of code in a recursive algorithm.

Duran Harris
Ranch Hand
Posts: 608
Tell me about it!!My minesweeper used reverse recursion....

Instead of reducing to a base case, it exponentially increased the amount of cases!