0

Arithmetic expression input

I want create a programm that its input is an arithmetic expression like "2*(7+4/5)" and then it gets the result. Could you help me please?

23rd May 2018, 4:29 PM
Ali
Ali - avatar
9 Réponses
+ 1
Depends on how far you want to go but it's not as easy as it looks. You have parentheses to worry about and also operator precedence (that means that for example in 4+3*6, the multiplication is done first, so you can't just read the string left-to-right). You can look into so-called "recursive descent parsers", those are the most simple to implement. There is a related but similar problem that is a lot easier to program, so you could give that a try before tackling the hard one: Parsing reverse polish notation (RPN). That means that you don't input an arithmetic expression like you wrote, but rather one where you put the operator *after* the operands. Examples: "5 6 +" stands for "5 + 6" "5 6 + 3 *" stands for "(5 + 6) * 3" "1 2 + 3 4 + *" stands for "(1 + 2) * (3 + 4)" It's cool because you can represent any maths expression without needing parentheses. And it's very easy to implement: [post too long]
23rd May 2018, 5:10 PM
Schindlabua
Schindlabua - avatar
+ 1
[cont.] Read the string from left to right. Have an empty array ready (a stack technically but an array will do). Everytime you read a number, add it to the array. Everytime you read an operator, take the last two values from the array, remove them, do the operation (+, *, /, etc), and add the result to the array. Whatever is left in the array when you're done is the answer.
23rd May 2018, 5:12 PM
Schindlabua
Schindlabua - avatar
+ 1
Meir, check out the `exec` function, it does just that! Be careful though, running arbitrary code is potentially dangerous.
28th May 2018, 10:02 AM
Schindlabua
Schindlabua - avatar
+ 1
It's easy to do on a piece of paper! It's mental gymnastics but you can translate any RPN expression into a regular ("infix") one and vice versa, RPN is super easy to parse though. Here's an example; we start with an empty stack and an RPN expression. Numbers push themselves onto the stack, operations pop two numbers and push the result. stack [], expr "8 1 2 + * 2 +" stack [8], expr "1 2 + * 2 +" stack [8,1], expr "2 + * 2 +" stack [8,1,2], expr "+ * 2 +" stack [8,3], expr "* 2 +" stack [24], expr "2 +" stack [24,2], expr "+" stack [26], we are done So turns out RPN "8 1 2 + * 2 +" is equivalent to infix "(8*(1+2))+2". Order of operations in RPN is simply left to right :)
28th May 2018, 12:32 PM
Schindlabua
Schindlabua - avatar
+ 1
So it pushes back the problemone step. In the OP, Ali wanted to enter mathematical expressions like we write them normally, and have the program evaluate them. But using a stack this way, you still have to get the expression into a form. you can nicely evaluate. No? What am I missing?
28th May 2018, 4:46 PM
Meir-Simchah
Meir-Simchah - avatar
+ 1
Yeah I was just saying, it's an easier problem for him to tackle. RPN is not uncommon in the wild by the way, the FORTH programming langue from 1970 is a good example. If he wants to do infix expressions with operator precedence and all that, he'll have to look a bit deeper (recursive descent for example, which I wrote in my first post). It's just a lot harder to do.
28th May 2018, 6:53 PM
Schindlabua
Schindlabua - avatar
0
You want create a parser that transform symbols and tokens in instructions that your code can understand and execute... You can do it easly with if/else/for/while etc or using a more laborious method like creating a lexer and make a tree of symbols
23rd May 2018, 4:40 PM
KrOW
KrOW - avatar
0
Is there a way to get the string input and have it read and evaluated in the program as if it were a line of code? I can do this is Matlab, but don't know how in Python...
28th May 2018, 4:09 AM
Meir-Simchah
Meir-Simchah - avatar
0
didn't understand how the stack is supposed to work - how will it handle order of operations?
28th May 2018, 10:06 AM
Meir-Simchah
Meir-Simchah - avatar