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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
#!/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.buscar_semejantes(operacion)
else:
self.buscar_semejantes(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 buscar_semejantes(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('*')
elif not '*' in operacion and '/' in operacion:
lista = operacion.split('/')
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)
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' 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
else:
l.append(v)
numero -= 1
for x2 in l[::-1]:
s += x2
if not semejantes.get(int(s)):
semejantes[int(s)] = []
semejantes[int(s)].append(x)
print 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)
|