Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
pip install -r requirements.txt

- name: Run tests
run: pytest tests/ -v
run: pytest -v
5 changes: 4 additions & 1 deletion src/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
un punto de entrada principal.
"""

from .calculator import add, subtract, multiply, divide, power, valor_maximo, valor_minimo, abs_value
try:
from .calculator import add, subtract, multiply, divide, power, valor_maximo, valor_minimo, abs_value
except ImportError:
from calculator import add, subtract, multiply, divide, power, valor_maximo, valor_minimo, abs_value

def main():
"""Función principal del programa."""
Expand Down
62 changes: 42 additions & 20 deletions src/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
- Manejo de errores con mensajes visuales
"""
import tkinter as tk
from .calculator import (add, subtract, multiply, divide, power, valor_maximo, valor_minimo, abs_value)

try:
from .calculator import add, subtract, multiply, divide, power, valor_maximo, valor_minimo, abs_value
except ImportError:
from calculator import add, subtract, multiply, divide, power, valor_maximo, valor_minimo, abs_value

class CalculatorGUI:
def __init__(self, root):
Expand Down Expand Up @@ -193,29 +197,47 @@ def number_button_click(self, valor):


def decimal_click(self):
"""Maneja click del botón decimal.
"""Maneja click del botón decimal con validaciones mejoradas.

Corrige casos como:
"-" + "." → "-0."
"-.3" → "-0.3"

Agrega un punto decimal solo si no existe uno ya en el número actual.

Examples:
>>> "5" → click(.) → "5."
>>> "5." → click(.) → "5." (no cambia)
>>> "" → click(.) → "0."
Previene:
"-." como número inválido.
"""
# Validar que current_value sea válido antes de agregar punto
if self.current_value and self.current_value != '-':
try:
float(self.current_value)
except ValueError:
self.show_error("Número inválido")
return

if '.' not in self.current_value:
if not self.current_value:
self.current_value = "0"
self.current_value += '.'

# --- Caso 1: si el usuario presiona "." justo después de "-" ---
if self.current_value == "-":
# Autocompletar a -0.
self.current_value = "-0."
self.display.delete(0, tk.END)
self.display.insert(0, self.current_value)
return

# --- Caso 2: si no hay nada escrito, iniciar con "0." ---
if not self.current_value:
self.current_value = "0."
self.display.delete(0, tk.END)
self.display.insert(0, self.current_value)
return

# --- Caso 3: evitar doble punto ---
if '.' in self.current_value:
return

# --- Caso 4: validar número antes de agregar punto ---
try:
# Permitir cadenas como "5", "-3", "12"
float(self.current_value)
except ValueError:
self.show_error("Número inválido")
return

# --- Agregar el punto decimal ---
self.current_value += '.'
self.display.delete(0, tk.END)
self.display.insert(0, self.current_value)


def operation_click(self, operation):
Expand Down