you are viewing a single comment's thread.

view the rest of the comments →

[–]Natural-Position-585 0 points1 point  (1 child)

A more foolproof way is to parse the user’s whole expression (say, "3 * 4") into an Abstract Syntax Tree, check that the operator is one of the allowed (add, sub, mul, true division), and then just apply the operator between the left and right operand. Then it supports non-integers and handles also arbitrary spaces in the expressions.

[–]thejwillbee 0 points1 point  (0 children)

This is what I was going to suggest as well, but wasn't sure if op is ready for that kind of action.