Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Calculadora.py
blob: a232cd1bde2549de1c82d3bff1cd5ebf3cf83730 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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)