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)
  • 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 to a, second to b, 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) 0
0
Output 1: incorrect value for square root operator

Input 2:
sqrt(-4)
(a + 3 * (b - 2) 2 1 2
0
Output 2: invalid bracket

Input 3:
sqrt(-4)
10 / (a - 5) 1 5
0
Output 3: divided by zero

Input 4:
sqrt(-4)
- a + b * 2 2 3 4
0
Output 4: 5.00
Input 5:
sqrt(-4)
abs(a + 1) * ((7 - b^2) / 3!) 2 -5 1
0
Output 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

© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.