# Order of operations using stacks and queues

• 05-15-2012, 06:12 AM
ale626
Order of operations using stacks and queues
Greetings, this is my first post. Sorry if it is in the wrong section.

So my problem is making my calculator use order of operations, including parenthesis.
right now I got it to read regular input like 3 + 4 , or 4 / 3 + 4 * 3, etc
as of now it just reads it from left to right.
the problem im having is the order of operations, which ive been staring at for hours.
This is the part of my assignment im missing.

As it is, the expression evaluator can only evaluate expressions involving additions and subtractions. You will extend the evaluator to support multiplication and division, as well as parenthesized expressions. You will also add the feature of precedence on the operators, imposing the following precedence:

+, -
*
/
You are allowed to used recursion and Queues to solve these problems.

So im just stuck, i emailed my professor but he is bad with returning emails. Wondering if someone can help me with the general idea. Right now my program runs with stacks, if my code is required ill post, but i rather not as its not due till friday.
• 05-15-2012, 09:14 AM
JosAH
Re: Order of operations using stacks and queues
Google for "operator precedence parsing"; there are numerous examples; or you can read my blog article on expression compilation (see the Blogs button near the top of this page).

kind regards,

Jos
• 05-16-2012, 06:59 AM
ale626
Re: Order of operations using stacks and queues
Thanks for the tip!
i found a few examples, the java ones i did find used stacks whereas i need queues or recursion, and the ones that did use recursion are in other languages im struggling to understand.
do you mind being a bit specific?
• 05-16-2012, 07:45 AM
JosAH
Re: Order of operations using stacks and queues
For parsing infix expressions you don't need queues; stacks or recursion will do; have you read my blog entry? It is full of recursive methods ...

kind regards,

Jos
• 05-16-2012, 08:19 AM
ale626
Re: Order of operations using stacks and queues
i completely blanked on looking up your article, its the one titled compilers?
• 05-16-2012, 08:33 AM
JosAH
Re: Order of operations using stacks and queues
Yep, that's the one; it describes several implementations of (C-like) expression parsers and code generators.

kind regards,

Jos
• 05-16-2012, 08:49 AM
ale626
Re: Order of operations using stacks and queues
thanks, im reading it now, ill see how far i get into it tonight
• 05-16-2012, 08:54 AM
ale626
Re: Order of operations using stacks and queues
oh link to it is down, any chance of updating it?
• 05-16-2012, 08:56 AM
JosAH
Re: Order of operations using stacks and queues
Quote:

Originally Posted by ale626
oh link to it is down, any chance of updating it?

Strange, I just re-read my own blog article and everything worked fine ...

kind regards,

Jos
• 05-16-2012, 09:04 AM
ale626
Re: Order of operations using stacks and queues
i just get this when i click the link to the zip