← 返回 bytedance 的题目列表Basic Calculator without Parentheses
类型:online_judge
Implement a simple calculator that supports addition (+), subtraction (-), multiplication (*), and division (/). The calculator input is a string consisting of non-negative integers and operators, separated by spaces, without parentheses. All integers are guaranteed to fit in a 32-bit integer range. The input is valid, so you don't need to handle division by zero.
Input
A string containing operands and operators, such as "3 + 5 * 2 - 8 / 4".
Output
An integer, which is the result of the calculation.
Examples
Input: "3 + 5 * 2 - 8 / 4" Output: 10
Input: "10 + 2 * 6" Output: 22
Input: "100 * 2 + 12" Output: 212
Input: "100 * ( 2 + 12 )" Output: 1400
Input: "100 * ( 2 + 12 / 4) " Output: 1400
Example
Input
3 + 5 * 2 - 8 / 4