← 返回 bytedance 的题目列表Implement an Infix Expression Evaluator (Calculator)
类型:online_judge
Problem: Implement an Infix Expression Evaluator (Calculator)
You are given an infix expression string s containing:
Non-negative integers (base-10, possibly multi-digit)
Operators: +, -, *, /
Parentheses: (, )
Spaces (may appear anywhere and should be ignored)
Implement a function/program to evaluate the expression and output the result.
Evaluation Rules
Operator precedence: * and / have higher precedence than + and -
Operators with the same precedence are left-associative
/ is integer division truncated toward 0
Parenthesized sub-expressions must be evaluated first
Input/Output
Input: one line string s
Output: one integer — the evaluated result
Constraints
1 <= len(s) <= 2 * 10^5
The expression is guaranteed to be valid
Intermediate results fit in 32-bit signed integer range
Examples
Example 1
Input:
3 + 2*2
Output:
7
Example 2
Input:
3/2
Output:
1
Example 3
Input:
(1+(4+5+2)-3)+(6+8)
Output:
23
Example 4
Input:
14-3/2
Output:
13
Example 5
Input:
2*(5+5*2)/3+(6/2+8)
Output:
21
Example
Input
3 + 2*2
Output
7