diff --git a/calctests.py b/calctests.py index 1964570..19affac 100644 --- a/calctests.py +++ b/calctests.py @@ -1,25 +1,112 @@ +#From Efe + import unittest 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) + + 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) + + 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) + + 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) + -if __name__ == '__main__': +""" runs the test case TestCalculator. executes each test method + defined in the class and gives us the result.""" +if __name__ == "__main__": unittest.main() +TestCalculator.py +4 KB \ No newline at end of file diff --git a/calculator.py b/calculator.py index 3c85ead..bffd8a7 100644 --- a/calculator.py +++ b/calculator.py @@ -1,12 +1,137 @@ +import math class Calculator: def __init__(self): + memory = 0.0 pass 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 + + 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 + + + # added by KB + + 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: + """ + if (string_mode == 'bin'): + return str(bin(int(x)).replace("0b", "")) + + 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) + + 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 ln(self, x): + return math.log(x) + + def inv_ln(self,x): + return math.exp(x) + + #def logTen(self, self.state): + #return math.log10(self.state) + + 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 + -# add lots more methods to this calculator class. diff --git a/main-app.py b/main-app.py index a7cc4e2..61b8457 100644 --- a/main-app.py +++ b/main-app.py @@ -2,33 +2,520 @@ def getTwoNumbers(): - a = float(input("first number? ")) - b = float(input("second number? ")) + a = float(input("First Number? ")) + b = float(input("Second Number? ")) return a, b -def displayResult(x: float): - print(x, "\n") +def getOneNumber(): # get one number instead + a = float(input("Number? ")) + return a -def performCalcLoop(calc): +def switchDisplayModeInput(): # added by KB + """ + 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(): # added by KB + """ + 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('~' * 85) + print("1 : Addition \t\t 16 : Tangent") + print("2 : Subtraction \t 17 : Inverse sine") + print("3 : Multiplication\t 18 : Inverse cosine") + print("4 : Division \t\t 19 : Inverse tangent") + print("5 : Square \t 20 : Trig units mode - Convert Radians to Degrees") + print("6 : Square Root\t 21 : Trig units mode - Convert Degrees to Radians") + print("7 : Variable Exponentiation \t 22 : Factorial") + print("8 : Inverse of Display \t 23 : Log x") + print("9 : Invert Sign (+/-)\t 24 : 10 power x - Inverse nog") + print("10 : Switch Display\t 25 : Ln x - Natural log") + print("11 : M+ \t\t 26 : e power x - Inverse natural log") + print("12 : MC \t 27 : Pi") + print("13 : MRC \t 28 : Exponentiation constant") + print("14 : Sine\t 29 : Clear") + print("15 : Cosine 30 : Quit") + print('~' * 85) + temp_display = 0 + print("DISPLAY:") + print(temp_display) + + memory = 0.0 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.") + + while temp_display == 0: + + try: + choice = input("\nEnter number to choose your operation:\n") + + except: + print("Please enter a valid number.") + + if 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': # added by KB + # switch display + a = switchDisplayModeInput() + displayResult(calc.switchDisplayMode(a, temp_display)) + + elif choice == '11': # added by KB + displayResult(calc.madd(temp_display, memory)) + memory = calc.madd(temp_display, memory) + # return memory + + elif choice == '12': # added by KB + displayResult(calc.mclear()) + memory = calc.mclear() + # return memory + + elif choice == '13': # added by KB + displayResult(memory) + # displayResult(calc.mrecall(memory)) + # memory = calc.mrecall(memory) + # return memory + + elif choice == '14': # added by KB + 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': # added by KB + 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': # added by KB + 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': # added by KB + 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': # added by KB + 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': # added by KB + 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': # added by KB + x = getOneNumber() + temp_display = calc.trig_units_mode_deg_to_rad(x) + print(displayResult(temp_display), " radians") + + elif choice == '21': # added by KB + x = getOneNumber() + temp_display = calc.trig_units_mode_rad_to_deg(x) + print(displayResult(temp_display), " degrees") + + elif choice == '22': # added by KB + x = getOneNumber() + temp_display = calc.factorial(x) + print(displayResult(temp_display)) + + elif choice == '23': # added by KB + 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': # added by KB + x = getOneNumber() + temp_display = calc.inverse_log(x) + print(displayResult(temp_display)) + + elif choice == '25': # added by KB + x = getOneNumber() + temp_display = calc.ln(x) + print(displayResult(temp_display)) + + elif choice == '26': # added by KB + x = getOneNumber() + temp_display = calc.inv_ln(x) + print(displayResult(temp_display)) + + elif choice == '27': # added by KB + temp_display = calc.pi() + displayResult(temp_display) + + elif choice == '28': # added by KB + temp_display = calc.e() + displayResult(temp_display) + + elif choice == '29': + temp_display = 0 + displayResult(temp_display) + + elif choice == '30': + return + + elif temp_display == "Err": + if choice == '29': + temp_display = 0 + displayResult(temp_display) + + else: + temp_display = "Err" # TL Errors must be cleared before any other operation can take place + displayResult(temp_display) + + else: + print("That is not a valid input.") + displayResult(temp_display) + + while temp_display != 0: + + try: + choice = input("\nEnter number to choose your operation: \n") + + except: + print("Please enter a valid number.") + + if temp_display == "Err": + if choice == '29': + temp_display = 0 + displayResult(temp_display) + + else: + temp_display = "Err" # TL Errors must be cleared before any other operation can take place + displayResult(temp_display) + + elif choice == '1': + b = getOneNumber() + temp_display = calc.add(temp_display, b) + displayResult(temp_display) + + elif choice == '2': + b = getOneNumber() + temp_display = calc.sub(temp_display, b) + displayResult(temp_display) + + elif choice == '3': + b = getOneNumber() + temp_display = calc.mult(temp_display, b) + displayResult(temp_display) + + elif choice == '4': + b = getOneNumber() + if b == 0: + print("\nDISPLAY:\nErr") + else: + temp_display = calc.div(temp_display, b) + displayResult(temp_display) + + elif choice == '5': + temp_display = calc.sq(temp_display) + displayResult(temp_display) + + elif choice == '6': + temp_display = calc.sqrt(temp_display) + displayResult(temp_display) + + elif choice == '7': + b = getTwoNumbers() + temp_display = calc.varexp(temp_display, b) + displayResult(temp_display) + + elif choice == '8': + temp_display = calc.inverse(temp_display) + displayResult(temp_display) + + elif choice == '9': + temp_display = calc.invert_sign(temp_display) + displayResult(temp_display) + + elif choice == '10': # added by KB + # switch display + a = switchDisplayModeInput() + displayResult(calc.switchDisplayMode(a, temp_display)) + + elif choice == '11': # added by KB + displayResult(calc.madd(temp_display, memory)) + memory = calc.madd(temp_display, memory) + # return memory + + elif choice == '12': # added by KB + displayResult(calc.mclear()) + memory = calc.mclear() + # return memory + + elif choice == '13': # added by KB + displayResult(memory) + # displayResult(calc.mrecall(memory)) + # memory = calc.mrecall(memory) + # return memory + + elif choice == '14': # added by KB + a = trig_units_mode_input() + # x=getOneNumber() + x = temp_display + 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': # added by KB + a = trig_units_mode_input() + x = temp_display + # 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': # added by KB + a = trig_units_mode_input() + x = temp_display + # 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': # added by KB + 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': # added by KB + 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': # added by KB + 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': # added by KB + x = getOneNumber() + temp_display = calc.trig_units_mode_deg_to_rad(x) + print(displayResult(temp_display), " radians") + + elif choice == '21': # added by KB + x = getOneNumber() + temp_display = calc.trig_units_mode_rad_to_deg(x) + print(displayResult(temp_display), " degrees") + + elif choice == '22': # added by KB + x = getOneNumber() + temp_display = calc.factorial(x) + print(displayResult(temp_display)) + + elif choice == '23': # added by KB + 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': # added by KB + x = getOneNumber() + temp_display = calc.inverse_log(x) + print(displayResult(temp_display)) + + elif choice == '25': # added by KB + x = getOneNumber() + temp_display = calc.ln(x) + print(displayResult(temp_display)) + + elif choice == '26': # added by KB + x = getOneNumber() + temp_display = calc.inv_ln(x) + print(displayResult(temp_display)) + + elif choice == '27': # added by KB + temp_display = calc.pi() + displayResult(temp_display) + + elif choice == '28': # added by KB + temp_display = calc.e() + displayResult(temp_display) + + elif choice == '29': + temp_display = 0 + displayResult(temp_display) + + elif choice == '30': + return + + elif temp_display == "Err": + if choice == '29': + temp_display = 0 + displayResult(temp_display) + + else: + temp_display = "Err" # TL Errors must be cleared before any other operation can take place + displayResult(temp_display) + + else: + print("That is not a valid input.") + displayResult(temp_display) + # main start def main(): calc = Calculator() - performCalcLoop(calc) + performCalcLoop(calc, 0) #KB - passed second input parameter 0 print("Done Calculating.") if __name__ == '__main__': - main() + main() \ No newline at end of file