Calculator
Build a web calculator, that supports the basic operations of add, remove, multiply and divide.

In addition, support resetting the current state, as well as a decimal point button.

When you feel ready, you can validate your solution using any collaborative HTML/CSS renderer, such as this one.

Hints & Tips
Make sure your peers focuses first on functionality and layout, then optionally if time permits, on visual design
To make the best use of the time, suggest they first implement “add” end-to-end using HTML elements, and then expand
Suggest that your peer first try to think of ways the user might “break” the calculator, to not allow invalid inputs, e.g. “what happens if I click a number when the last digit is already a number? what about operations?”
Solution
https://codepen.io/prampcontent/pen/jvNyKR

DSA used

  1. Stack: Useful for implementing the calculator’s operation logic, especially for handling expressions in infix, postfix, or prefix notation. Stacks are commonly used for storing operands and operators and for implementing operations like evaluating expressions, handling parentheses, and managing operator precedence.
  2. Queue: You might use a queue for handling input events from the user interface, such as button presses or keystrokes. Queues can help in managing the sequence of input events and processing them in a first-in-first-out (FIFO) order.
  3. Tree: If your calculator supports complex mathematical expressions with operator precedence, you might use a tree data structure (such as an expression tree) to represent and evaluate the expressions. This allows for efficient evaluation by following the order of operations (e.g., PEMDAS/BODMAS.

Using postfix with Stack should do the job

  1. Conversion to Postfix: The first step is to convert the infix expression to postfix notation. Postfix notation (also known as Reverse Polish Notation, RPN) eliminates the need for parentheses and makes it easier to evaluate expressions using a stack. For the expression “3+2-1*8”:
    • Convert it to postfix: “3 2 + 1 8 * -”
  2. Evaluation using Stack: Once you have the expression in postfix notation, you can evaluate it using a stack.
    • Start scanning the expression from left to right.

    • If an operand is encountered, push it onto the stack.

    • If an operator is encountered, pop the required number of operands from the stack, perform the operation, and push the result back onto the stack.

    • Continue this process until you’ve processed the entire expression. For the postfix expression “3 2 + 1 8 * -”:

    • Start with an empty stack.

    • Push operands onto the stack:

      • Push 3 onto the stack.
      • Push 2 onto the stack.
      • Encounter ”+”: Pop 2 and 3 from the stack, add them, and push the result (5) back onto the stack.
      • Push 1 onto the stack.
      • Push 8 onto the stack.
      • Encounter ”*”: Pop 8 and 1 from the stack, multiply them, and push the result (8) back onto the stack.
      • Encounter ”-”: Pop 5 and 8 from the stack, subtract 8 from 5, and push the result (-3) back onto the stack.
  3. Result: After evaluating the entire expression, the final result (-3) will be on top of the stack.