#K5361. Evaluate Equations
Evaluate Equations
Evaluate Equations
You are given a sequence of equations. Each equation is given in the format:
\(variable = expression\)
where the expression can involve integer constants, basic arithmetic operators (+, -, *, /) and variables defined in previous equations. The equations are processed sequentially. For each equation, if the evaluation of the expression leads to an error (for example, division by zero or usage of an undefined variable), then that equation is marked as invalid. Otherwise, it is marked as valid and the computed value is stored for potential use in subsequent equations.
The arithmetic expressions follow standard operator precedence rules. Note that if a division by zero is encountered in any operation (even indirectly), the corresponding equation should be considered invalid. The output is a list indicating the validity of each equation in order.
inputFormat
The input is read from standard input (stdin). The first line contains a single integer \(n\), denoting the number of equations. Each of the following \(n\) lines contains an equation in the format variable = expression
, where the variable is a string (typically a single letter) and the expression can include integers, previously defined variables, and the operators +, -, *, and /.
outputFormat
For each equation, output a line to standard output (stdout) containing either True
if the equation is evaluated successfully, or False
if an error occurred (such as division by zero or use of an invalid variable).
4
a = 1 + 1
b = a - 1
c = a * b
d = c / a
True
True
True
True
</p>