-
Arithematic operations
I want to evaluate a user entered string like the one below but how do I do that? I am not understanding how do I perform the arithematic operations maintaining the operators precedence.
Here's the problem :
34*45+56/(54*12)-25
some sort of like this.
-
Look up (on google) converting between postfix and infix expressions.
Although most of the time I'd just get the user to do that and make them put it in in the form:
34 45 * 56 54 12 * / + 25 -
(that *should* produce the right output, I can't test it since I've still got to recover the code for my stack evaluator).
Basically, you have everything on a stack, so you push 34 and 45 on, then use the * operator to replace those with (34*45). And so on. It's a lot easier for the computer to process because there's no way for it to be ambiguous.
-
Is there any other way apart from using stacks for it ? :rolleyes:
-
probably but stacks would be most efficient, and easiest for that matter :)