diff options
author | Cristian García <cristian99garcia@gmail.com> | 2013-09-05 23:00:54 (GMT) |
---|---|---|
committer | Cristian García <cristian99garcia@gmail.com> | 2013-09-05 23:00:54 (GMT) |
commit | 0a57eb42473cac6adb040c0428b511342f823c13 (patch) | |
tree | 03d67b594ee0d53d4045e82f4a99adc45d8d9ab1 | |
parent | 249af5220f81e410bbc06ab8f0c0eb0f3b458f45 (diff) |
Emprolijando casi todo el codigo
-rw-r--r-- | Calculadora.py | 198 |
1 files changed, 103 insertions, 95 deletions
diff --git a/Calculadora.py b/Calculadora.py index bb2faf8..4f59e15 100644 --- a/Calculadora.py +++ b/Calculadora.py @@ -56,14 +56,14 @@ class Calculadora(): if operacion: if not 'x' in operacion.lower(): - print self.operar(operacion) + print self.reducir_polinomios(operacion) else: if '=' in operacion: - self.buscar_semejantes(operacion) + print self.resolver_ecuacion(operacion) else: - self.buscar_semejantes(operacion) + print self.reducir_polinomios(operacion) def operar(self, operacion): @@ -77,123 +77,131 @@ class Calculadora(): return resultado - def resolver_ecuacion(self, operacion): + def resolver_ecuacion(self, ecuacion): - pass + miembros = ecuacion.split('=') + miembro1 = self.reducir_polinomios(miembros[0])[0] + miembro2 = self.reducir_polinomios(miembros[1])[0] + miembro1_str = '' + miembro2_str = '' - def buscar_semejantes(self, operacion): + for x in miembro1: + for x2 in miembro1[x]: + miembro1_str += x2 - def agregar_operacion(lista, operacion): + for x in miembro2: + for x2 in miembro2[x]: + miembro2_str += x2 + + ecuacion = miembro1_str + '=' + miembro2_str + return ecuacion - numero = 0 - for x in lista: - if_int = str - - try: - int(x) - if_int = int + def limpiar_valor(self, valor): - except: - if_int = str + if type(valor) == str: + if ' ' in valor: + valor = valor.replace(' ', '') - if ('x' in x or if_int == int) and not x in OPERACIONES: - lista.insert(numero, operacion) - print lista + elif type(valor) == list and len(valor) >= 1: + while valor[0] in OPERACIONES: + valor.remove(valor[0]) - numero += 1 + while valor[-1] in OPERACIONES: + valor.remove(valor[-1]) - #for x in range(0, len(lista), 2): - # if not lista[x] in OPERACIONES: - # lista.insert(x + 1, operacion) - # break - # agregar_operacion(lista, operacion) + for x in valor: + if not x: + valor.remove(x) - print lista + return valor + def reducir_polinomios(self, operacion): + """ + Realizando la reducción de polinomios. + """ + operacion = self.limpiar_valor(operacion) + diccionario = {} + resultado = [] - lista = [] - - if '*' in operacion and not '/' in operacion: - lista = operacion.split('*') - agregar_operacion(lista, '*') - - elif not '*' in operacion and '/' in operacion: - lista = operacion.split('/') - agregar_operacion(lista, '/') - - elif '*' in operacion and '/' in operacion: - lista = operacion.split('*') - agregar_operacion(lista, '*') - - for x in lista: - if '/' in x: - numero = lista.index(x) - lista.remove(x) - for x2 in x.split('/'): - lista.insert(numero + x.split('/').index(x2), x2) - - agregar_operacion(lista, '/') - - semejantes = {} + if '+' in operacion: + operacion = operacion.replace('+', ' +') + if '-' in operacion: + operacion = operacion.replace('-', ' -') + + lista = operacion.split(' ') lista = self.limpiar_valor(lista) + resultado = 0 for x in lista: - if x and x[-1].lower() in LETRAS and not 'e' in x: - if not semejantes.get(1): - semejantes[1] = [] - - semejantes[1].append(x) - - elif x and 'x' in x and 'e' in x: - numero = -1 - l = [] - s = '' - - while True: - v = (x[numero]) - if v == 'e': - break - + if x and x[0] != '-' and x[0] != '+': + x = '+' + x + + if 'x' in x and not 'e' in x: + if not diccionario.get(1): + diccionario[1] = [] + + diccionario[1].append(x) + + elif 'x' in x and 'e' in x: + numero = x.split('e')[1] + if not diccionario.get(numero): + diccionario[numero] = [] + + diccionario[numero].append(x) + + elif not 'x' in x and 'e' in x: + x = int(x.split('e')[0]) ** int(x.split('e'))[1] + + if not diccionario.get(0): + diccionario[0] = [] + + diccionario[0].append(x) + + else: # not 'x' in x and not 'e' in x: + if not diccionario.get(0): + diccionario[0] = [] + + diccionario[0].append(x) + + for x in diccionario: + resultado = '' + + lista = diccionario[x] + + for valor in lista: + if 'x' in valor: + numero = valor.split('x')[0] + + if not 'x' in valor: + if 'e' in valor: + numero = x.split('e') + else: - l.append(v) + numero = x - numero -= 1 + print resultado, numero + if type(numero) == int and type(resultado) == str: + resultado += str(numero) - l.reverse() + resultado += numero - for x2 in l: - s += x2 + if resultado == '0x': + resultado = '0' - if not semejantes.get(int(s)): - semejantes[int(s)] = [] + print resultado - semejantes[int(s)].append(x) + if x == 0: + print eval(resultado) + elif x == 1: + print str(eval(resultado)) + 'x' + else: - if not semejantes.get(0): - semejantes[0] = [] - - semejantes[0].append(x) - - return semejantes, lista - - def limpiar_valor(self, valor): - - if type(valor) == str: - if ' ' in valor: - valor = valor.replace(' ', '') - - elif type(valor) == list and len(valor) >= 1: - while valor[0] in OPERACIONES: - valor.remove(valor[0]) - - while valor[-1] in OPERACIONES: - valor.remove(valor[-1]) - - return valor + print str(eval(resultado)) + 'xe' + str(x) + return diccionario if __name__ == '__main__': cerrar = False |