From 249af5220f81e410bbc06ab8f0c0eb0f3b458f45 Mon Sep 17 00:00:00 2001 From: Cristian García Date: Thu, 29 Aug 2013 23:40:40 +0000 Subject: Actualizando los avances del dia --- diff --git a/Calculadora.py b/Calculadora.py index d1d2003..bb2faf8 100644 --- a/Calculadora.py +++ b/Calculadora.py @@ -1,16 +1,62 @@ #!/usr/bin/env python # -*- coding: utf -*- - OPERACIONES = ['+', '-', '*', '/', '**', '//'] +LETRAS = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', + 'ñ', 'o', 'p', 'q', 'r', 's', 't', 'u', 'w', 'x', 'y', 'z'] + class Calculadora(): + """ + Pasos a seguir al resolver ecuaciones: + + 1) Reducir los polinomios en los dos miembros + 2) Dejar la x en un miembro y sola + 3) Expresar el conjunto solución, S = {SOLUCIONES} + + 3x + 3 - 2 = 13 + 3x + 1 = 13 <--- 1 + 3x - 1 + 1 = 13 - 1 <--- 2 + 3x / 3 = 12 / 3 <--- 2 + x = 4 <--- 2 + S = {4} <--- 3 + + + Pasos a seguir al reducir pilinomios: + + 1) Buscar los monomios con el mismo grado, dependiendo del exponente, + en caso de no tener exponene depende de si el monomio tiene o no una x, + en caso de no tener, el grado es 0, de lo contrario es 1, y si tiene + exponente, el exponente es el grado. + 2) Realizar las operaciones entre los monomios + semejanes de la siguiente manera: + + Multiplicación/División: + Se multiplican o dividen los coeficientes y + partes literales por separado: + + Resolver 3x * 5x: + 3 * 5 = 15 + x * x = xe2 + + Entonces 3x * 5x = 15xe2 + + Suma/Resta: + Se suman o restan los coeficientes(sabiendo que si la x está sola, + cuenta como coeficiente 1): + + 3x + 2x = 5x + x + 5x = 6x + 7x - 4x = 3x + + """ + def __init__(self, operacion): if operacion: - if not 'x' in operacion: - self.operar(operacion) + if not 'x' in operacion.lower(): + print self.operar(operacion) else: if '=' in operacion: @@ -26,10 +72,10 @@ class Calculadora(): try: resultado = eval(operacion) - except TypeError: - raise TypeError('Un término introducido es un número') + except NameError: + raise NameError('Un término introducido no es un número.') - print resultado + return resultado def resolver_ecuacion(self, operacion): @@ -39,58 +85,62 @@ class Calculadora(): def agregar_operacion(lista, operacion): - for x in range(0, len(lista)*2, 2): - if not lista[range(0, len(lista)*2, 2).index(x)] in OPERACIONES: - lista.insert(range(0, len(lista)*2, 2).index(x), operacion) + numero = 0 + for x in lista: + if_int = str + + try: + int(x) + if_int = int + + except: + if_int = str + + if ('x' in x or if_int == int) and not x in OPERACIONES: + lista.insert(numero, operacion) + print lista + + numero += 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) + + print lista operacion = self.limpiar_valor(operacion) lista = [] - if '*' in operacion: + 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 not '/' in operacion and '+' in operacion: - lista = operacion.split('+') - - elif not '+' in operacion and '-' in operacion: - lista = operacion.split('-') - - for x in range(0, len(lista)*2, 2): - lista.insert(x, '*') - - 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) - - 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) + 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) + 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, '/') + agregar_operacion(lista, '/') semejantes = {} lista = self.limpiar_valor(lista) for x in lista: - if x and x[-1] == 'x' and not 'e' in x: + if x and x[-1].lower() in LETRAS and not 'e' in x: if not semejantes.get(1): semejantes[1] = [] @@ -100,6 +150,7 @@ class Calculadora(): numero = -1 l = [] s = '' + while True: v = (x[numero]) if v == 'e': @@ -110,15 +161,23 @@ class Calculadora(): numero -= 1 - for x2 in l[::-1]: + l.reverse() + + for x2 in l: s += x2 if not semejantes.get(int(s)): semejantes[int(s)] = [] - + semejantes[int(s)].append(x) - print semejantes + else: + if not semejantes.get(0): + semejantes[0] = [] + + semejantes[0].append(x) + + return semejantes, lista def limpiar_valor(self, valor): @@ -142,7 +201,7 @@ if __name__ == '__main__': while not cerrar: cuenta = raw_input('Operacion: ') if cuenta.lower() in ['cerrar', 'salir']: - cerrar = True + cerrar = True else: Calculadora(cuenta) -- cgit v0.9.1