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.
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.
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.
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.