Calculadora: e os números negativos? Solução técnica!
This commit is contained in:
@@ -3,7 +3,7 @@ from typing import TYPE_CHECKING
|
||||
|
||||
from PySide6.QtCore import Slot
|
||||
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
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -36,7 +36,7 @@ class ButtonsGrid(QGridLayout):
|
||||
['7', '8', '9', '*'],
|
||||
['4', '5', '6', '-'],
|
||||
['1', '2', '3', '+'],
|
||||
['', '0', '.', '='],
|
||||
['N', '0', '.', '='],
|
||||
]
|
||||
self.display = display
|
||||
self.info = info
|
||||
@@ -90,6 +90,9 @@ class ButtonsGrid(QGridLayout):
|
||||
if text == 'D':
|
||||
self._connectButtonClicked(button, self.display.backspace)
|
||||
|
||||
if text == 'N':
|
||||
self._connectButtonClicked(button, self._invertNumber)
|
||||
|
||||
if text in '+-/*^':
|
||||
self._connectButtonClicked(
|
||||
button,
|
||||
@@ -106,6 +109,16 @@ class ButtonsGrid(QGridLayout):
|
||||
func(*args, **kwargs)
|
||||
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()
|
||||
def _insertToDisplay(self, text):
|
||||
newDisplayValue = self.display.text() + text
|
||||
@@ -137,7 +150,7 @@ class ButtonsGrid(QGridLayout):
|
||||
# Se houver algo no número da esquerda,
|
||||
# não fazemos nada. Aguardaremos o número da direita.
|
||||
if self._left is None:
|
||||
self._left = float(displayText)
|
||||
self._left = converToNumber(displayText)
|
||||
|
||||
self._op = text
|
||||
self.equation = f'{self._left} {self._op} ??'
|
||||
@@ -150,7 +163,7 @@ class ButtonsGrid(QGridLayout):
|
||||
self._showError('Conta incompleta.')
|
||||
return
|
||||
|
||||
self._right = float(displayText)
|
||||
self._right = converToNumber(displayText)
|
||||
self.equation = f'{self._left} {self._op} {self._right}'
|
||||
result = 'error'
|
||||
|
||||
|
||||
@@ -7,6 +7,15 @@ def isNumOrDot(string: str):
|
||||
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):
|
||||
valid = False
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user