Connecting Tech Pros Worldwide Forums | Help | Site Map

Comparing operators for precedence

Newbie
 
Join Date: Sep 2007
Posts: 6
#1: Aug 2 '08
Hi all, i am writing an impementation of the shunting yard algorithm for practicing basic data structures but i have encountered the following problem: I need to compare two operators in terms of precedence. Is this possible in C#?
Example * > + should return true.

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#2: Aug 4 '08

re: Comparing operators for precedence


I am not sure there is any method that will tell you what operator has presidence, unless you can write your own test cases? I believe they are implemented following mathmatical standards and are thus effected by use of ( ) as well.

Such as
Expand|Select|Wrap|Line Numbers
  1. int x = 10 * 5 + 1;
  2. //this will either be 60 or 51 depending on operator precendence right?
  3.  
TRScheel's Avatar
Expert
 
Join Date: Apr 2007
Location: Iowa
Posts: 624
#3: Aug 4 '08

re: Comparing operators for precedence


Based off my readings of the shunting yard algorithm (which is pretty cool imho) this should get you on the right road to doing what you want:

Expand|Select|Wrap|Line Numbers
  1. enum Operators
  2. {
  3.     None = 0,
  4.     AdditionSubtraction = 1,
  5.     MultiplicationDivision = 5,
  6.     ExponentRoot = 10
  7. }
  8.  
  9. static Operators GetOperatorWeight(char value)
  10. {
  11.     if (value == '^')
  12.         return Operators.ExponentRoot;
  13.     else if (value == '*' || value == '/')
  14.         return Operators.MultiplicationDivision;
  15.     else if (value == '+' || value == '-')
  16.         return Operators.AdditionSubtraction;
  17.     else
  18.         return Operators.None;
  19. }
  20.  
Note that you would pass the equation one char at a time. You will have to fill in the logic/expand for other symbols and numbers.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#4: Aug 5 '08

re: Comparing operators for precedence


So I looked up shunting yard and well, couldn't figure out what it was useful for?
Maybe I didn't read it enough?

Quote:
Input : 3 + 4 * 2 / ( 1 − 5 ) ^ 2 ^ 3
Output: 3 4 2 * 1 5 - 2 3 ^ ^ / +
What does that tell us?
Work left-right on numbers and right-left on operators?
Newbie
 
Join Date: Sep 2007
Posts: 6
#5: Aug 5 '08

re: Comparing operators for precedence


Thanks everyone for the replays. I solved the problem myself using dictionary

Expand|Select|Wrap|Line Numbers
  1.    private static Dictionary<string, int> OperatorPrecedence()
  2.         {
  3.             Dictionary<string, int> operators = new Dictionary<string, int>();
  4.             operators.Add("+", 1);
  5.             operators.Add("-", 1);
  6.             operators.Add("*", 2);
  7.             operators.Add("/", 2);
  8.             return operators;
  9.         }
  10.  
  11.  
  12. and then compare the oprators like:
  13.  
  14.    if (precedence[temp[i]] <= precedence[operStack.Peek()])
  15.                         {
  16.                             output.Enqueue(operStack.Pop());
  17.                         }
... etc

where temp[i] is the array of my tokens. By the way the enumeration thing looks also pretty good, i might try it.

@ plater - shunting yard is used when u want to convert expressions from infix to postfix notation. This is usefull for calculators for example, because it eliminates the need for parenthesis and thus the calculations are easier :)
Reply


Similar .NET Framework bytes