From dca19de6ee7cd7cbdb003938ada346db00495b70 Mon Sep 17 00:00:00 2001 From: Cantekin Efe Date: Sun, 31 Oct 2021 11:01:26 -0400 Subject: [PATCH 01/10] first successful test --- Calculator 2.py | 36 +++++++++++++++++ Calculator_without_class.py | 53 +++++++++++++++++++++++++ TestCalculator.py | 34 ++++++++++++++++ main-app-dev.py | 77 +++++++++++++++++++++++++++++++++++++ main.py | 16 ++++++++ 5 files changed, 216 insertions(+) create mode 100644 Calculator 2.py create mode 100644 Calculator_without_class.py create mode 100644 TestCalculator.py create mode 100644 main-app-dev.py create mode 100644 main.py diff --git a/Calculator 2.py b/Calculator 2.py new file mode 100644 index 0000000..fadb018 --- /dev/null +++ b/Calculator 2.py @@ -0,0 +1,36 @@ +class Calculator: + + def __init__(self): + pass + + def add(self, x, y): + return x + y + + def sub(self, x, y): + return x - y + + def mult(self, x, y): + return x * y + + def div(self, x, y): + if y == 0: + print(Err) + return x / y + + def sq(self, x): + return x**2 + + def sqrt(self, x): + return x*x + + def varexp(self, x, y): + return x**y + + def inverve(self, x): + return 1 / x + + def invert_sign(self, x): + return x * -1 + + +# add lots more methods to this calculator class. diff --git a/Calculator_without_class.py b/Calculator_without_class.py new file mode 100644 index 0000000..5606d18 --- /dev/null +++ b/Calculator_without_class.py @@ -0,0 +1,53 @@ +import math + +def calc(term): + """ + The term will be the input from the calculator. + Output will be result of computed term. + This is where the functions will actually calculate. + """ + + term = term.replace(' ','') # gets rid of spaces + term = term.replace('^','**') # to power of sign recognized as ** + term = term.replace('=','') + term = term.replace('?','') + term = term.replace('%','/100') # for percentages + term = term.replace('rad','radians') + term = term.replace('mod','%') # for modula + + functions = ['sin', 'cos', 'tan', 'cosh', 'sinh', 'tanh', 'sqrt', 'pi', 'radians', 'e', 'square', 'exp','factorial'] + + term = term.lower() # convert to lowercase for case sensitive + + for function in functions: + if function in term: + usemath = 'math.' + function # will source the function and combine with math. to create math function + term = term.replace(function , usemath) + + try: + term = eval(term) + + except ZeroDivisionError: # builds in zero division error + print("Sorry it is not possible to divide by zero! Try again my friend.") + except NameError: + print("Sorry that input is not valid! Try again my friend.") + except AttributeError: + print("Incorrect usage method! Try again my friend.") + return(term) + +def result(term): # This passes the argument 'term' to the function 'calc' and prints the result + + print("\n" + str(calc(term))) + +def intro(): + """This is for handling the user data and printing the introduction and input + """ + while True: + print("\n BOO!! Welcome to the Halloween Scientific Calculator!! Input in any format, enter 'quit' to escape!!") + answer = input("\n What do you want to know? ") + + if answer == 'quit': + break + result(answer) + +intro() \ No newline at end of file diff --git a/TestCalculator.py b/TestCalculator.py new file mode 100644 index 0000000..7cae049 --- /dev/null +++ b/TestCalculator.py @@ -0,0 +1,34 @@ +import unittest +from Calculator import Calculator + + +class TestCalculator(unittest.TestCase): + """ Whenever this test case is executed, + setUp() method gets executed first. + In our case, we simply create an object + of the Calculator class and save it as a class attribute.""" + + def setUp(self): + self.calculator = Calculator() + + """ write test_xxx methods to test each method in the Calculator class. + This tells Python (via unittest framework) that these are test methods. + assertEqual in order to check if the Calculator methods returns + the expected value.""" + + def test_add(self): + self.assertEqual(self.calculator.add(4, 7), 11) + + def test_sub(self): + self.assertEqual(self.calculator.sub(10,5), 5) + + def test_mult(self): + self.assertEqual(self.calculator.mult(3,7), 21) + + def test_div(self): + self.assertEqual(self.calculator.div(10,2), 5) + +""" runs the test case TestCalculator. It execute each test method + defined inside the class and gives us the result.""" +if __name__ == "__main__": + unittest.main() diff --git a/main-app-dev.py b/main-app-dev.py new file mode 100644 index 0000000..993417a --- /dev/null +++ b/main-app-dev.py @@ -0,0 +1,77 @@ +from Calculator import Calculator + + +def getTwoNumbers(): + a = float(input("first number? ")) + b = float(input("second number? ")) + return a, b + + +def getOneNumber(): # get one number instead + a = float(input("first number? ")) + return a + + +def displayResult(x: float): + print(x, "\n") + + +def performCalcLoop(calc): + while True: + choice = input("Operation? ") + if choice == 'q': + print("Thanks for stopping by, have a great day.") + break # user types q to quit calulator. + + elif choice == 'add': + a, b = getTwoNumbers() + displayResult(calc.add(a, b)) + + elif choice == 'subtract': + a, b = getTwoNumbers() + displayResult(calc.sub(a, b)) + + elif choice == 'multiply': + a, b = getTwoNumbers() + displayResult(calc.mult(a, b)) + + elif choice == 'divide': + a, b = getTwoNumbers() + if b == 0: + print("Err") + else: + displayResult(calc.div(a, b)) + + elif choice == 'square': + a = getOneNumber() + displayResult(calc.sq(a)) + + elif choice == 'square root': + a = getOneNumber() + displayResult(calc.sqrt(a)) + + elif choice == 'variable exponentiation': + a, b = getTwoNumbers() + displayResult(calc.varexp(a, b)) + + elif choice == 'inverse': + a = getOneNumbers() + displayResult(calc.inverse(a)) + + elif choice == 'invert sign': + a = getOneNumber() + displayResult(calc.invert_sign(a)) + + else: + print("That is not a valid input.") + + +# main start +def main(): + calc = Calculator() + performCalcLoop(calc) + print("Done Calculating.") + + +if __name__ == '__main__': + main() diff --git a/main.py b/main.py new file mode 100644 index 0000000..94e3a87 --- /dev/null +++ b/main.py @@ -0,0 +1,16 @@ +# This is a sample Python script. + +# Press ⌃R to execute it or replace it with your code. +# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings. + + +def print_hi(name): + # Use a breakpoint in the code line below to debug your script. + print(f'Hi, {name}') # Press ⌘F8 to toggle the breakpoint. + + +# Press the green button in the gutter to run the script. +if __name__ == '__main__': + print_hi('PyCharm') + +# See PyCharm help at https://www.jetbrains.com/help/pycharm/ From bdb82c6cc1f09bcb487a62bc8ea985027ce57202 Mon Sep 17 00:00:00 2001 From: Cantekin Efe Date: Sun, 31 Oct 2021 20:54:57 -0400 Subject: [PATCH 02/10] added simple math --- Calculator 2.py | 36 ----------------- Calculator_without_class.py | 53 ------------------------- TestCalculator.py | 34 ---------------- calculator.py | 14 ++++++- main-app-dev.py | 77 ------------------------------------- main.py | 16 -------- 6 files changed, 12 insertions(+), 218 deletions(-) delete mode 100644 Calculator 2.py delete mode 100644 Calculator_without_class.py delete mode 100644 TestCalculator.py delete mode 100644 main-app-dev.py delete mode 100644 main.py diff --git a/Calculator 2.py b/Calculator 2.py deleted file mode 100644 index fadb018..0000000 --- a/Calculator 2.py +++ /dev/null @@ -1,36 +0,0 @@ -class Calculator: - - def __init__(self): - pass - - def add(self, x, y): - return x + y - - def sub(self, x, y): - return x - y - - def mult(self, x, y): - return x * y - - def div(self, x, y): - if y == 0: - print(Err) - return x / y - - def sq(self, x): - return x**2 - - def sqrt(self, x): - return x*x - - def varexp(self, x, y): - return x**y - - def inverve(self, x): - return 1 / x - - def invert_sign(self, x): - return x * -1 - - -# add lots more methods to this calculator class. diff --git a/Calculator_without_class.py b/Calculator_without_class.py deleted file mode 100644 index 5606d18..0000000 --- a/Calculator_without_class.py +++ /dev/null @@ -1,53 +0,0 @@ -import math - -def calc(term): - """ - The term will be the input from the calculator. - Output will be result of computed term. - This is where the functions will actually calculate. - """ - - term = term.replace(' ','') # gets rid of spaces - term = term.replace('^','**') # to power of sign recognized as ** - term = term.replace('=','') - term = term.replace('?','') - term = term.replace('%','/100') # for percentages - term = term.replace('rad','radians') - term = term.replace('mod','%') # for modula - - functions = ['sin', 'cos', 'tan', 'cosh', 'sinh', 'tanh', 'sqrt', 'pi', 'radians', 'e', 'square', 'exp','factorial'] - - term = term.lower() # convert to lowercase for case sensitive - - for function in functions: - if function in term: - usemath = 'math.' + function # will source the function and combine with math. to create math function - term = term.replace(function , usemath) - - try: - term = eval(term) - - except ZeroDivisionError: # builds in zero division error - print("Sorry it is not possible to divide by zero! Try again my friend.") - except NameError: - print("Sorry that input is not valid! Try again my friend.") - except AttributeError: - print("Incorrect usage method! Try again my friend.") - return(term) - -def result(term): # This passes the argument 'term' to the function 'calc' and prints the result - - print("\n" + str(calc(term))) - -def intro(): - """This is for handling the user data and printing the introduction and input - """ - while True: - print("\n BOO!! Welcome to the Halloween Scientific Calculator!! Input in any format, enter 'quit' to escape!!") - answer = input("\n What do you want to know? ") - - if answer == 'quit': - break - result(answer) - -intro() \ No newline at end of file diff --git a/TestCalculator.py b/TestCalculator.py deleted file mode 100644 index 7cae049..0000000 --- a/TestCalculator.py +++ /dev/null @@ -1,34 +0,0 @@ -import unittest -from Calculator import Calculator - - -class TestCalculator(unittest.TestCase): - """ Whenever this test case is executed, - setUp() method gets executed first. - In our case, we simply create an object - of the Calculator class and save it as a class attribute.""" - - def setUp(self): - self.calculator = Calculator() - - """ write test_xxx methods to test each method in the Calculator class. - This tells Python (via unittest framework) that these are test methods. - assertEqual in order to check if the Calculator methods returns - the expected value.""" - - def test_add(self): - self.assertEqual(self.calculator.add(4, 7), 11) - - def test_sub(self): - self.assertEqual(self.calculator.sub(10,5), 5) - - def test_mult(self): - self.assertEqual(self.calculator.mult(3,7), 21) - - def test_div(self): - self.assertEqual(self.calculator.div(10,2), 5) - -""" runs the test case TestCalculator. It execute each test method - defined inside the class and gives us the result.""" -if __name__ == "__main__": - unittest.main() diff --git a/calculator.py b/calculator.py index 3c85ead..af3ab6b 100644 --- a/calculator.py +++ b/calculator.py @@ -1,3 +1,6 @@ +import math + + class Calculator: def __init__(self): @@ -7,6 +10,13 @@ def add(self, x, y): return x + y def sub(self, x, y): - return 0 + return x - y + + def mult(self, x, y): + return x * y + + def div(self, x, y): + if y == 0: + print(Err) + return x / y -# add lots more methods to this calculator class. diff --git a/main-app-dev.py b/main-app-dev.py deleted file mode 100644 index 993417a..0000000 --- a/main-app-dev.py +++ /dev/null @@ -1,77 +0,0 @@ -from Calculator import Calculator - - -def getTwoNumbers(): - a = float(input("first number? ")) - b = float(input("second number? ")) - return a, b - - -def getOneNumber(): # get one number instead - a = float(input("first number? ")) - return a - - -def displayResult(x: float): - print(x, "\n") - - -def performCalcLoop(calc): - while True: - choice = input("Operation? ") - if choice == 'q': - print("Thanks for stopping by, have a great day.") - break # user types q to quit calulator. - - elif choice == 'add': - a, b = getTwoNumbers() - displayResult(calc.add(a, b)) - - elif choice == 'subtract': - a, b = getTwoNumbers() - displayResult(calc.sub(a, b)) - - elif choice == 'multiply': - a, b = getTwoNumbers() - displayResult(calc.mult(a, b)) - - elif choice == 'divide': - a, b = getTwoNumbers() - if b == 0: - print("Err") - else: - displayResult(calc.div(a, b)) - - elif choice == 'square': - a = getOneNumber() - displayResult(calc.sq(a)) - - elif choice == 'square root': - a = getOneNumber() - displayResult(calc.sqrt(a)) - - elif choice == 'variable exponentiation': - a, b = getTwoNumbers() - displayResult(calc.varexp(a, b)) - - elif choice == 'inverse': - a = getOneNumbers() - displayResult(calc.inverse(a)) - - elif choice == 'invert sign': - a = getOneNumber() - displayResult(calc.invert_sign(a)) - - else: - print("That is not a valid input.") - - -# main start -def main(): - calc = Calculator() - performCalcLoop(calc) - print("Done Calculating.") - - -if __name__ == '__main__': - main() diff --git a/main.py b/main.py deleted file mode 100644 index 94e3a87..0000000 --- a/main.py +++ /dev/null @@ -1,16 +0,0 @@ -# This is a sample Python script. - -# Press ⌃R to execute it or replace it with your code. -# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings. - - -def print_hi(name): - # Use a breakpoint in the code line below to debug your script. - print(f'Hi, {name}') # Press ⌘F8 to toggle the breakpoint. - - -# Press the green button in the gutter to run the script. -if __name__ == '__main__': - print_hi('PyCharm') - -# See PyCharm help at https://www.jetbrains.com/help/pycharm/ From 516937a21880972a8a5f883ffc2ca5615fd2689c Mon Sep 17 00:00:00 2001 From: Cantekin Efe Date: Sun, 31 Oct 2021 20:56:28 -0400 Subject: [PATCH 03/10] added math features --- calculator.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/calculator.py b/calculator.py index af3ab6b..67fa859 100644 --- a/calculator.py +++ b/calculator.py @@ -20,3 +20,18 @@ def div(self, x, y): print(Err) return x / y + def sq(self, x): + return x ** 2 + + def sqrt(self, x): + return x ** (1 / 2) + + def varexp(self, x, y): + return x ** y + + def inverse(self, x): + return 1 / x + + def invert_sign(self, x): + return x * -1 + From 8b030658a54aa4793ca2f24ecb0c056a646c4460 Mon Sep 17 00:00:00 2001 From: Cantekin Efe Date: Sun, 31 Oct 2021 20:58:58 -0400 Subject: [PATCH 04/10] added trigonometry functions --- calculator.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/calculator.py b/calculator.py index 67fa859..03b99d0 100644 --- a/calculator.py +++ b/calculator.py @@ -35,3 +35,70 @@ def inverse(self, x): def invert_sign(self, x): return x * -1 + def switchDisplayMode(self, string_mode, x): + """ + Switch display mode (binary, octal, decimal, hexadecimal) + switchDisplayMode(String mode) should set the display to the mode given + :param string_mode: + :return: + """ + # binary=0 + # counter=0 + # temp=x + if (string_mode == 'bin'): + # while (temp > 0): + # base = 2 + # binary = ((temp % base)) + # print(binary) + return str(bin(int(x)).replace("0b", "")) # STATE TO BE MODIFIED AS NEEDED + elif (string_mode == 'dec'): + return str(round(float(x), 2)) + elif (string_mode == 'oct'): + return str(oct(int(x)).replace("0o", "")) + elif (string_mode == 'hex'): + return str(hex(int(x)).replace("0x", "")) + else: + return str('invalid selection') + + def sin_deg(self, x): + x = math.radians(x) + return math.sin(x) + + def sin_rad(self, x): + return math.sin(x) + + def cos_deg(self, x): + return math.cos(math.radians(x)) + + def cos_rad(self, x): + return math.cos(x) + + def tan_deg(self, x): + return math.tan(math.radians(x)) + + def tan_rad(self, x): + return math.tan(x) + + def inv_sin_deg(self, x): + return math.sin(math.radians(x)) + + def inv_sin_rad(self, x): + return math.sin(x) + + def inv_cos_deg(self, x): + return math.cos(math.radians(x)) + + def inv_cos_rad(self, x): + return math.cos(x) + + def inv_tan_deg(self, x): + return math.tan(math.radians(x)) + + def inv_tan_rad(self, x): + return math.tan(x) + + def trig_units_mode_deg_to_rad(self, x): + return math.radians(x) + + def trig_units_mode_rad_to_deg(self, x): + return math.degrees(x) From 6d819257e914e8826284ce4472e8900a4fb01e28 Mon Sep 17 00:00:00 2001 From: Cantekin Efe Date: Sun, 31 Oct 2021 21:59:49 -0400 Subject: [PATCH 05/10] added unittest for simple math --- calctests.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/calctests.py b/calctests.py index 1964570..4425165 100644 --- a/calctests.py +++ b/calctests.py @@ -2,24 +2,25 @@ from calculator import Calculator -class TestStringMethods(unittest.TestCase): +class TestCalculator(unittest.TestCase): + """ When the test case is executed, + setUp() method gets executed first.""" - def test_add(self): - c = Calculator() - self.assertEqual(c.add(3, 3), 6) + def setUp(self): + self.calculator = Calculator() - def test_add2(self): - c = Calculator() - self.assertEqual(c.add(12, -10), 2) + """ test each method in the Calculator class. + assertEqual checks if the Calculator methods returns + the expected value.""" - def test_add3(self): - c = Calculator() - self.assertEqual(c.add(5, 8), 13) + def test_add(self): + self.assertEqual(self.calculator.add(4, 7), 11) def test_sub(self): - c = Calculator() - self.assertEqual(c.sub(9, 3), 6) + self.assertEqual(self.calculator.sub(10, 5), 5) + def test_mult(self): + self.assertEqual(self.calculator.mult(3, 7), 21) -if __name__ == '__main__': - unittest.main() + def test_div(self): + self.assertEqual(self.calculator.div(10, 2), 5) From e6302a3d7e016d946b50daf9776b9ce381f7f605 Mon Sep 17 00:00:00 2001 From: Cantekin Efe Date: Sun, 31 Oct 2021 23:07:27 -0400 Subject: [PATCH 06/10] added unittest for math features --- calctests.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/calctests.py b/calctests.py index 4425165..e489995 100644 --- a/calctests.py +++ b/calctests.py @@ -24,3 +24,18 @@ def test_mult(self): def test_div(self): self.assertEqual(self.calculator.div(10, 2), 5) + + def test_sq(self): + self.assertEqual(self.calculator.sq(2), 4) + + def test_sqrt(self): + self.assertEqual(self.calculator.sqrt(4), 2) + + def test_varexp(self): + self.assertEqual(self.calculator.varexp(10, 2), 100) + + def test_inverse(self): + self.assertEqual(self.calculator.inverse(10), 0.1) + + def test_invert_sign(self): + self.assertEqual(self.calculator.invert_sign(10), -10) From 85c350b8d31bf5d4867b328d11f291dc5ca9dbe7 Mon Sep 17 00:00:00 2001 From: Cantekin Efe Date: Sun, 31 Oct 2021 23:30:05 -0400 Subject: [PATCH 07/10] added choice option --- main-app.py | 78 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 26 deletions(-) diff --git a/main-app.py b/main-app.py index a7cc4e2..70baedf 100644 --- a/main-app.py +++ b/main-app.py @@ -6,29 +6,55 @@ def getTwoNumbers(): b = float(input("second number? ")) return a, b - -def displayResult(x: float): - print(x, "\n") - - -def performCalcLoop(calc): - while True: - choice = input("Operation? ") - if choice == 'q': - break # user types q to quit calulator. - elif choice == 'add': - a, b = getTwoNumbers() - displayResult(calc.add(a, b)) - else: - print("That is not a valid input.") - - -# main start -def main(): - calc = Calculator() - performCalcLoop(calc) - print("Done Calculating.") - - -if __name__ == '__main__': - main() +def getOneNumber(): + a = float(input("Number? ")) + return a + +def switchDisplayModeInput(): + """ + Switch display mode (binary, octal, decimal, hexadecimal) + switchDisplayMode() should rotate through the options + :return: + """ + return str(input('Select a display mode - bin, oct, dec, hex: ')) + +def trig_units_mode_input(): + """ + Switch trig units mode (Degrees, Radians) + switchUnitsMode() should rotate through the options + :return: + """ + return input('Select deg or rad: ') + +def displayResult(temp_display: float): + print("\nDISPLAY:\n") + print(temp_display) + + +def performCalcLoop(calc, temp_display): # KB - removed none assignment to temp_display + print('\nBOO! Happy Halloween ;) \n\n "Welcome to your Scientific Calculator."') + print("\nHere's a list of choices:") + print('~' * 70) + print("1 : Addition \t\t 17 : Inverse sine") + print("2 : Subtraction \t 18 : Inverse cosine") + print("3 : Multiplication\t 19 : Inverse tangent") + print("4 : Division \t\t 20 : Trig units mode - Convert Radians to Degrees") + print("5 : Square \t 21 : Trig units mode - Convert Degrees to Radians") + print("6 : Square Root\t 22 : Factorial") + print("7 : Variable Exponentiation \t 23 : Log x") + print("8 : Inverse of Display \t 24 : 10 power x - Inverse nog") + print("9 : Invert Sign (+/-)\t 25 : Ln x - Natural log") + print("10 : Switch Display\t 26 : e power x - Inverse natural log") + print("11 : M+ \t 27 : Pi") + print("12 : MC \t 28 : Exponentiation constant") + print("13 : MRC \t 29: Clear") + print("14 : Sine\t 30: Quit") + print("15 : Cosine") + print("16 : Tangent") + print('~' * 70) + + temp_display = 0 + print("DISPLAY:") + print(temp_display) + + memory = 0.0 From 2c7c6da42919f03ab117b8ae9db350a82500c4ad Mon Sep 17 00:00:00 2001 From: Cantekin Efe Date: Sun, 31 Oct 2021 23:44:49 -0400 Subject: [PATCH 08/10] added unittest for trigonometr --- calctests.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/calctests.py b/calctests.py index e489995..5253f8f 100644 --- a/calctests.py +++ b/calctests.py @@ -39,3 +39,45 @@ def test_inverse(self): def test_invert_sign(self): self.assertEqual(self.calculator.invert_sign(10), -10) + + def test_sin_deg(self): + self.assertEqual(self.calculator.sin_deg(45), 0.7071067811865475) + + def test_sin_rad(self): + self.assertEqual(self.calculator.sin_rad(45), 0.8509035245341184) + + def test_cos_deg(self): + self.assertEqual(self.calculator.cos_deg(45), 0.7071067811865476) + + def test_cos_rad(self): + self.assertEqual(self.calculator.cos_rad(45), 0.5253219888177297) + + def test_tan_deg(self): + self.assertEqual(self.calculator.tan_deg(45), 0.9999999999999999) + + def test_tan_rad(self): + self.assertEqual(self.calculator.tan_rad(45), 1.6197751905438615) + + def test_inv_sin_deg(self): + self.assertEqual(self.calculator.inv_sin_deg(45), 0.7071067811865475) + + def test_inv_sin_rad(self): + self.assertEqual(self.calculator.inv_sin_rad(45), 0.8509035245341184) + + def test_inv_cos_deg(self): + self.assertEqual(self.calculator.inv_cos_deg(45), 0.7071067811865476) + + def test_inv_cos_rad(self): + self.assertEqual(self.calculator.inv_cos_rad(45), 0.5253219888177297) + + def test_inv_tan_deg(self): + self.assertEqual(self.calculator.inv_tan_deg(45), 0.9999999999999999) + + def test_inv_tan_rad(self): + self.assertEqual(self.calculator.inv_tan_rad(45), 1.6197751905438615) + + def test_trig_units_mode_deg_to_rad(self): + self.assertEqual(self.calculator.trig_units_mode_deg_to_rad(45), 0.7853981633974483) + + def test_trig_units_mode_rad_to_deg(self): + self.assertEqual(self.calculator.trig_units_mode_rad_to_deg(45), 2578.3100780887044) From a94815941fe838c61474d9aa57544eec1d323f9e Mon Sep 17 00:00:00 2001 From: Cantekin Efe Date: Mon, 1 Nov 2021 00:02:30 -0400 Subject: [PATCH 09/10] added bonus features in the core code --- calculator.py | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/calculator.py b/calculator.py index 03b99d0..7d34b8c 100644 --- a/calculator.py +++ b/calculator.py @@ -42,15 +42,10 @@ def switchDisplayMode(self, string_mode, x): :param string_mode: :return: """ - # binary=0 - # counter=0 - # temp=x + if (string_mode == 'bin'): - # while (temp > 0): - # base = 2 - # binary = ((temp % base)) - # print(binary) - return str(bin(int(x)).replace("0b", "")) # STATE TO BE MODIFIED AS NEEDED + + return str(bin(int(x)).replace("0b", "")) elif (string_mode == 'dec'): return str(round(float(x), 2)) elif (string_mode == 'oct'): @@ -102,3 +97,29 @@ def trig_units_mode_deg_to_rad(self, x): def trig_units_mode_rad_to_deg(self, x): return math.degrees(x) + + def factorial(self,x ): + return math.factorial(x) + + def log(self, x, base): + return math.log(x,base) + + def inverse_log(self,x): + return 10**x + + def inv_ln(self,x): + return math.exp(x) + + def pi(self): + return math.pi + + def e(self): + return math.e + + def madd(self, x, memory): + memory = x + memory + return memory + + def mclear(self): + memory=0 + return memory From 841fa0bdefdf63ab066546794f973945d95b4ece Mon Sep 17 00:00:00 2001 From: Cantekin Efe Date: Mon, 1 Nov 2021 00:40:58 -0400 Subject: [PATCH 10/10] finalized the scientific and bonus features --- calctests.py | 24 ++++++ main-app.py | 223 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 245 insertions(+), 2 deletions(-) diff --git a/calctests.py b/calctests.py index 5253f8f..ad01ead 100644 --- a/calctests.py +++ b/calctests.py @@ -81,3 +81,27 @@ def test_trig_units_mode_deg_to_rad(self): def test_trig_units_mode_rad_to_deg(self): self.assertEqual(self.calculator.trig_units_mode_rad_to_deg(45), 2578.3100780887044) + + def test_factorial(self): + self.assertEqual(self.calculator.factorial(5), 120) + + def test_log(self): + self.assertEqual(self.calculator.log(45, 10), 1.6532125137753435) + + def test_inverse_log(self): + self.assertEqual(self.calculator.inverse_log(3), 1000) + + def test_inv_ln(self): + self.assertEqual(self.calculator.inv_ln(3), 20.085536923187668) + + def test_pi(self): + self.assertEqual(self.calculator.pi(), 3.141592653589793) + + def test_e(self): + self.assertEqual(self.calculator.e(), 2.718281828459045) + + +""" runs the test case TestCalculator. executes each test method + defined in the class and gives us the result.""" +if __name__ == "__main__": + unittest.main() diff --git a/main-app.py b/main-app.py index 70baedf..78921f9 100644 --- a/main-app.py +++ b/main-app.py @@ -44,11 +44,11 @@ def performCalcLoop(calc, temp_display): # KB - removed none assignment to temp_ print("7 : Variable Exponentiation \t 23 : Log x") print("8 : Inverse of Display \t 24 : 10 power x - Inverse nog") print("9 : Invert Sign (+/-)\t 25 : Ln x - Natural log") - print("10 : Switch Display\t 26 : e power x - Inverse natural log") + print("10 : Switch Display\t 26 : e power x - Inverse natural log") print("11 : M+ \t 27 : Pi") print("12 : MC \t 28 : Exponentiation constant") print("13 : MRC \t 29: Clear") - print("14 : Sine\t 30: Quit") + print("14 : Sine\t 30: Quit") print("15 : Cosine") print("16 : Tangent") print('~' * 70) @@ -58,3 +58,222 @@ def performCalcLoop(calc, temp_display): # KB - removed none assignment to temp_ print(temp_display) memory = 0.0 + + while True: + + try: + choice = input("Enter number to choose your operation:\n") + + except: + print("Please enter a valid number.") + + if choice == '30': + break + + elif choice == '1': + a, b = getTwoNumbers() + temp_display = calc.add(a, b) + displayResult(temp_display) + + elif choice == '2': + a, b = getTwoNumbers() + temp_display = calc.sub(a, b) + displayResult(temp_display) + + elif choice == '3': + a, b = getTwoNumbers() + temp_display = calc.mult(a, b) + displayResult(temp_display) + + elif choice == '4': + a, b = getTwoNumbers() + if b == 0: + temp_display = "Err" + displayResult(temp_display) + else: + temp_display = calc.div(a, b) + displayResult(temp_display) + + elif choice == '5': + a = getOneNumber() + temp_display = calc.sq(a) + displayResult(temp_display) + + elif choice == '6': + a = getOneNumber() + temp_display = calc.sqrt(a) + displayResult(temp_display) + + elif choice == '7': + a, b = getTwoNumbers() + temp_display = calc.varexp(a, b) + displayResult(temp_display) + + elif choice == '8': + a = getOneNumber() + temp_display = calc.inverse(a) + displayResult(temp_display) + + elif choice == '9': + a = getOneNumber() + temp_display = calc.invert_sign(a) + displayResult(temp_display) + + + + elif choice == '10': + + a = switchDisplayModeInput() + displayResult(calc.switchDisplayMode(a, temp_display)) + + elif choice == '11': + displayResult(calc.madd(temp_display, memory)) + memory = calc.madd(temp_display, memory) + + + elif choice == '12': + displayResult(calc.mclear()) + memory = calc.mclear() + + + elif choice == '13': + displayResult(memory) + + + elif choice == '14': + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.sin_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.sin_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + + elif choice == '15': + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.cos_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.cos_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + + elif choice == '16': + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.tan_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.tan_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + elif choice == '17': + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.inv_sin_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.inv_sin_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + + elif choice == '18': + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.inv_cos_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.inv_cos_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + + elif choice == '19': + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.inv_tan_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.inv_tan_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + elif choice == '20': + x = getOneNumber() + temp_display = calc.trig_units_mode_deg_to_rad(x) + print(displayResult(temp_display), " radians") + + elif choice == '21': + x = getOneNumber() + temp_display = calc.trig_units_mode_rad_to_deg(x) + print(displayResult(temp_display), " degrees") + + elif choice == '22': + x = getOneNumber() + temp_display = calc.factorial(x) + print(displayResult(temp_display)) + + elif choice == '23': + print("Enter your number and base as prompted below\n") + x, b = getTwoNumbers() + temp_display = calc.log(x, b) + print(displayResult(temp_display)) + + elif choice == '24': + x = getOneNumber() + temp_display = calc.inverse_log(x) + print(displayResult(temp_display)) + + elif choice == '25': + x = getOneNumber() + temp_display = calc.ln(x) + print(displayResult(temp_display)) + + elif choice == '26': + x = getOneNumber() + temp_display = calc.inv_ln(x) + print(displayResult(temp_display)) + + elif choice == '27': + temp_display = calc.pi() + displayResult(temp_display) + + elif choice == '28': + temp_display = calc.e() + displayResult(temp_display) + + elif choice == '29': + temp_display = 0 + displayResult(temp_display) + + else: + print("That is not a valid input.") + displayResult(temp_display) + + +def main(): + calc = Calculator() + performCalcLoop(calc, 0) + print("Done Calculating.") + + +if __name__ == '__main__': + main()