 |
|
 |
I know it was mentioned only binary operators were okay (and not unary), but isn't it possible to use for one of them:
else if (token == operator_not) r = !(long)(op1);
Seems to work okay for me (so far).
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
You mean you want to offer a unicode version?
Well yes why not, If you have a website then post a message with a link to the unicode version.
Though the code is obsolete, since many people reported that they added and changed stuff on their own, but I did not combine all the changes. There are lots of C# and more advanced parser on CP now.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Hello
The code is outdated and served its demonstrative purpose. You probably can update it through a dirty quick patch via search/replace a "-" with "0-" to make it a binary operator.
I think other users fixed the issue.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
Hello jewelgal,
Thank you for your comments.
I hope it helps you too, however bear in mind that this you will only teach you how to eval expressions using RPN.
There are many other ways to *parse* and evaluate expressions.
Regards, Elias
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Do u know any method to check the parentheses before evaluate the expression using rpn? is rpn neans postfix expression too?
thx
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hello
Sorry your question is not clear.
> is rpn neans postfix expression too?
If you read the article, that what the first line says.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thanks for this, Elias. I've learned quite a bit from it. A suggestion: Get rid of the std::string. It slows this code down quite a bit (try stepping through it with F11 instead of F10). =(
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hello
Thanks for your comment.
The code is outdated and just for educational purpose.
Please feel free to modify as you like.
Regards, Elias
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
I think the use of std::string is fine - the only problem is that many functions pass the strings by value rather than by reference, e.g. isOperator(string op) would be much more efficient as isOperator( const string & op ).
Other than that this is a very handy example - thanks.
-Paul
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
When I try to make this operation:
23!=1
I get an "eval_invalidoperand"
Could anybody tell me what I´m doing wrong?I want to check if 23 is different of 1.
Thank you.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
Hello
This is a bug, it seems I forgot to include the "!" in the op[0] list, add it as:
{"!<>=*/%+-^&|\\", -1}, // Operators[0] is reserved for storing all symbols that can be used in operators
Regards, Elias
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I've been trying to figure out a way to handle cases such as these: -2^2=-4 and 2^-2=0.25 In the first case the exponentiation operator has precedence but in the second case the negation operator has precedence. This must be handled when converting from infix to postfix. Is there a simple solution to this problem. There are, of course, many ugly ways to solve this--just hoping for one of those solutions that makes me say "Now why didn't I think of that?"
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hello there,
A quick fix would be to create two operators the "^" and "^-" and two different handlers.
You handle the first as postive exponent while the second a negative exponent directly though the input number will be positive since the "-" will be part of the "^-" operator.
HTH Elias
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hadn't thought of using two different operators...
...anyway, here's how I solved it: while converting from infix to post fix when a '^' is on the top of the stack and a '-' (negation) is the current token, push it (negation operator) onto the stack. Otherwise, do everthing as described in the psuedo code.
|
| Sign In·View Thread·PermaLink | 3.00/5 (1 vote) |
|
|
|
 |
|
 |
This is pretty cool and is a great learning tool, however this doesn't take floats for inputs (it will only handle integers). To change this, get rid of the cast to (long) in evaluateRPN in the "// not an operator" block. Instead, cast d to type T when pushing it onto the stack.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
snip // not an operator if (idx < 0) { tokenLen = getToken(rpn.substr(i)); token = rpn.substr(i, tokenLen); char *errstr; double d = strtod(token.c_str(), &errstr); if (*errstr) return eval_invalidoperand; r = d; st.push(r); i += tokenLen - 1; continue; // not an operator if (idx < 0) { tokenLen = getToken(rpn.substr(i)); token = rpn.substr(i, tokenLen); char *errstr; double d = strtod(token.c_str(), &errstr); if (*errstr) return eval_invalidoperand; r = d; // <--- (A) st.push(r); i += tokenLen - 1; continue;
I tested the code again and it supports floats evaluation.
cccMangus wrote: , cast d to type T when pushing it onto the stac
Check the line (A).
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
It most certainly doesn't work on my platform without the cast (nor should it - the cast should be required in terms of C++ compliance), but I'm guessing its compiler related. I'm compiling with .NET 2003 (98% compliant). That said, with the fix, it works most excellent. Thanks for this! It saves me quite a bit of time. =)
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
 |
I intend to implement some single operand operation ( such as sin(), abs() etc) into this module, would you have time to hint me the directions? As the code is doing char by char, odinary form "ABS()" might be out of the question?
Thank you and Best wishes!
Kevin,
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I did a rewrite of this code some time ago, and have added functions as well as variables. If you are interested, take a look here[^].
-- modified at 9:28 Saturday 8th October, 2005
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hello
Kevin: Don't know if RPN can handle functions (especially in the way it parses), however I can think of writing a simple expression parser/evaluator (not RPN) based.
Just saw Stephane's article and it looks nice and well designed, I can safely recommend it!
|
| Sign In·View Thread·PermaLink | 2.00/5 (2 votes) |
|
|
|
 |