Código de Python - Calculadora (notación infija)

Imágen de perfil
Val: 712
Bronce
Ha aumentado 1 puesto en Python (en relación al último mes)
Gráfica de Python

Calculadora (notación infija)gráfica de visualizaciones


Python

Actualizado el 31 de Julio del 2020 por Antonio (75 códigos) (Publicado el 29 de Febrero del 2020)
5.571 visualizaciones desde el 29 de Febrero del 2020
Calculadora con interfaz gráfica que emplea notación infija (el operador se introduce entre los operandos) en donde el resultado en pantalla va actualizándose a medida que se introducen las operaciones. Aunque, esta versión no incorpora operaciones entre paréntesis, se puede almacenar el resultado de una operación hecha, en la memoria, mediante el botón "MEM",(almacenándose el valor que esté en pantalla) dicho valor, podrá borrarse de la memoria, con el botón "DEL".
infix_calc

Requerimientos

Lenguaje: Python
Librerías: Tkinter, math

2.0

Actualizado el 31 de Julio del 2020 (Publicado el 29 de Febrero del 2020)gráfica de visualizaciones de la versión: 2.0
5.572 visualizaciones desde el 29 de Febrero del 2020
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tkinter import *
from math import *
 
ventana=Tk()
ventana.title("INFIX-CALC3")
ventana.configure(background="gray36")
ventana.geometry("366x450")
numeroPantalla=StringVar()
memoria=""
 
def numeroPulsado(n):
    global numero
    global exc
    if exc==True:
        clear()
        exc=False
    numero=numero+n
    numeroPantalla.set(numero)
 
def onediv():
    global numero
    global resultado
    global exc
    try:
        if abs(float(numeroPantalla.get()))==abs(float(numero)):
            numero=1/(float(numero))
            numeroPantalla.set(numero)
        else:
            resultado=1/float(resultado)
            numeroPantalla.set(resultado)
    except:
        clear()
        numeroPantalla.set("ERROR")
    exc=True
 
def delete():
    global memoria
    if memoria!="":
        if "M" in numeroPantalla.get():
            n=numeroPantalla.get()
            n=n.replace("M","D")
            numeroPantalla.set(n)
        t[0].config(bg="cornflower blue",fg="white")
        memoria=""
 
def memo():
    global resultado
    global memoria
    global numero
    if exc==True and numeroPantalla.get()!="ERROR":
        if memoria=="":
            memoria=float(numeroPantalla.get())
            resultado=0
            numeroPantalla.set(str(memoria)+"(M)")
            t[0].config(bg="white",fg="cornflower blue")
        else:
            numero=memoria
            numeroPantalla.set(numero)
    else:
        if memoria!="":
            numero=memoria
            numeroPantalla.set(memoria)
 
 
def opera_calculo(operador):
    global resultado
    if operador=="+":
        resultado=resultado+float(numero)
    elif operador=="-":
        resultado=resultado-float(numero)
    elif operador=="*":
        resultado=resultado*float(numero)
    elif operador=="/":
        resultado=resultado/float(numero)
    elif operador=="**":
        resultado=resultado**float(numero)
    elif operador=="%":
        resultado=resultado%float(numero)
    elif operador=="log":
        resultado=log(resultado)/log(float(numero))
 
def loga():
    global numero
    global resultado
    global exc
    if primr==True:
        if numero=="":
            numero=0
    try:
        if numero!="":
            if abs(float(numeroPantalla.get()))==abs(float(numero)):
                numero=log(float(numero))
                numeroPantalla.set(numero)
            else:
                resultado=log(float(resultado))
                numeroPantalla.set(resultado)
    except:
        clear()
        numeroPantalla.set("ERROR")
 
 
def rounde():
    global numero
    global resultado
    try:
        if abs(float(numeroPantalla.get()))==abs(float(numero)):
            numero=round(float(numero))
            numeroPantalla.set(numero)
        else:
            resultado=round(float(resultado))
            numeroPantalla.set(resultado)
    except:
        clear()
        numeroPantalla.set("ERROR")
 
 
