← 返回 meta 的题目列表Evaluate Arithmetic Expression Without Parentheses
类型:online_judge
Problem: Evaluate an Arithmetic Expression Without Parentheses
Implement a function to evaluate an arithmetic expression containing only non-negative integers, spaces, and the operators + - * /.
The expression has no parentheses. Multiplication and division have higher precedence than addition and subtraction, and operators with the same precedence are evaluated from left to right.
If the expression is invalid, print Invalid.
Invalid cases include
Characters other than spaces, digits, and + - * /;
Invalid operator positions, such as 1++2, *1+2, or 1+;
Division by zero;
Empty expression.
Assumptions
Numbers are non-negative integers. Unary signs such as -1+2 are not supported and are considered invalid;
Division is real-number division;
If the final result is an integer, print it as an integer; otherwise print it as a decimal.
Input Format
One line string s, the expression.
Output Format
Print the evaluated result, or Invalid if the expression is invalid.
Constraints
1 <= len(s) <= 100000
Example
Input:
10/2+1*3+5
Output:
13
Example
Input
10/2+1*3+5
Output
13