aspose file tools*
The moose likes Game Development and the fly likes Explicit precedence with parentheses Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Explicit precedence with parentheses" Watch "Explicit precedence with parentheses" New topic
Author

Explicit precedence with parentheses

Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

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!


===>SCJP 1.5(72%)<===
==>SCWCD1.5(76%)<===
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11498
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

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

Joined: Nov 09, 2008
Posts: 598

Busy busy busy...
Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

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

Joined: Oct 02, 2003
Posts: 11498
    
  16

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

Joined: May 13, 2009
Posts: 679
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

Joined: Nov 09, 2008
Posts: 598

Tell me about it!!My minesweeper used reverse recursion....

Instead of reducing to a base case, it exponentially increased the amount of cases!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explicit precedence with parentheses