|
1 | 1 | """ |
2 | | -Class that handles the input and calculations |
| 2 | +Class that handles the input_key and calculations |
3 | 3 | """ |
4 | 4 | class Calculator: |
5 | 5 | def __init__(self, calc_display, clear_button, label_offset): |
6 | 6 | self._calc_display = calc_display |
7 | 7 | self._clear_button = clear_button |
8 | 8 | self._label_offset = label_offset |
| 9 | + self._accumulator = "0" |
| 10 | + self._operator = None |
| 11 | + self._equal_pressed = False |
| 12 | + self._operand = None |
9 | 13 | self.all_clear() |
10 | 14 |
|
11 | 15 | def calculate(self, number_one, operator, number_two): |
@@ -39,63 +43,72 @@ def _set_text(self, text): |
39 | 43 | def _get_text(self): |
40 | 44 | return self._calc_display.text |
41 | 45 |
|
42 | | - def add_input(self, input): |
| 46 | + def _handle_number(self, input_key): |
| 47 | + display_text = self._get_text() |
| 48 | + if self._operand is None and self._operator is not None: |
| 49 | + display_text = "" |
| 50 | + elif self._operand is not None and self._operator is not None and self._equal_pressed: |
| 51 | + self._accumulator = self._operand |
| 52 | + self._operator = None |
| 53 | + self._operand = None |
| 54 | + display_text = "" |
| 55 | + elif display_text == "0": |
| 56 | + display_text = "" |
| 57 | + display_text += input_key |
| 58 | + self._set_text(display_text) |
| 59 | + if self._operator is not None: |
| 60 | + self._operand = display_text |
| 61 | + self._set_button_ce(True) |
| 62 | + self._equal_pressed = False |
| 63 | + |
| 64 | + def _handle_operator(self, input_key): |
| 65 | + if input_key == "x": |
| 66 | + input_key = "*" |
| 67 | + if self._equal_pressed: |
| 68 | + self._operand = None |
| 69 | + if self._operator is None: |
| 70 | + self._operator = input_key |
| 71 | + else: |
| 72 | + # Perform current calculation before changing input_keys |
| 73 | + if self._operand is not None: |
| 74 | + self._accumulator = self.calculate(self._accumulator, self._operator, self._operand) |
| 75 | + self._set_text(self._accumulator) |
| 76 | + self._operand = None |
| 77 | + self._operator = input_key |
| 78 | + self._accumulator = self._get_text() |
| 79 | + self._equal_pressed = False |
| 80 | + |
| 81 | + def _handle_equal(self, input_key): |
| 82 | + if self._operator is not None: |
| 83 | + if self._operand is None: |
| 84 | + self._operand = self._get_text() |
| 85 | + self._accumulator = self.calculate(self._accumulator, self._operator, self._operand) |
| 86 | + self._set_text(self._accumulator) |
| 87 | + self._equal_pressed = True |
| 88 | + |
| 89 | + def add_input(self, input_key): |
43 | 90 | try: |
44 | | - if input == "AC": |
| 91 | + if input_key == "AC": |
45 | 92 | self.all_clear() |
46 | | - elif input == "CE": |
| 93 | + elif input_key == "CE": |
47 | 94 | self.clear_entry() |
48 | | - elif self._operator is None and input == "0": |
| 95 | + elif self._operator is None and input_key == "0": |
49 | 96 | pass |
50 | | - elif len(input) == 1 and 48 <= ord(input) <= 57: |
51 | | - display_text = self._get_text() |
52 | | - if self._operand is None and self._operator is not None: |
53 | | - display_text = "" |
54 | | - elif self._operand is not None and self._operator is not None and self._equal_pressed: |
55 | | - self._accumulator = self._operand |
56 | | - self._operator = None |
57 | | - self._operand = None |
58 | | - display_text = "" |
59 | | - elif display_text == "0": |
60 | | - display_text = "" |
61 | | - display_text += input |
62 | | - self._set_text(display_text) |
63 | | - if self._operator is not None: |
64 | | - self._operand = display_text |
65 | | - self._set_button_ce(True) |
66 | | - self._equal_pressed = False |
67 | | - elif input in ('+', '-', '/', 'x'): |
68 | | - if input == "x": |
69 | | - input = "*" |
70 | | - if self._equal_pressed: |
71 | | - self._operand = None |
72 | | - if self._operator is None: |
73 | | - self._operator = input |
74 | | - else: |
75 | | - # Perform current calculation before changing inputs |
76 | | - if self._operand is not None: |
77 | | - self._accumulator = self.calculate(self._accumulator, self._operator, self._operand) |
78 | | - self._set_text(self._accumulator) |
79 | | - self._operand = None |
80 | | - self._operator = input |
81 | | - self._accumulator = self._get_text() |
82 | | - self._equal_pressed = False |
83 | | - elif input == ".": |
84 | | - if not input in self._get_text(): |
85 | | - self._set_text(self._get_text() + input) |
| 97 | + elif len(input_key) == 1 and 48 <= ord(input_key) <= 57: |
| 98 | + self._handle_number(input_key) |
| 99 | + elif input_key in ('+', '-', '/', 'x'): |
| 100 | + self._handle_operator(input_key) |
| 101 | + elif input_key == ".": |
| 102 | + if not input_key in self._get_text(): |
| 103 | + self._set_text(self._get_text() + input_key) |
86 | 104 | self._set_button_ce(True) |
87 | 105 | self._equal_pressed = False |
88 | | - elif input == "+/-": |
| 106 | + elif input_key == "+/-": |
89 | 107 | self._set_text(self.calculate(self._get_text(), "*", "-1")) |
90 | | - elif input == "%": |
| 108 | + elif input_key == "%": |
91 | 109 | self._set_text(self.calculate(self._get_text(), "/", "100")) |
92 | | - elif input == "=": |
93 | | - if self._operator is not None: |
94 | | - if self._operand is None: |
95 | | - self._operand = self._get_text() |
96 | | - self._accumulator = self.calculate(self._accumulator, self._operator, self._operand) |
97 | | - self._set_text(self._accumulator) |
98 | | - self._equal_pressed = True |
| 110 | + elif input_key == "=": |
| 111 | + self._handle_equal(input_key) |
99 | 112 | except (ZeroDivisionError, RuntimeError): |
100 | 113 | self.all_clear() |
101 | 114 | self._set_text("Error") |
0 commit comments