In the textbox enter an expression (for example, 2 + 3-7). With certain functions Split I divide the expression into two types String array where digits are stored in the first and second arithmetic signs. a = {"2", "3", "7"} b = {"+", "-"} How to calculate this expression?
-
1Its just like using stacks for arithmetic evaluation. – ameyCU Aug 02 '15 at 13:29
-
If this question is not a duplicate of [Evaluating mathematical expressions in Python](http://stackoverflow.com/questions/5049489/evaluating-mathematical-expressions-in-python) then it is a duplicate of one of the very many similar questions that have been asked and answered here on SO. A little searching would bring rewards. – High Performance Mark Aug 02 '15 at 13:30
1 Answers
Infix Notation: Operators are written between the operands they operate on, e.g. 3 + 4 .
Infix Expressions are harder for Computers to evaluate because of the addional work needed to decide precedence. Infix notation is how expressions are written and recognized by humans and, generally, input to programs. Given that they are harder to evaluate, they are generally converted to one of the two remaining forms. A very well known algorithm for converting an infix notation to a postfix notation is Shunting Yard Algorithm by Edgar Dijkstra. This algorithm takes as input an Infix Expression and produces a queue that has this expression converted to a postfix notation. Same algorithm can be modified so that it outputs result of evaluation of expression instead of a queue. Trick is using two stacks instead of one, one for operands and one for operators.
For complete code refer - http://www.geeksforgeeks.org/expression-evaluation/

- 1,366
- 15
- 23
-
what type of data the stack use for the operators (+, -, *, /). – Владимир Нагорный Aug 02 '15 at 14:52