#K80862. Arithmetic Expression Evaluator
Arithmetic Expression Evaluator
Arithmetic Expression Evaluator
You are given a string expression
representing a mathematical expression. The expression contains non-negative integers and the operators +, -, *, and /. Note that division is defined as integer division (i.e. truncating toward zero). The input may include arbitrary spaces, which should be ignored.
Formally, given an expression string S
, you need to compute the value of the expression. The evaluation follows the standard operator precedence rules, that is, multiplication and division are performed before addition and subtraction.
For example, when the input is 3+2*2
, the result is 7
, because the multiplication is performed before the addition.
The arithmetic operations can be described in LaTeX as follows:
\[ \text{result} = \sum_{i=1}^{n} a_i \quad \text{with proper handling of operators}\]
Your task is to implement a program that reads such an expression from standard input and outputs the evaluated integer result to standard output.
inputFormat
The input consists of a single line containing the arithmetic expression. The expression includes non-negative integers and the operators +
, -
, *
, and /
. Spaces may appear arbitrarily and should be ignored.
Input Format:
line 1: a string representing the arithmetic expression
outputFormat
Output a single integer: the result of evaluating the arithmetic expression.
Output Format:
result## sample
3+2*2
7