From 2cfad03da1d8201b96d6c4742aabfc12d563bea6 Mon Sep 17 00:00:00 2001 From: Cristian García Date: Wed, 28 Aug 2013 20:52:59 +0000 Subject: Agregando el archivo principal --- diff --git a/Calculadora.py b/Calculadora.py new file mode 100644 index 0000000..a232cd1 --- /dev/null +++ b/Calculadora.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python +# -*- coding: utf -*- + + +OPERACIONES = ['+', '-', '*', '/', '**', '//'] + +class Calculadora(): + + def __init__(self, operacion): + + if operacion: + if not 'x' in operacion: + self.operar(operacion) + + else: + if '=' in operacion: + self.resolver_ecuacion(operacion) + + else: + self.resolver_polinomio(operacion) + + def operar(self, operacion): + + resultado = 0 + + try: + resultado = eval(operacion) + + except TypeError: + raise TypeError('Un término introducido es un número') + + print resultado + + def resolver_ecuacion(self, operacion): + + pass + + def resolver_polinomio(self, operacion): + + 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) + + operacion = self.limpiar_valor(operacion) + + lista = [] + + if '*' 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) + + agregar_operacion(lista, '/') + + semejantes = [] + lista = self.limpiar_valor(lista) + + for x in lista: + if x and x[-1] == 'x': + lista1 = [x] + try: + if lista[lista.index(x)+1] != x: + lista1.append(lista[lista.index(x)+1]) + + except: + pass + + semejantes.append(lista1) + #for x in semejantes: + # self.limpiar_valor(x) + + print lista, semejantes + + 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 + + +if __name__ == '__main__': + cerrar = False + + while not cerrar: + cuenta = raw_input('Operacion: ') + if cuenta.lower() in ['cerrar', 'salir']: + cerrar = True + + else: + Calculadora(cuenta) -- cgit v0.9.1