Calculadora: e os números negativos? Solução técnica!

This commit is contained in:
Luiz Otávio
2023-03-04 10:02:42 -03:00
parent f74e1d191d
commit 1287ce5b5c
2 changed files with 26 additions and 4 deletions

View File

@@ -3,7 +3,7 @@ from typing import TYPE_CHECKING
from PySide6.QtCore import Slot from PySide6.QtCore import Slot
from PySide6.QtWidgets import QGridLayout, QPushButton from PySide6.QtWidgets import QGridLayout, QPushButton
from utils import isEmpty, isNumOrDot, isValidNumber from utils import converToNumber, isEmpty, isNumOrDot, isValidNumber
from variables import MEDIUM_FONT_SIZE from variables import MEDIUM_FONT_SIZE
if TYPE_CHECKING: if TYPE_CHECKING:
@@ -36,7 +36,7 @@ class ButtonsGrid(QGridLayout):
['7', '8', '9', '*'], ['7', '8', '9', '*'],
['4', '5', '6', '-'], ['4', '5', '6', '-'],
['1', '2', '3', '+'], ['1', '2', '3', '+'],
['', '0', '.', '='], ['N', '0', '.', '='],
] ]
self.display = display self.display = display
self.info = info self.info = info
@@ -90,6 +90,9 @@ class ButtonsGrid(QGridLayout):
if text == 'D': if text == 'D':
self._connectButtonClicked(button, self.display.backspace) self._connectButtonClicked(button, self.display.backspace)
if text == 'N':
self._connectButtonClicked(button, self._invertNumber)
if text in '+-/*^': if text in '+-/*^':
self._connectButtonClicked( self._connectButtonClicked(
button, button,
@@ -106,6 +109,16 @@ class ButtonsGrid(QGridLayout):
func(*args, **kwargs) func(*args, **kwargs)
return realSlot return realSlot
@Slot()
def _invertNumber(self):
displayText = self.display.text()
if not isValidNumber(displayText):
return
number = converToNumber(displayText) * -1
self.display.setText(str(number))
@Slot() @Slot()
def _insertToDisplay(self, text): def _insertToDisplay(self, text):
newDisplayValue = self.display.text() + text newDisplayValue = self.display.text() + text
@@ -137,7 +150,7 @@ class ButtonsGrid(QGridLayout):
# Se houver algo no número da esquerda, # Se houver algo no número da esquerda,
# não fazemos nada. Aguardaremos o número da direita. # não fazemos nada. Aguardaremos o número da direita.
if self._left is None: if self._left is None:
self._left = float(displayText) self._left = converToNumber(displayText)
self._op = text self._op = text
self.equation = f'{self._left} {self._op} ??' self.equation = f'{self._left} {self._op} ??'
@@ -150,7 +163,7 @@ class ButtonsGrid(QGridLayout):
self._showError('Conta incompleta.') self._showError('Conta incompleta.')
return return
self._right = float(displayText) self._right = converToNumber(displayText)
self.equation = f'{self._left} {self._op} {self._right}' self.equation = f'{self._left} {self._op} {self._right}'
result = 'error' result = 'error'

View File

@@ -7,6 +7,15 @@ def isNumOrDot(string: str):
return bool(NUM_OR_DOT_REGEX.search(string)) return bool(NUM_OR_DOT_REGEX.search(string))
def converToNumber(string: str):
number = float(string)
if number.is_integer():
number = int(number)
return number
def isValidNumber(string: str): def isValidNumber(string: str):
valid = False valid = False
try: try: