# 1960 MINCalculator
# 19
60 MIN
Overview
Build a simple calculator that supports basic arithmetic operations, decimal numbers, and negative values, while handling edge cases in user input.
Solution preview
Here's the result you'll build by completing this challenge.
Requirements
- Display the current expression as the user types.
- Evaluate mathematical expressions correctly when the equals button is pressed, respecting operator precedence (
*and/before+and-). - Support the four basic operations: addition, subtraction, multiplication, and division.
- Allow decimal numbers and negative numbers.
- Prevent invalid inputs or sequences:
- The expression cannot start with
+,*, or/. - The expression can start with
-to represent a negative number. - Disallow consecutive operators except when using
-to indicate a negative number. - Avoid multiple dots within the same number.
- Treat a lone
.at the start or right after an operator as0.(e.g.,.5→0.5). - Prevent evaluation when the last element is an operator or when the expression is empty.
- The expression cannot start with
- Handle negative numbers properly after any operator.
- Allow correcting a pending unary
-after an operator by pressing another operator (e.g.,5+-then+→5+). - Prevent more than two consecutive
-signs. - Replace leading zeros when typing new numbers (e.g.,
03→3). - Allow chaining of operations and continuous usage after evaluating a result:
- If a result is displayed and the user presses an operator, continue from that result.
- If a result is displayed and the user presses a number, start a new expression.
- Clear the display and reset the calculator when pressing
C.
Notes
- The display should show either the full expression or the result, depending on context.
- Expressions must update immediately as buttons are pressed.
- Consecutive operators must be handled gracefully (e.g., replacing the previous one if needed), including a pending unary
-(e.g.,5+-then+→5+). - Evaluation must respect operator precedence (
*and/before+and-). It can be implemented withoutevalusing a small expression parser. - Negative numbers after operators (e.g.,
5*-2) must be valid. - A lone dot is normalized to
0.both at the start and after an operator. - The calculator should handle decimal precision correctly (round results to two decimals if needed).
- Avoid any syntax errors or invalid evaluations when using
evalor equivalent. - Clearing should reset both the expression and the result.
Tests
- basic sum operation
- more complex sum operation
- does not allow '+' as the first value
- does not allow '*' as the first value
- does not allow '/' as the first value
- allows '-' as the first value (negative numbers)
- allow negative numbers after any operator
- prevents more than two consecutive '-' and allows negative numbers correctly
- handle leading zeros correctly
- if there is a result and a number or operator is pressed
- prevent multiple dots in the same number
- prevent consecutive operators
- replaces a pending negative sign when another operator is pressed
- treats a lone dot as 0.
- concatenate numbers/dots if the last element is a number
- prevent evaluating if the last element is an operator
- prevent evaluating if is empty
- clear