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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
#!/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.lower():
print self.reducir_polinomios(operacion)
else:
if '=' in operacion:
print self.resolver_ecuacion(operacion)
else:
print self.reducir_polinomios(operacion)
def operar(self, operacion):
resultado = 0
try:
resultado = eval(operacion)
except NameError:
raise NameError('Un término introducido no es un número.')
return resultado
def resolver_ecuacion(self, ecuacion):
miembros = ecuacion.split('=')
miembro1 = self.reducir_polinomios(miembros[0])[0]
miembro2 = self.reducir_polinomios(miembros[1])[0]
miembro1_str = ''
miembro2_str = ''
for x in miembro1:
for x2 in miembro1[x]:
miembro1_str += x2
for x in miembro2:
for x2 in miembro2[x]:
miembro2_str += x2
ecuacion = miembro1_str + '=' + miembro2_str
return ecuacion
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])
for x in valor:
if not x:
valor.remove(x)
return valor
def reducir_polinomios(self, operacion):
"""
Realizando la reducción de polinomios.
"""
operacion = self.limpiar_valor(operacion)
diccionario = {}
if '+' in operacion:
operacion = operacion.replace('+', ' +')
if '-' in operacion:
operacion = operacion.replace('-', ' -')
lista = operacion.split(' ')
lista = self.limpiar_valor(lista)
for x in lista:
if x[0] != '-' and x[0] != '+':
_numero = lista.index(x)
lista.remove(x)
lista.insert(_numero, '+' + 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 = str(int(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 = str(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(str(x))
for x in diccionario:
resultado = ''
lista = diccionario[x]
for valor in lista:
if 'x' in valor and valor != '+x' and not 'e' in valor:
numero = valor.split('x')[0]
elif valor == '+x':
lista.remove('+x')
lista.append('+1x')
numero = '+1'
elif valor == '-x':
lista.remove('-x')
lista.append('-1x')
numero = '-1'
if not 'x' in valor and 'e' in valor:
numero = str(int(valor.split('e')[0] ** valor.split('e')[1]))
if 'x' in valor and 'e' in valor:
numero = valor.split('xe')[0]
resultado += numero
if x == 0:
res = str(eval(resultado))
elif x == 1:
res = str(eval(resultado)) + 'x'
elif x > 1 or x < 0:
res = str(eval(resultado)) + 'xe' + str(x)
diccionario[x] = [res]
_lista = []
_resultado_final = ''
for x in diccionario:
_lista.append(x)
_lista.sort()
_lista.reverse()
for x in _lista:
lista = diccionario[x]
for valor in lista:
if valor[0] != '+' and valor[0] != '-':
valor = '+' + valor
_resultado_final += valor
if _resultado_final[0] == '+':
_resultado_final = _resultado_final[1:]
print diccionario
return _resultado_final
#return diccionario
if __name__ == '__main__':
cerrar = False
while not cerrar:
cuenta = raw_input('Operacion: ')
if cuenta.lower() in ['cerrar', 'salir']:
cerrar = True
else:
Calculadora(cuenta)
|