def funcis(f):
    global numero
    global resultado
    global exc
    global prev_func
    if primr==True:
        if numero=="":
            numero=0
    if numero!="" and numeroPantalla.get()!="ERROR":
        li=["sin","cos","tan"]
        if exc==False or prev_func in li:
            if abs(float(numeroPantalla.get()))==abs(float(numero)):
                numero=eval(f+"("+str(numero)+")")
                numeroPantalla.set(numero)
            else:
                resultado=eval(f+"("+str(resultado)+")")
                numeroPantalla.set(resultado)
        prev_func=f
        exc=True
 
def comas():
    global numero
    if numero!="" and not "." in str(numero) and exc==False:
        numero=numero+"."
        numeroPantalla.set(numero)
 
def clear_error():
    global numero
    if numero != "" and exc == False:
        lista = list(numero)
        lista.pop()
        numero = ("").join(lista)
        if numero == "":
            numero = "0"
        numeroPantalla.set(numero)
 
def cambio_signo():
    global numero
    global resultado
    try:
        if numero!="" and abs(float(numeroPantalla.get()))==abs(float(numero)):
            if float(numero)!=0:
                numero=float(numero)*(-1)
                numeroPantalla.set(numero)
        else:
            if resultado!=0:
                resultado=resultado*(-1)
                numeroPantalla.set(resultado)
    except:
        clear()
        numeroPantalla.set("ERROR")
 
def raiz_cuadrada():
    global numero
    global resultado
    global exc
    try:
        if numero!="" and abs(float(numeroPantalla.get()))==abs(float(numero)):############
            numero=sqrt(float(numero))
            numeroPantalla.set(numero)
        else:
            resultado=sqrt(resultado)
            numeroPantalla.set(resultado)
    except:
        clear()#N
        numeroPantalla.set("ERROR")
    exc=True
 
def pee():
    global numero
    global exc
    if exc==True:
        clear()
    numero=str(pi)
    numeroPantalla.set(numero)
 
def calculo(o):
    global resultado
    global numero
    global primr
    global prev_sign
    global operacion
    global exc
    if primr==True:
        if numero=="":
            numero=0
        resultado=float(numero)
        prev_sign=o
        numero=""
        primr=False
    else:
        try:
            if numero!="" and exc==False:
                opera_calculo(prev_sign)
            prev_sign=o
            numeroPantalla.set(resultado)
        except:
            clear()
            numeroPantalla.set("ERROR")
            resultado=0
            primr=True
 
        numero=""
    exc=False
 
def clear():
    global numero
    global resultado
    global primr
    global prev_sign
    global operacion
    global exc
    global prev_func
    numero=""
    resultado=0
    primr=True
    prev_sign=""
    operacion=""
    exc=False
    prev_func="sin"
    numeroPantalla.set(resultado)
 
def result():
    global numero
    global resultado
    global prev_sign
    global operacion
    global primr
    global exc
    if primr==True:
        try:
            resultado=float(numeroPantalla.get())
            primr=False
        except:
            numeroPantalla.set(numeroPantalla.get())
    try:
        operacion=prev_sign
        if numero=="":
            numero=resultado
        opera_calculo(operacion)
        numeroPantalla.set(resultado)
    except:
        numeroPantalla.set("ERROR")
        primr=True
        numero=0
        resultado=0
        prev_sign=""
        operacion=""
    exc=True
 
t=[]
Entry(ventana,font=('Arial',23,'bold'),textvariable=numeroPantalla,width=21,bd=2,bg="PaleGreen3",justify="right").place(x=1,y=30)
 
