From 834da825ff7ac41e8516d0c56a12fbeb295b51c3 Mon Sep 17 00:00:00 2001 From: sayampradhan <112542130+sayampradhan@users.noreply.github.com> Date: Sat, 15 Oct 2022 01:09:13 +0530 Subject: [PATCH] Update CALC.py --- CALCULATOR_PYTHON/CALC.py | 375 +++++++++++++++++++++++++++++--------- 1 file changed, 292 insertions(+), 83 deletions(-) diff --git a/CALCULATOR_PYTHON/CALC.py b/CALCULATOR_PYTHON/CALC.py index 7fdd053..643abdc 100644 --- a/CALCULATOR_PYTHON/CALC.py +++ b/CALCULATOR_PYTHON/CALC.py @@ -2,7 +2,7 @@ # coding: utf-8 # # CALCULATOR -# +# # SIMPLE CALCULATOR THAT CAN PERFORM FOLLOWING OPERATIONS # BASIC OPERATIONS # 1. ADDITION @@ -10,7 +10,7 @@ # 3. MULTIPLICATION # 4. DIVISION # 5. MODULUS -# +# # ADVANCED OPERATIONS # 1. NATURAL LOG # 2. TRIGNOMETRIC CALCULATIONS @@ -19,10 +19,10 @@ # In[ ]: - from tkinter import * import math as m -root =Tk() + +root = Tk() # GIVING TITLE TO THE APPLICATION AS simple calculator root.title("Simple Calculator") e = Entry(root, width=50, borderwidth=5, relief=RIDGE, fg="White", bg="Black") @@ -30,119 +30,332 @@ # Creating some userdefined functions for the operations involved in the application. def click(to_print): - old=e.get() - e.delete(0, END) - e.insert(0, old+to_print) - return + old = e.get() + e.delete(0, END) + e.insert(0, old + to_print) + return + def sc(event): - key=event.widget - text=key['text'] - no=e.get() - result='' - if text=='deg': - result=str(m.degrees(float(no))) - if text=='sin': - result=str(m.sin(float(no))) - if text=='cos': - result=str(m.cos(float(no))) - if text=='tan': - result=str(m.tan(float(no))) - if text=='lg': - result=str(m.log10(float(no))) - if text=='ln': - result=str(m.log(float(no))) - if text=='Sqrt': - result=str(m.sqrt(float(no))) - if text=='x!': - result=str(m.factorial(float(no))) - if text=='1/x': - result=str(1/(float(no))) - if text=='pi': - if no=="": - result=str(m.pi) - else: - result=str(float(no) * m.pi) - if text=='e': - if no=="": - result=str(m.e) - else: - result=str(m.e**float(no)) - - e.delete(0, END) - e.insert(0, result) + key = event.widget + text = key["text"] + no = e.get() + result = "" + if text == "deg": + result = str(m.degrees(float(no))) + if text == "sin": + result = str(m.sin(float(no))) + if text == "cos": + result = str(m.cos(float(no))) + if text == "tan": + result = str(m.tan(float(no))) + if text == "lg": + result = str(m.log10(float(no))) + if text == "ln": + result = str(m.log(float(no))) + if text == "Sqrt": + result = str(m.sqrt(float(no))) + if text == "x!": + result = str(m.factorial(float(no))) + if text == "1/x": + result = str(1 / (float(no))) + if text == "pi": + if no == "": + result = str(m.pi) + else: + result = str(float(no) * m.pi) + if text == "e": + if no == "": + result = str(m.e) + else: + result = str(m.e ** float(no)) + + e.delete(0, END) + e.insert(0, result) + # This function clears all the contents displayed on the computation window. def clear(): - e.delete(0, END) - return + e.delete(0, END) + return + # This function clears one character from rare end of the string. def bksps(): - current=e.get() - length=len(current)-1 - e.delete(length, END) + current = e.get() + length = len(current) - 1 + e.delete(length, END) + # This function is defined to evaluate the results and to prompt on the console. def evaluate(): - ans=e.get() - ans=eval(ans) - e.delete(0, END) - e.insert(0, ans) - + ans = e.get() + ans = eval(ans) + e.delete(0, END) + e.insert(0, ans) + + # Arrangement of buttons for better visualition and computation. lg = Button(root, text="log", padx=24, pady=10, relief=RAISED, bg="Black", fg="White") lg.bind("", sc) ln = Button(root, text="ln", padx=28, pady=10, relief=RAISED, bg="Black", fg="White") ln.bind("", sc) -par1st = Button(root, text="(", padx=29, pady=10, relief=RAISED, bg="Black", fg="White",command=lambda: click("(")) -par2nd = Button(root, text=")", padx=30, pady=10, relief=RAISED, bg="Black", fg="White",command=lambda: click(")")) -dot = Button(root, text=".", padx=29, pady=10, relief=RAISED, bg="Green", fg="Black",command=lambda: click(".")) - -exp = Button(root, text="^", padx=29, pady=10, relief=RAISED, bg="Black", fg="White", command=lambda: click("**")) +par1st = Button( + root, + text="(", + padx=29, + pady=10, + relief=RAISED, + bg="Black", + fg="White", + command=lambda: click("("), +) +par2nd = Button( + root, + text=")", + padx=30, + pady=10, + relief=RAISED, + bg="Black", + fg="White", + command=lambda: click(")"), +) +dot = Button( + root, + text=".", + padx=29, + pady=10, + relief=RAISED, + bg="Green", + fg="Black", + command=lambda: click("."), +) + +exp = Button( + root, + text="^", + padx=29, + pady=10, + relief=RAISED, + bg="Black", + fg="White", + command=lambda: click("**"), +) degb = Button(root, text="deg", padx=23, pady=10, relief=RAISED, bg="Black", fg="White") degb.bind("", sc) -sinb= Button(root, text="sin", padx=24, pady=10, relief=RAISED, bg="Black", fg="White",) +sinb = Button( + root, + text="sin", + padx=24, + pady=10, + relief=RAISED, + bg="Black", + fg="White", +) sinb.bind("", sc) -cosb= Button(root, text="cos", padx=23, pady=10, relief=RAISED, bg="Black", fg="White") +cosb = Button(root, text="cos", padx=23, pady=10, relief=RAISED, bg="Black", fg="White") cosb.bind("", sc) tanb = Button(root, text="tan", padx=23, pady=10, relief=RAISED, bg="Black", fg="White") tanb.bind("", sc) -sqrtm = Button(root, text="Sqrt", padx=23, pady=10, relief=RAISED, bg="Black", fg="White") +sqrtm = Button( + root, text="Sqrt", padx=23, pady=10, relief=RAISED, bg="Black", fg="White" +) sqrtm.bind("", sc) -ac = Button(root, text="C", padx=29, pady=10, relief=RAISED, bg="Dark Red", fg="White",command=lambda:clear()) -bksp = Button(root, text="DEL", padx=24, pady=10, relief=RAISED, bg="Dark Red", fg="White",command=lambda: bksps()) -mod = Button(root, text=" % ", padx=24, pady=10, relief=RAISED, bg="Black", fg="White",command=lambda: click("%")) -div = Button(root, text="/", padx=29, pady=10, relief=RAISED, bg="yellow", fg="Black",command=lambda: click("/")) +ac = Button( + root, + text="C", + padx=29, + pady=10, + relief=RAISED, + bg="Dark Red", + fg="White", + command=lambda: clear(), +) +bksp = Button( + root, + text="DEL", + padx=24, + pady=10, + relief=RAISED, + bg="Dark Red", + fg="White", + command=lambda: bksps(), +) +mod = Button( + root, + text=" % ", + padx=24, + pady=10, + relief=RAISED, + bg="Black", + fg="White", + command=lambda: click("%"), +) +div = Button( + root, + text="/", + padx=29, + pady=10, + relief=RAISED, + bg="yellow", + fg="Black", + command=lambda: click("/"), +) fact = Button(root, text="x!", padx=29, pady=10, relief=RAISED, bg="Black", fg="White") fact.bind("", sc) -seven = Button(root, text="7", padx=30, pady=10, relief=RAISED, bg="Grey", fg="White",command=lambda: click("7")) -eight = Button(root, text="8", padx=29, pady=10, relief=RAISED, bg="Grey", fg="White",command=lambda: click("8")) -nine = Button(root, text="9", padx=29, pady=10, relief=RAISED, bg="Grey", fg="White",command=lambda: click("9")) -mult = Button(root, text="X", padx=29, pady=10, relief=RAISED, bg="Yellow", fg="Black",command=lambda: click("*")) +seven = Button( + root, + text="7", + padx=30, + pady=10, + relief=RAISED, + bg="Grey", + fg="White", + command=lambda: click("7"), +) +eight = Button( + root, + text="8", + padx=29, + pady=10, + relief=RAISED, + bg="Grey", + fg="White", + command=lambda: click("8"), +) +nine = Button( + root, + text="9", + padx=29, + pady=10, + relief=RAISED, + bg="Grey", + fg="White", + command=lambda: click("9"), +) +mult = Button( + root, + text="X", + padx=29, + pady=10, + relief=RAISED, + bg="Yellow", + fg="Black", + command=lambda: click("*"), +) frac = Button(root, text="1/x", padx=25, pady=10, relief=RAISED, bg="Black", fg="White") frac.bind("", sc) -four = Button(root, text="4", padx=30, pady=10, relief=RAISED, bg="Grey", fg="White",command=lambda: click("4")) -five = Button(root, text="5", padx=29, pady=10, relief=RAISED, bg="Grey", fg="White",command=lambda: click("5")) -six = Button(root, text="6", padx=29, pady=10, relief=RAISED, bg="Grey", fg="White",command=lambda: click("6")) -minus = Button(root, text="-", padx=29, pady=10, relief=RAISED, bg="Yellow", fg="Black",command=lambda: click("-")) +four = Button( + root, + text="4", + padx=30, + pady=10, + relief=RAISED, + bg="Grey", + fg="White", + command=lambda: click("4"), +) +five = Button( + root, + text="5", + padx=29, + pady=10, + relief=RAISED, + bg="Grey", + fg="White", + command=lambda: click("5"), +) +six = Button( + root, + text="6", + padx=29, + pady=10, + relief=RAISED, + bg="Grey", + fg="White", + command=lambda: click("6"), +) +minus = Button( + root, + text="-", + padx=29, + pady=10, + relief=RAISED, + bg="Yellow", + fg="Black", + command=lambda: click("-"), +) pib = Button(root, text="pi", padx=28, pady=10, relief=RAISED, bg="Black", fg="White") pib.bind("", sc) -one = Button(root, text="1", padx=30, pady=10, relief=RAISED, bg="Grey", fg="White",command=lambda: click("1")) -two = Button(root, text="2", padx=29, pady=10, relief=RAISED, bg="Grey", fg="White",command=lambda: click("2")) -three = Button(root, text="3", padx=29, pady=10, relief=RAISED, bg="Grey", fg="White",command=lambda: click("3")) -plus = Button(root, text="+", padx=29, pady=10, relief=RAISED, bg="Yellow", fg="Black",command=lambda: click("+")) +one = Button( + root, + text="1", + padx=30, + pady=10, + relief=RAISED, + bg="Grey", + fg="White", + command=lambda: click("1"), +) +two = Button( + root, + text="2", + padx=29, + pady=10, + relief=RAISED, + bg="Grey", + fg="White", + command=lambda: click("2"), +) +three = Button( + root, + text="3", + padx=29, + pady=10, + relief=RAISED, + bg="Grey", + fg="White", + command=lambda: click("3"), +) +plus = Button( + root, + text="+", + padx=29, + pady=10, + relief=RAISED, + bg="Yellow", + fg="Black", + command=lambda: click("+"), +) e_b = Button(root, text="e", padx=29, pady=10, relief=RAISED, bg="Black", fg="White") e_b.bind("", sc) -zero = Button(root, text="0", padx=29, pady=10, relief=RAISED, bg="Grey", fg="White",command=lambda: click("0")) -equal = Button(root, text="=", padx=29, pady=10, relief=RAISED, bg="Dark Orange", fg="Black",command=lambda: evaluate()) +zero = Button( + root, + text="0", + padx=29, + pady=10, + relief=RAISED, + bg="Grey", + fg="White", + command=lambda: click("0"), +) +equal = Button( + root, + text="=", + padx=29, + pady=10, + relief=RAISED, + bg="Dark Orange", + fg="Black", + command=lambda: evaluate(), +) bksp.grid(row=1, column=0) @@ -189,7 +402,3 @@ def evaluate(): # In[ ]: - - - -