Documente Academic
Documente Profesional
Documente Cultură
import math
operation = "N"
stored_value = 0
flag = 0
new_value = 0
results = []
def getStoredVal():
global stored_value
try:
if (stored_value == "pi"):
stored_value = math.pi()
elif (stored_value == "e"):
stored_value = math.e()
else:
float(stored_value)
return stored_value
except:
stored_value = 0
float(stored_value)
return stored_value
def reset(sum):
global operation
text4.insert('0.0', sum)
entry_left.delete(0, END)
results.append(sum)
operation = "N"
def clrBtn():
global operation
entry_left.delete(0, END)
operation = "N"
def btn(operation_name):
global stored_value
global flag
global operation
operation = operation_name
if flag == 0:
stored_value = entry_left.get()
entry_left.delete(0, END)
flag = 1
if flag == 1:
flag = 0
def calculate():
stored_value = getStoredVal()
global operation
global new_value
if operation in ("A", "S", "M", "D", "ID", "MO", "EX", "RO", "LOG", "ABS",
"FAC", "SIN", "COS", "TAN"):
text4.delete('0.0', END)
new_value = entry_left.get()
if operation == "A":
sum = float(stored_value) + float(new_value)
reset(sum)
elif operation == "S":
sum = float(stored_value) - float(new_value)
reset(sum)
elif operation == "M":
sum = float(stored_value) * float(new_value)
reset(sum)
elif operation == "D":
sum = divisionCheck(stored_value, new_value, operation)
reset(sum)
elif operation == "ID":
sum = divisionCheck(stored_value, new_value, operation)
reset(sum)
elif operation == "MO":
sum = divisionCheck(stored_value, new_value, operation)
reset(sum)
elif operation == "EX":
sum = float(stored_value) ** float(new_value)
reset(sum)
elif operation == "RO":
sum = float(new_value) ** (1 / float(stored_value))
# first input is the root type
reset(sum)
elif operation == "LOG":
sum = math.log(float(new_value),float(stored_value))
# first input is the base
reset(sum)
elif operation == "ABS":
sum = math.fabs(float(stored_value))
reset(sum)
elif operation == "FAC":
sum = math.factorial(float(stored_value))
reset(sum)
elif operation == "SIN":
sum = math.sin(math.radians(int(new_value)))
reset(sum)
elif operation == "COS":
sum = math.cos(math.radians(float(new_value)))
reset(sum)
elif operation == "TAN":
sum = math.tan(math.radians(float(new_value)))
reset(sum)
print("History: ", results)
else:
return
def addBtn():
btn("A")
def multBtn():
btn("M")
def subBtn():
btn("S")
def divBtn():
btn("D")
def intDivBtn():
btn("ID")
def modBtn():
btn("MO")
def expBtn():
btn("EX")
def rootBtn():
btn("RO")
def logBtn():
btn("LOG")
def absBtn():
btn("ABS")
def factBtn():
btn("FAC")
def sinBtn():
btn("SIN")
def cosBtn():
btn("COS")
def tanBtn():
btn("TAN")
window = Tk()
window.title('Calculator')
window.configure(background="linen")
window.mainloop()