Hints — p96 Basic Calculator II
-
Single-pass scan. Build
cur_numdigit by digit; defer applying the previous operator until you see the next non-digit char (or end). -
Deferred-operator stack: track
prev_op(initially+). When finalizingcur_num: ifprev_opis+/-, push signed; if*//, reduce against stack top in place. -
Final answer =
sum(stack)because*and/already collapsed; only additively-combinable terms remain. -
Truncation gotcha: Python’s
//rounds toward-inf. LC needs truncation toward zero. Useint(a / b). -
With parens (LC 224 / 772): on
(, push current(result, sign); on), pop and combine. Or write a recursive-descent parser.
If stuck: see solution.py.