Evaluate an expression?

Sep 7, 2009 at 10:31am
Greetings!

I'm working in an application wich must evaluate a mathematical expression, I have the following expresion: A + B + C, the problem is that when I use strtok("A+B+C"," + - * /"), I only get the Operands, and I also need the operators.

Does anybody knows how to solve this?
Sep 7, 2009 at 11:40am
I would use std::string, .find() and .substr() instead, as they are less error prone and easier to use (IMO).
Sep 7, 2009 at 12:59pm
stack can help you.
YOu nees two stacks.
And you should convert it to post-fixed expression
Sep 10, 2009 at 5:04am
I'm working in an application wich must evaluate a mathematical expression, I have the following expresion: A + B + C, the problem is that when I use strtok("A+B+C"," + - * /"), I only get the Operands, and I also need the operators.


Do you mean you are only using operator+ and operator- as in A+B+C or A-B-C or do you also intend to use operator* and operator/. If the latter and you expect the listing to dynamically compute for example 2+3.0/7+6.0*11.3-2.5/5.1 you are making an arithmetic calculator and heading into difficult waters because you can`t rely on establishing correct precedence without using parenthesis
Make your objectives a bit clearer and we`ll see what happens when the experts get hold of it.
Sep 10, 2009 at 12:29pm
If your intent is to create a more general purpose expression evaluator, then using a standard parsing
technique is advisable. This means parsing the string left to right, character by character. With strtok()
you will be hard pressed to implement proper operator precedence and parenthetical expressions.
Sep 10, 2009 at 8:45pm
Well, Thank you all. I've already solved the problem of parsing the mathematical expression with a class written in C++. Now my problem is evaluating the expression.
In this particular case, this would be ((A + B) + C), but if I change the seccond operator,(A+B*C), it would be: (A+(B*C)). I know I should implement the procedures to evaluate it as a human would do, but I really don't know where to start.

Topic archived. No new replies allowed.