This tool is designed to validate, translate, and evaluate mathematical expressions in prefix notation. It includes several features such as input validation, infix to prefix conversion, prefix expression evaluation, and overflow checking.
- Function: check_valid_input(expr)
- Description: Validates the given mathematical expression to ensure it follows the correct syntax for operations and parentheses usage.
- Function: infix_to_prefix(expression)
- Description: Converts a mathematical expression from infix notation to prefix notation.
The following table shows various infix expressions and their corresponding prefix conversions tested with the utility:
Infix Expression | Prefix Conversion |
---|---|
-b |
^b |
(a + b) |
+ab |
(x+y-z) |
-+xyz |
((x+y-z)/u+v) |
+/-+xyzuv |
((x+y-z)/u+v*w) |
+/-+xyzu*vw |
(-a + b) |
+^ab |
(x-(-y)+ z) |
+-x^yz |
a |
a |
Note: ^
is used to denote a unary minus for clarity in the prefix expressions.
- Function: evaluate_prefix(expression)
- Description: Evaluates a mathematical expression provided in prefix notation and returns the result.
Here are some example expressions and their results:
- Function: check_overflow(result)
- Description: Checks if the result of the expression evaluation is within the specified bounds. If an overflow occurs, it adjusts the result accordingly.
- Function: evaluate_expression(expression)
- Description: Evaluates a prefix expression and checks for overflow. Returns the final result or an overflow notification.
- Function: check_parentheses_balance(expr)
- Description: Verifies that the parentheses in the expression are correctly balanced.
- Checks for balanced parentheses, outputting an error message for unbalanced expressions.
To use this tool, simply run the main() function. The user will be prompted to enter a mathematical expression. The tool then performs the following steps:
- Validates the input expression.
- Checks for balanced parentheses.
- Converts the expression from infix to prefix notation if valid.
- Evaluates the prefix expression.
- Checks for and handles any overflow in the evaluation result.
- Displays the evaluation result or an error message if the input is invalid.