Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Knowing Operator Precedence


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Chad Smith   Members   -  Reputation: 1139

Like
0Likes
Like

Posted 14 October 2012 - 09:48 PM

Ok so I had a quick question on how some of y'all would attack this.

This is a school assignment so I won't be giving code nor do I really want any code. Just trying to get some ideas on some of the best ways to implement this small thing.

The school assignment really just focuses on us building our own stack, which I got done really fast and I like how it is designed (for a simple Linked List based Stack). We are using this stack for infix to postfix conversion which I also understand and can code. The only thing that is giving my brain fits right now is a good way to implement operator precedence. I need to check whether or not the operator I just read in the input is of higher precedence than the operator that is on top of the stack. What would be some ways to do some quick checking for this?

Even though I am not expecting code, C++ is the language. Though just some ideas on how to put this in code would be great.

If you have any question then just ask.

Thanks,
Chad

EDIT: to make things simpler right now we can ignore parenthesis

Edited by Chad Smith, 14 October 2012 - 09:50 PM.


Sponsor:

#2 Serapth   Crossbones+   -  Reputation: 5580

Like
0Likes
Like

Posted 14 October 2012 - 09:57 PM

This sounds like the classic RPN calculator assignment.

This answer shows you a C++ stack based implementation.

#3 Chad Smith   Members   -  Reputation: 1139

Like
0Likes
Like

Posted 15 October 2012 - 01:45 AM

Thanks, though that is not entirely it. The assignment really is just converting a very simple infix expression to a postfix expression

Example
infix: 3+2
postfix: 32+

or

infix: 8-3+2-1
postfix: 83-2+1-

When the operator (or just character as it is just a string) I am reading is of equal or lower precedence then I just pop the top element until it isn't, and then push it onto the stack.

As of right now I just decided to go ahead and do some if else if statements over each operator to see. They are pretty long though for a simple assignment that isn't focusing on operator parsing really then I believe it will be fine.

Though any other suggestions are nice. I will also take a look at the code in the link though to see if I can by chance learn something.

Chad

#4 Olof Hedman   Crossbones+   -  Reputation: 2896

Like
0Likes
Like

Posted 15 October 2012 - 01:51 AM

You could have a map where you map each operator to a priority-value.
Then just compare priority values in a single if




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS