#C8139. Evaluate Mathematical Expression
Evaluate Mathematical Expression
Evaluate Mathematical Expression
You are given a string s
representing a mathematical expression. The expression contains non-negative integers (which may consist of multiple digits) and the operators +, -, *, and /. Your task is to evaluate this expression and output the result as an integer.
The rules for evaluation are as follows:
- Follow the standard operator precedence: multiplication and division are performed before addition and subtraction.
- All the operations are evaluated from left to right.
- Division between two integers should truncate toward zero. In other words, the quotient is the integer part of the result.
You can assume that the input expression is valid and does not contain any parentheses.
For example, using the formula below: \[ result = \sum_{i=1}^{n} term_i \] where each term is computed based on the operator preceding it, you should output a single integer corresponding to the final evaluated result.
inputFormat
The input is a single line string which represents the mathematical expression. The string may contain spaces.
Input Format: A single line read from standard input (stdin
).
outputFormat
Output an integer representing the result of evaluating the expression. Write the result to standard output (stdout
).
3+2*2
7