Submit | All submissions | Best solutions | Back to list |
COMEXP - Complex expression calculator |
You are given an arithmetic expression in infix notation (standard mathematical notation). The expression may contain:
- Binary operators:
+
,-
,*
,/
,%
,^
- Unary operators:
- Unary minus (
-
) - Absolute value
abs
- Square root
sqrt
- Factorial
!
(postfix)
- Unary minus (
- Variables: lowercase English letters (
a
,b
,c
, ...) - Numbers: real numbers
- Parentheses
(
and)
The task is to evaluate the value of the given expression based on the provided variable values.
If the expression is invalid (unmatched parentheses, invalid syntax, unknown variable, invalid operand for an operator, division by zero, etc.), output an appropriate error message.
Error messages (exact text to print):
invalid bracket
– if parentheses are mismatched.
-
invalid expression
– if expression syntax is wrong or operand count is wrong. -
incorrect value for square root operator
– if taking the square root of a negative number. -
divided by zero
– if division or modulo by zero.
Input
- The first line contains the expression in infix notation.
- The second line contains an integer
n
— the number of variables in the expression (1 ≤ n ≤ 26
). - The next
n
lines each contain the value of variables in order: first value corresponds toa
, second tob
, and so on. Values can be integers or real numbers.
Output
- If the expression is valid and can be evaluated, output the value of the expression with exactly two decimal places.
- Otherwise, output the corresponding error message as described above.
Example
Input 1:sqrt(-4)sqrt(-4) 00Output 1: incorrect value for square root operator
Input 2:
sqrt(-4)(a + 3 * (b - 2) 2 1 20Output 2: invalid bracket
Input 3:
sqrt(-4)10 / (a - 5) 1 50Output 3: divided by zero
Input 4:
sqrt(-4)- a + b * 2 2 3 40Output 4: 5.00
Input 5:sqrt(-4)abs(a + 1) * ((7 - b^2) / 3!) 2 -5 10Output 5: 4.00
Added by: | khanhvh |
Date: | 2025-08-13 |
Time limit: | 1s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | C C++ 4.3.2 CPP JAVA PYTHON3 |