@@ -9,11 +9,11 @@ use rui::*;
9
9
/// Represents the state of the clear button functionality.
10
10
/// Used to toggle between Clear (C) and All Clear (AC) modes.
11
11
#[ derive( PartialEq , Clone , Copy ) ]
12
- enum ClearState {
13
- /// Initial state - displays "C"
14
- Initial ,
15
- /// State after clearing - displays "AC"
16
- JustCleared ,
12
+ enum ClearMode {
13
+ /// Initial state - displays "C", only clears current input
14
+ OnlyInput ,
15
+ /// State after clearing - displays "AC", resets all calculator state
16
+ FullReset ,
17
17
}
18
18
19
19
/// Represents the basic arithmetic operations supported by the calculator.
@@ -362,7 +362,7 @@ impl Calculator {
362
362
} ,
363
363
Button :: Special ( action) => match action {
364
364
SpecialOperator :: Clear => {
365
- if cx[ s] . clear_state == ClearState :: JustCleared {
365
+ if cx[ s] . clear_mode == ClearMode :: FullReset {
366
366
"AC" . to_string ( )
367
367
} else {
368
368
"C" . to_string ( )
@@ -404,7 +404,7 @@ pub struct CalculatorState {
404
404
/// The last operator used (for repeat calculations)
405
405
last_operator : Option < Operator > ,
406
406
/// Current state of the clear button (C/AC)
407
- clear_state : ClearState ,
407
+ clear_mode : ClearMode ,
408
408
}
409
409
410
410
impl CalculatorState {
@@ -417,7 +417,7 @@ impl CalculatorState {
417
417
has_error : false ,
418
418
is_result_displayed : false ,
419
419
last_operator : None ,
420
- clear_state : ClearState :: Initial ,
420
+ clear_mode : ClearMode :: OnlyInput ,
421
421
}
422
422
}
423
423
@@ -435,13 +435,7 @@ impl CalculatorState {
435
435
Some ( Operator :: Add ) => first_operand + second_operand,
436
436
Some ( Operator :: Subtract ) => first_operand - second_operand,
437
437
Some ( Operator :: Multiply ) => first_operand * second_operand,
438
- Some ( Operator :: Divide ) => {
439
- if second_operand == 0.0 {
440
- self . has_error = true ;
441
- return ; // Early return on division by zero
442
- }
443
- first_operand / second_operand
444
- }
438
+ Some ( Operator :: Divide ) => first_operand / second_operand,
445
439
None => return , // Handle the case where no operator is set
446
440
} ;
447
441
@@ -511,7 +505,7 @@ impl CalculatorState {
511
505
self . is_input_new = true ;
512
506
self . has_error = false ;
513
507
self . is_result_displayed = false ;
514
- self . clear_state = ClearState :: Initial ;
508
+ self . clear_mode = ClearMode :: OnlyInput ;
515
509
}
516
510
517
511
/// Processes button presses and updates calculator state accordingly.
@@ -523,10 +517,10 @@ impl CalculatorState {
523
517
| Button :: Special ( SpecialOperator :: Percentage )
524
518
| Button :: Special ( SpecialOperator :: Equals )
525
519
| Button :: Special ( SpecialOperator :: Decimal ) => {
526
- if self . clear_state == ClearState :: JustCleared {
520
+ if self . clear_mode == ClearMode :: FullReset {
527
521
self . reset ( ) ;
528
522
} else {
529
- self . clear_state = ClearState :: Initial ;
523
+ self . clear_mode = ClearMode :: OnlyInput ;
530
524
}
531
525
532
526
match button {
@@ -558,16 +552,20 @@ impl CalculatorState {
558
552
}
559
553
}
560
554
Button :: Special ( SpecialOperator :: Clear ) => {
561
- if self . second_operand = = "0"
562
- && self . first_operand == "0"
563
- && self . current_operator . is_none ( )
555
+ if self . second_operand ! = "0"
556
+ || self . current_operator . is_some ( )
557
+ || self . is_result_displayed
564
558
{
565
- self . reset ( ) ;
566
- self . clear_state = ClearState :: JustCleared ;
567
- } else {
559
+ // If there's any ongoing state, first clear the current input
568
560
self . second_operand = "0" . to_string ( ) ;
561
+ self . current_operator = None ;
569
562
self . is_input_new = true ;
570
- self . clear_state = ClearState :: Initial ;
563
+ self . is_result_displayed = false ;
564
+ self . clear_mode = ClearMode :: FullReset ;
565
+ } else {
566
+ // If already cleared, perform a full reset
567
+ self . reset ( ) ;
568
+ self . clear_mode = ClearMode :: OnlyInput ;
571
569
}
572
570
}
573
571
}
0 commit comments