Hone logo
Hone
Problems

Simple Arithmetic Expression Interpreter

This challenge asks you to implement a basic interpreter for arithmetic expressions. The interpreter should evaluate expressions consisting of integers, addition (+), subtraction (-), multiplication (*), and division (/). This is a fundamental exercise in compiler design and demonstrates how to parse and evaluate simple languages.

Problem Description

You are tasked with creating a Go program that interprets arithmetic expressions provided as strings. The interpreter should parse the input string, identify the numbers and operators, and then evaluate the expression according to the standard order of operations (left to right, no parentheses). The interpreter should return the result of the evaluation as a float64.

Key Requirements:

  • Input: A string representing an arithmetic expression.
  • Operators: The interpreter must support addition (+), subtraction (-), multiplication (*), and division (/).
  • Numbers: The interpreter must handle integer numbers.
  • Evaluation: The expression must be evaluated from left to right.
  • Output: A float64 representing the result of the expression.
  • Error Handling: The interpreter should return 0.0 and print an error message to the console if the input is invalid (e.g., contains invalid characters, division by zero).

Expected Behavior:

The interpreter should correctly evaluate expressions of varying complexity, including those with multiple operators. It should handle positive and negative numbers.

Edge Cases to Consider:

  • Division by zero.
  • Invalid characters in the input string (e.g., letters, symbols other than +, -, *, /).
  • Empty input string.
  • Expressions starting or ending with operators.
  • Multiple consecutive operators.

Examples

Example 1:

Input: "2 + 3 * 4"
Output: 14.0
Explanation: 3 * 4 = 12, then 2 + 12 = 14

Example 2:

Input: "10 - 5 / 2"
Output: 7.5
Explanation: 5 / 2 = 2.5, then 10 - 2.5 = 7.5

Example 3:

Input: "5 + 2 - 1 * 3"
Output: 4.0
Explanation: 2 - 1 = 1, 1 * 3 = 3, 5 + 3 = 8.  (Left to right evaluation)

Example 4:

Input: "10 / 0"
Output: 0.0
Explanation: Division by zero results in an error, and the interpreter returns 0.0 and prints an error message.

Example 5:

Input: "abc + 5"
Output: 0.0
Explanation: Invalid input characters result in an error, and the interpreter returns 0.0 and prints an error message.

Constraints

  • The input string will contain only integers, the operators +, -, *, /, and spaces.
  • The integers in the expression will be non-negative.
  • The length of the input string will be between 1 and 100 characters.
  • The interpreter should handle division by zero gracefully, returning 0.0 and printing an error message.
  • Performance is not a primary concern for this challenge. Focus on correctness and clarity.

Notes

  • You can use the strconv package to convert strings to numbers.
  • Consider using a simple parsing approach, such as tokenizing the input string and then evaluating the tokens sequentially.
  • Error handling is crucial. Make sure your interpreter handles invalid input gracefully.
  • Remember that the evaluation should be left-to-right, without considering operator precedence. This simplifies the implementation.
  • The output should always be a float64 to handle division results accurately.
Loading editor...
go