473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Comparing operators for precedence

6
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.
Aug 2 '08 #1
4 1322
Plater
7,872 Expert 4TB
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.  
Aug 4 '08 #2
TRScheel
638 Expert 512MB
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.
Aug 4 '08 #3
Plater
7,872 Expert 4TB
So I looked up shunting yard and well, couldn't figure out what it was useful for?
Maybe I didn't read it enough?

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?
Aug 5 '08 #4
eeasss
6
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 :)
Aug 5 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Gerrit Holl | last post by:
Hi, is it proper to compare booleans? It is possible, of course, because they're compatible with numbers, but booleans aren't truly numbers. I'm tempted to write: return cmp(self.extends,...
27
by: glen herrmannsfeldt | last post by:
The subject recently came up in comp.compilers, though I believe that I asked here before. If you use relational operators, other than == and !=, on pointers to different objects, is there any...
25
by: August Karlstrom | last post by:
Hi, Can someone explain the reason for the warning from GCC below: <shell-session> $ cat test.c #include <stdbool.h> bool a, b, c, d;
17
by: Steve R. Hastings | last post by:
I have been studying Python recently, and I read a comment on one web page that said something like "the people using Python for heavy math really wish they could define their own operators". The...
48
by: spibou | last post by:
This concerns the Wikipedia article on C and C++ operators: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B Until very recently the first table in the page was a very useful one on the...
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
28
by: dspfun | last post by:
I'm trying to get a good understanding of how unary operators work and have some questions about the following test snippets. int *p; ~!&*++p--; It doesn't compile, why? The problem seems to be...
8
by: subramanian100in | last post by:
What does "associativity of operators" mean ? I am unable to undersatand this from K & R 2nd edition. Kindly explain with an example. Thanks
7
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 5 - Expressions * exercises 5.1 and 5.2 */ #include <iostream>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.