Created
November 30, 2018 01:06
-
-
Save sairoko12/08d9dcee39d5a8dace8935aab87035b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
class Operaciones(): | |
def __init__(self, operation): | |
self.operaciones_disponibles = { | |
"+": self.suma, | |
"-": self.resta, | |
"/": self.division, | |
"*": self.multiplicacion | |
} | |
if not operation in self.operaciones_disponibles.keys(): | |
raise ValueError("No mams no soporto esa opción prro! >:v") | |
else: | |
self.operation = operation | |
def suma(self, numero_1, numero_2): | |
return numero_1 + numero_2 | |
def resta(self, numero_1, numero_2): | |
return numero_1 - numero_2 | |
def division(self, numero1, numero2): | |
return numero1 / numero2 | |
def multiplicacion(self, numero1, numero2): | |
return numero1 * numero2 | |
def execute(self, numero1, numero2): | |
return self.operaciones_disponibles[self.operation](numero1, numero2) | |
class Calculadora(): | |
def __init__(self, operation): | |
self.last_result = None | |
self.operation = operation | |
def execute(self): | |
operaciones = list() | |
valores = list() | |
for operacion in self.evalua_operaciones(): | |
if not str.isnumeric(operacion): | |
operaciones.append(operacion) | |
else: | |
try: | |
valor = int(operacion) | |
except ValueError: | |
raise ValueError("No es numero rufian >:v") | |
else: | |
valores.append(valor) | |
for operacion in operaciones: | |
if self.last_result == None: | |
numeros = valores[:2:1] | |
if len(numeros) < 2: | |
break | |
numero1 = numeros[0] | |
numero2 = numeros[1] | |
operacion_objecto = Operaciones(operacion) | |
self.last_result = operacion_objecto.execute(numero1, numero2) | |
del valores[:2:1] | |
else: | |
if len(valores) == 0: | |
break | |
numero1 = valores[0] | |
operacion_objeto = Operaciones(operacion) | |
# print("ultimo resultado {}".format(self.last_result)) | |
# print("numero 1 {}".format(numero1)) | |
# print(str(numeros)) | |
self.last_result = operacion_objecto.execute(self.last_result, numero1) | |
del valores[0] | |
return self.last_result | |
def evalua_operaciones(self): | |
return re.split("([+-/*])", self.operation.replace(" ", "")) | |
while True: | |
print() | |
operacion = input("Dame la operacion: ") | |
calculadora = Calculadora(operacion) | |
print(calculadora.execute()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment