 |
|
 |
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).
|
|
|
|
 |
|
 |
How about to offer a unicode version?
|
|
|
|
 |
|
 |
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.
|
|
|
|
 |
|
 |
negative numbers after operators seem to kill it... is this easily fixable?
|
|
|
|
 |
|
 |
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.
|
|
|
|
 |
|
 |
Wouldn't that mess up the - operator?
|
|
|
|
 |
|
 |
thankssssssssssssssssssssss
|
|
|
|
 |
|
 |
Im new to this site. I took ur example n hope it'll help me to understand abt evaluating expression.
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
Do u know any method to check the parentheses before evaluate the expression using rpn? is rpn neans postfix expression too?
thx
|
|
|
|
 |
|
 |
Hello
Sorry your question is not clear.
> is rpn neans postfix expression too?
If you read the article, that what the first line says.
|
|
|
|
 |
|
 |
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). =(
|
|
|
|
 |
|
 |
Hello
Thanks for your comment.
The code is outdated and just for educational purpose.
Please feel free to modify as you like.
Regards,
Elias
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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.
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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?"
|
|
|
|
 |
|
 |
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
|
|
|
|
 |
|
 |
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.
|
|
|
|
 |
|
 |
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.
|
|
|
|
 |
|
 |
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).
|
|
|
|
 |
|
 |
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. =)
|
|
|
|
 |
|
 |
How about writinh sin, cos, log, and other math functions?
|
|
|
|
 |
|
 |
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,
|
|
|
|
 |
|
 |
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
|
|
|
|
 |