Basic Calculator

To solve this coding challenge, we aim to implement a basic calculator that evaluates arithmetic expressions given as strings. We'll need to parse and evaluate the expression without using any built-in functions like
eval()
, which directly interprets and executes strings as code.

Explanation

The problem is to evaluate a valid arithmetic expression provided as a string that includes digits, '+', '-', '(', ')', and spaces. It could involve:
  1. Addition and subtraction operators.
  2. Possible use of parenthesis to specify the precedence of operations.
  3. To evaluate this, we need an approach to handle these different parts of the expression correctly:
  4. Parse each character of the string.
  5. Handle numbers and build multi-digit numbers.
  6. Apply operations when encountering operators ('+', '-') or parenthesis.
  7. Use a stack to manage the current state and results when dealing with parenthesis.
  8. Strategy

  9. We'll iterate through the string character by character.
  10. Maintain a stack to store intermediate results and signs when encountering '(' and ')'.
  11. Use variables to keep track of the current number, result, and sign.
  12. On encountering a digit, build the complete number.
  13. On encountering an operator, update the result based on the current number and reset the number to zero.
  14. On encountering '(', push the current result and sign on the stack, and reset them for the new sub-expression within the parenthesis.
  15. On encountering ')', complete the evaluation for the parenthesis.
  16. Detailed Steps in Pseudocode

  17. Initialization :
    • Create an empty stack.
    • Initialize
      current_number
      to 0 to keep track of the ongoing number.
    • Initialize
      current_result
      to 0 to keep the ongoing result.
    • Initialize
      current_sign
      to 1 to manage the sign of the current ongoing number.
  18. Iteration :
    • Loop through each character in the string:
      • Digit :
        • Update
          current_number
          by shifting the previous digits of the number left (multiply by 10) and adding the new digit.
      • Operator ('+' or '-') :
        • Update
          current_result
          with
          current_sign * current_number
          .
        • Reset
          current_number
          to 0.
        • Update
          current_sign
          to 1 for '+' and -1 for '-'.
      • Left Parenthesis '(' :
        • Push the
          current_result
          and
          current_sign
          onto the stack.
        • Reset
          current_result
          to 0 and
          current_sign
          to 1 for new sub-expression evaluation.
      • Right Parenthesis ')' :
        • Update
          current_result
          with
          current_sign * current_number
          .
        • Reset
          current_number
          to 0.
        • Multiply
          current_result
          by the sign popped from the stack and add to the result popped from stack.
  19. End of Iteration :
    • If any
      current_number
      remains (i.e., an ongoing number at the end), add it to the
      current_result
      .
  20. Return the final result .

Pseudocode

                                            
# Initialize an empty stack to manage state across parenthesis levels
stack = []

# Initialization of current number to 0
current_number = 0

# Initialize current result to 0 to accumulate results
current_result = 0

# Initialize current sign to 1 to manage the positive and negative signs
current_sign = 1 

# Iterate through characters of the string to evaluate
for each character in expression_string:
    
    if the character is a digit:
        # Form the complete number by shifting digits left and adding the new digit
        current_number = current_number * 10 + integer value of the character
    
    else if the character is '+':
        # Accumulate the current result with sign-adjusted current number
        current_result += current_sign * current_number
        
        # Reset the current number to 0 for the next number
        current_number = 0
        
        # Set current sign to 1 for addition
        current_sign = 1
    
    else if the character is '-':
        # Accumulate the current result with sign-adjusted current number
        current_result += current_sign * current_number
        
        # Reset the current number to 0 for next number
        current_number = 0
        
        # Set current sign to -1 for subtraction
        current_sign = -1
    
    else if the character is '(':
        # Push the current result and sign onto the stack
        stack.push(current_result)
        stack.push(current_sign)
        
        # Reset state for new sub-expression inside the parenthesis
        current_result = 0
        current_sign = 1
    
    else if the character is ')':
        # Complete the result for the sub-expression
        current_result += current_sign * current_number
        
        # Reset current number
        current_number = 0
        
        # Multiply the result by the sign popped from the stack
        current_result *= stack.pop()
        
        # Add to the result popped from the stack
        current_result += stack.pop()

# Consider any number left unprocessed 
if current_number != 0:
    # Add final number to the result with its respective sign
    current_result += current_sign * current_number

# Return the final calculated result
return current_result

                                        
This pseudocode outlines the basic approach to implement the basic calculator, effectively dealing with digits, operators, and parentheses while ensuring the expected arithmetic operations are carried out correctly.