Button(ventana,text="7",width=7,fg="white",bg="gray13",height=2,command=lambda:numeroPulsado("7")).place(x=4,y=180)
Button(ventana,text="8",width=7,fg="white",bg="gray13",height=2,command=lambda:numeroPulsado("8")).place(x=78,y=180)
Button(ventana,text="9",width=7,fg="white",bg="gray13",height=2,command=lambda:numeroPulsado("9")).place(x=152,y=180)
Button(ventana,text="CE",width=7,bg="DarkOrange2",height=2,command=clear_error).place(x=227,y=180)
Button(ventana,text="C",width=7,bg="DarkOrange2",height=2,command=clear).place(x=302,y=180)
Button(ventana,text="4",width=7,fg="white",bg="gray13",height=2,command=lambda:numeroPulsado("4")).place(x=4,y=238)
Button(ventana,text="5",width=7,fg="white",bg="gray13",height=2,command=lambda:numeroPulsado("5")).place(x=78,y=238)
Button(ventana,text="6",width=7,fg="white",bg="gray13",height=2,command=lambda:numeroPulsado("6")).place(x=152,y=238)
Button(ventana,text="x",width=7,fg="white",bg="gray13",height=2,command=lambda:calculo("*")).place(x=227,y=238)
Button(ventana,text="√",width=7,fg="white",bg="gray13",height=2,command=raiz_cuadrada).place(x=302,y=238)
Button(ventana,text="1",width=7,fg="white",bg="gray13",height=2,command=lambda:numeroPulsado("1")).place(x=4,y=296)
Button(ventana,text="2",width=7,fg="white",bg="gray13",height=2,command=lambda:numeroPulsado("2")).place(x=78,y=296)
Button(ventana,text="3",width=7,fg="white",bg="gray13",height=2,command=lambda:numeroPulsado("3")).place(x=152,y=296)
Button(ventana,text="+",width=7,fg="white",bg="gray13",height=2,command=lambda:calculo("+")).place(x=227,y=296)
Button(ventana,text="-",width=7,fg="white",bg="gray13",height=2,command=lambda:calculo("-")).place(x=302,y=296)
Button(ventana,text="0",width=7,fg="white",bg="gray13",height=2,command=lambda:numeroPulsado("0")).place(x=4,y=354)
Button(ventana,text="/",width=7,fg="white",bg="gray13",height=2,command=lambda:calculo("/")).place(x=78,y=354)
Button(ventana,text=".",width=7,fg="white",bg="gray13",height=2,command=comas).place(x=152,y=354)
Button(ventana,text="EXP",width=7,fg="white",bg="gray13",height=2,command=lambda:calculo("**")).place(x=227,y=354)
Button(ventana,text="=",width=7,fg="white",bg="gray13",height=2,command=result).place(x=302,y=354)
 
Button(ventana,text="+/-",width=6,fg="white",bg="gray6",height=1,command=cambio_signo).place(x=4,y=100)
Button(ventana,text="sin",width=6,fg="white",bg="gray6",height=1,command=lambda:funcis("sin")).place(x=65,y=100)
Button(ventana,text="cos",width=6,fg="white",bg="gray6",height=1,command=lambda:funcis("cos")).place(x=126,y=100)
Button(ventana,text="tan",width=6,fg="white",bg="gray6",height=1,command=lambda:funcis("tan")).place(x=187,y=100)
Button(ventana,text="%",width=6,fg="white",bg="gray6",height=1,command=lambda:calculo("%")).place(x=248,y=100)
Button(ventana,text="1/x",width=6,fg="white",bg="gray6",height=1,command=onediv).place(x=309,y=100)
Button(ventana,text="DEL",width=6,fg="white",bg="cornflower blue",height=1,command=delete).place(x=65,y=136)
Button(ventana,text="R",width=6,fg="white",bg="gray6",height=1,command=rounde).place(x=126,y=136)
Button(ventana,text="π",width=6,fg="white",bg="gray6",height=1,command=pee).place(x=187,y=136)
Button(ventana,text="log",width=6,fg="white",bg="gray6",height=1,command=lambda:calculo("log")).place(x=248,y=136)
Button(ventana,text="ln",width=6,fg="white",bg="gray6",height=1,command=loga).place(x=309,y=136)
bton_memoria=Button(ventana,text="MEM",width=6,fg="white",bg="cornflower blue",height=1,command=memo)
t.append(bton_memoria)
bton_memoria.place(x=4,y=136)
clear()
 
ventana.mainloop()



Comentarios sobre la versión: 2.0 (0)


No hay comentarios
 

Comentar la versión: 2.0

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s5983