This project is a simple calculator that evaluates expressions step by step as the user inputs values, rather than following traditional BODMAS rules. Instead of waiting for a complete mathematical expression, the calculator immediately updates the result after each operator is pressed.
The project relies on three main variables to store user inputs and five important flags to track user interactions and calculation states.
### When making impossible calculations
liveVar: Holds the current active number or result of calculations.secVar: Temporarily stores the second number in an operation.oprVar: Stores the selected operator (+, -, *, /, etc.).
isOprClicked: Tracks whether an operator has been pressed. This is crucial because whenisOprClickedistrue, any new number input should be stored insecVarinstead ofliveVar. It also ensures calculations happen only when an operator is followed by a second number.checkerForLive: Ensures numbers are displayed correctly when entered. WhencheckerForLiveistrue, the next number input completely replaces the current value on the display instead of appending to it. This prevents unwanted number concatenation and ensures a clean UI experience. It is reset tofalseafter the number is entered.checkerForEqualsInPer: Ensures that percentage operations apply correctly. Without this, percentages might be calculated incorrectly when used after the equals button.checkerForFlag: Prevents unintended duplicate calculations when switching operators after pressing=. Without it, switching operators could cause the last operation to repeat unintentionally.checkerForDelete: Determines whether a delete action should remove digits fromliveVarorsecVar, preventing unwanted changes to stored values.
Example: 2 + 4 =
- User inputs
2, which is stored inliveVar. - User presses
+. This:- Stores
+inoprVar. - Sets
isOprClickedtotrue, indicating that the next number should be stored insecVar. - Sets
checkerForLivetotrue, ensuring that the next number input replaces the existing value on the display.
- Stores
- User inputs
4, which is now stored insecVar(sinceisOprClickedistrue). - User presses
=. This triggers the calculation:2 + 4 = 6- The result (
6) is stored inliveVar. secVaris cleared for future calculations.checkerForLiveis set totrue, so if the user enters a new number, it replaces6instead of appending to it.
- The calculator is now ready for the next input.
Example: 12 * 4 - 81
- User inputs
12, stored inliveVar. - User presses
*, which:- Stores
*inoprVar. - Sets
isOprClickedtotrue. - Sets
checkerForLivetotrueto ensure the next number replaces the current display.
- Stores
- User inputs
4, stored insecVar. - User presses
-, triggering the multiplication:12 * 4 = 48- The result (
48) is stored inliveVar. secVaris cleared.oprVaris updated to-, preparing for the next operation.checkerForLiveis set totrue.
- User inputs
81, which is stored insecVar. - Further calculations follow the same pattern.
When a user presses =, then changes the operator, checkerForFlag prevents unnecessary recalculations.
Example: 2 + 5 = - 4
- Without
checkerForFlag,4might be subtracted twice, leading to incorrect results.
If the user presses % after an operation, it ensures the percentage is calculated relative to the total.
Example: 5 + 5 %
- Instead of treating
5%as0.05, the program correctly calculates10%of10, resulting in1.0.
If the user deletes a number, the program needs to know whether to modify liveVar or secVar.
- If
isOprClickedistrue, deletion should affectsecVar. - If
isOprClickedisfalse, deletion modifiesliveVar.
This project showcases an interactive approach to real-time calculations by prioritizing user input flow. The flag system ensures calculations behave predictably, providing a seamless experience for users. Through careful state management, the calculator allows for continuous operations, correct handling of percentages, and intuitive input correction.