This question evaluates a candidate's ability to implement expression parsing and evaluation, assessing competencies in string parsing, operator precedence, parentheses handling, and integer arithmetic semantics including truncation toward zero.
Write a function that evaluates a mathematical expression given as a string.
Requirements:
+
,
-
,
*
,
/
with standard precedence (
*
and
/
before
+
and
-
).
(
and
)
(if you choose to support them, document your approach).
Examples:
"3+2*2"
→ Output:
7
" 3/2 "
→ Output:
1
" 3+5 / 2 "
→ Output:
5
"2*(3+(4-1))"
→ Output:
12