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.
4 1322
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 -
int x = 10 * 5 + 1;
-
//this will either be 60 or 51 depending on operator precendence right?
-
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: -
enum Operators
-
{
-
None = 0,
-
AdditionSubtraction = 1,
-
MultiplicationDivision = 5,
-
ExponentRoot = 10
-
}
-
-
static Operators GetOperatorWeight(char value)
-
{
-
if (value == '^')
-
return Operators.ExponentRoot;
-
else if (value == '*' || value == '/')
-
return Operators.MultiplicationDivision;
-
else if (value == '+' || value == '-')
-
return Operators.AdditionSubtraction;
-
else
-
return Operators.None;
-
}
-
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.
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?
Thanks everyone for the replays. I solved the problem myself using dictionary - private static Dictionary<string, int> OperatorPrecedence()
-
{
-
Dictionary<string, int> operators = new Dictionary<string, int>();
-
operators.Add("+", 1);
-
operators.Add("-", 1);
-
operators.Add("*", 2);
-
operators.Add("/", 2);
-
return operators;
-
}
-
-
-
and then compare the oprators like:
-
-
if (precedence[temp[i]] <= precedence[operStack.Peek()])
-
{
-
output.Enqueue(operStack.Pop());
-
}
... 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 :)
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
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...
|
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;
|
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...
|
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...
|
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...
|
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...
|
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
|
by: arnuld |
last post by:
/* C++ Primer - 4/e
* chapter 5 - Expressions
* exercises 5.1 and 5.2
*/
#include <iostream>
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |