Use design patterns to transform arithmetic expressions into a well-designed structure to calculate the answers.
For example:
$expression =
new Divider(
new Multiplier(
new Negate(
new Constant(5.0)
),
new Divider(
new Constant(9.0),
new Constant(6.0)
)
),
new Adder(
new Constant(7.0),
new Subtracter(
new Constant(2.0),
new Constant(1.5)
)
)
);
Use Composite design pattern:
Transform the question into a Composite structure: