diff --git a/calctests.py b/calctests.py index 1964570..ad01ead 100644 --- a/calctests.py +++ b/calctests.py @@ -2,24 +2,106 @@ 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() diff --git a/calculator.py b/calculator.py index 3c85ead..7d34b8c 100644 --- a/calculator.py +++ b/calculator.py @@ -1,3 +1,6 @@ +import math + + class Calculator: def __init__(self): @@ -7,6 +10,116 @@ 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 + + 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 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 -# add lots more methods to this calculator class. + def mclear(self): + memory=0 + return memory diff --git a/main-app.py b/main-app.py index a7cc4e2..78921f9 100644 --- a/main-app.py +++ b/main-app.py @@ -6,27 +6,272 @@ def getTwoNumbers(): b = float(input("second number? ")) return a, b +def getOneNumber(): + a = float(input("Number? ")) + return a -def displayResult(x: float): - print(x, "\n") +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 -def performCalcLoop(calc): while True: - choice = input("Operation? ") - if choice == 'q': - break # user types q to quit calulator. - elif choice == 'add': + + 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() - displayResult(calc.add(a, b)) + 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) -# main start def main(): calc = Calculator() - performCalcLoop(calc) + performCalcLoop(calc, 0) print("Done Calculating.")