#C14233. Expression Evaluator
Expression Evaluator
Expression Evaluator
Given a string that represents a mathematical expression including numbers, basic arithmetic operators (+
, -
, *
, /
) and parentheses, your task is to compute its value according to the standard rules of arithmetic operation precedence.
The input expression may contain extra whitespaces which should be ignored. However, if the expression contains any invalid characters (i.e. characters not among digits, +
, -
, *
, /
, (
, )
or .
) or is incomplete (for example, due to unbalanced parentheses), the program must output an error message. Specifically, output Invalid characters in the expression
for invalid characters and Incomplete expression
for expressions that cannot be fully parsed.
Note: Division is performed as floating-point division. If the result is an integer, output it without a decimal point.
The mathematical expression is evaluated using standard arithmetic rules and is represented in LaTeX format as follows:
\( expression = term \; (('+'|'-')\; term)* \)
\( term = factor \; (( '*' | '/' ) \; factor)* \)
\( factor = number \; | \; '(' \; expression \; ')' \)
inputFormat
The input is provided via standard input (stdin) and consists of a single line containing a valid mathematical expression.
outputFormat
Output the evaluated result to standard output (stdout). If the expression is invalid or incomplete, output the corresponding error message.
## sample3 + 5 * 2
13
</p>