• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Explicit precedence with parentheses

 
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
lowercase baba
Posts: 12856
52
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Busy busy busy...
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12856
52
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tell me about it!!My minesweeper used reverse recursion....

Instead of reducing to a base case, it exponentially increased the amount of cases!
 
He baked a muffin that stole my car! And this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic