diff --git a/calculator.py b/calculator.py index 909ab66..0275780 100644 --- a/calculator.py +++ b/calculator.py @@ -1,47 +1,201 @@ -class State: - """ - initiates a calculator object - """ + +import math + +class Calculator: def __init__(self): - self.value = 0 - self.command = None - self.display_mode = "decimal" - self.trig_mode = "rads" - self.display = "0" - self.user_entry = None - - def get_current_value(self): - pass - - def print(self): - if self.display_mode == "decimal": - self.display = str(self.value) - elif self.display_mode == "hex": - self.display = hex(self.value) - elif self.display_mode == "binary": - pass - elif self.display_mode == "octal": - pass - else: - self.error() - print("current display is:", self.display) - - def error(self): - self.value = None - self.command = None - self.display_mode = "Err" - - def add(self, n): - self.value = self.value + n - self.command = None - - def help(self): - print( - "You may enter a number or a command. They command will act on the displayed number, or wait for a second number to take action") - print( - "the commands are +, -, *, /, Square, SquareRoot, ^, Invert, ClearError, SwitchSign, Decimal, Binary, Octal, Hex, ToggleDisplayStyle, M+, MC, MRC, Sine, Cosine, Tangent, ASine, ACosine, ATangent, SwitchTrigMode, Rads, Degrees, !, Log, 10^x, Ln, e^x") - - def sub(self, n): - self.value = self.value - n - self.command = None + self.degrees = True + + + def add(self, a, b): + """ + ADDS TWO NUMBERS AND RETURNS RESULT + :param a: + :param b: + :return: + """ + print( a + b) + return a + b + + + def subtract(self, x, y): + """ + SUBTRACTS TWO NUMBERS AND RETURNS RESULT + :param x: + :param y: + :return: + """ + print( x - y) + return x - y + + + def multiply(self, x, y): + """ + MULTIPLIES TWO NUMBERS AND RETURNS RESULT + :param x: + :param y: + :return: + """ + print( x * y) + return y * x + + + def divide(self, x, y): + """ + DIVIDES TWO NUMBERS AND RETURNS RESULT + :param x: + :param y: + :return: + """ + print( x / y) + return x / y + + + def square(self, base): + """ + SQUARES A NUMBER AND RETURNS RESULT + :param base: + :return: + """ + print( base ** 2) + return base ** 2 + + + def exp(self, x, y): + """ + EXPONENTIATES TWO NUMBERS AND RETURNS RESULT + :param x: + :param y: + :return: + """ + print( x ** y) + return x ** y + + + def square_root(self, x): + """ + FINDS THE SQUARE ROOT OF A NUMBER AND RETURNS RESULT + :param x: + :return: + """ + print( x ** (1 / 2)) + return x ** (1 / 2) + + + def inverse(self, x): + """ + INVERSES A NUMBER AND RETURNS RESULT + :param x: + :return: + """ + print( 1 / x) + return 1 / x + + def deg_rad_swap(self): + """ + SWAPS BETWEEN RADIANS AND DEGREES + :return: + """ + self.degrees = not self.degrees + + def sin(self, x): + """ + FINDS SINE OF NUMBER AND RETURNS RESULT + :param x: + :return: + """ + if self.degrees: + x = math.radians(x) + return math.sin(x) + + def cosine(self, x): + """ + FINDS COSINE OF NUMBER AND RETURNS RESULT + :param x: + :return: + """ + if self.degrees: + x = math.radians(x) + return math.cos(x) + + def tangent(self, x): + """ + FINDS TANGENT OF NUMBER AND RETURNS RESULT + :param x: + :return: + """ + if self.degrees: + x = math.radians(x) + return math.tan(x) + + def inverse_sine(self, x): + """ + FINDS INVERSE SINE OF NUMBER AND RETURNS RESULT + :param x: + :return: + """ + if self.degrees: + x = math.radians(x) + return math.asin(x) + + def inverse_cosine(self, x): + """ + FINDS INVERSE COSINE OF NUMBER AND RETURNS RESULT + :param x: + :return: + """ + if self.degrees: + x = math.radians(x) + return math.acos(x) + + def inverse_tangent(self, x): + """ + FINDS INVERSE TANGENT OF NUMBER AND RETURNS RESULT + :param x: + :return: + """ + if self.degrees: + x = math.radians(x) + return math.atan(x) + + ##SECONDARY FORMULAS + + def add2(self, x): + """ + ADDS A NUMBER WITH THE SAME NUMBER + :param x: + :return: + """ + print( returned_result + x) + return returned_result + x + + + def subtract2(self, x): + """ + SUBTRACTS A NUMBER WITH THE SAME NUMBER + :param x: + :return: + """ + print( returned_result - x) + return returned_result - x + + + def multiply2(self, x): + """ + MULTIPLIES A NUMBER WITH THE SAME NUMBER + :param x: + :return: + """ + print( returned_result * x) + return returned_result * x + + + def divide2(self, x): + """ + DIVIDES A NUMBER WITH THE SAME NUMBER + :param x: + :return: + """ + print( returned_result / x) + return returned_result / x + diff --git a/main-app.py b/main-app.py index a7cc4e2..2e4670e 100644 --- a/main-app.py +++ b/main-app.py @@ -1,34 +1,305 @@ from calculator import Calculator +import math +import decimal +calc=Calculator() -def getTwoNumbers(): - a = float(input("first number? ")) - b = float(input("second number? ")) - return a, b +def enter_num(): + num1 = float(input()) + return num1 -def displayResult(x: float): - print(x, "\n") +def screen_options(): + print("1: Add 8: Inverse 15: M+") + print("2: Subtract 9: Sine 16: MC") + print("3: Multiply 10: Cosine 17: MRC") + print("4: Divide 11: Tangent 18: Exit") + print("5: Square 12: Inverse Sine") + print("6: Square Root 13: Inverse Consine") + print("7: Exponent 14: Inverse Tangent") + print("") + Operation = int(input('Choose an operation: (Select the number) ')) -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)) + memStore = None + + if Operation == 1: + print("Add") + print("Enter the first number: ") + x = enter_num() + print("Enter the second number: ") + y = enter_num() + result = calc.add(x, y) + return result + + elif Operation == 2: + print("Subtract") + print('Enter the first number: ') + x = enter_num() + print('Enter the second number: ') + y = enter_num() + result = calc.subtract(x, y) + return result + + elif Operation == 3: + print('Multiply') + print('Enter the first number: ') + x = enter_num() + print('Enter the second number: ') + y = enter_num() + result = calc.multiply(x, y) + return result + + elif Operation == 4: + print("Divide") + print("Enter the first number: ") + x = enter_num() + print("Enter the second number: ") + y = enter_num() + if y == 0: + print("ERROR") + return None else: - print("That is not a valid input.") + result = calc.divide(x, y) + return result + + elif Operation == 5: + print("Square") + print("Enter the number: ") + base = enter_num() + result = square(base) + return result + + elif Operation == 6: + print("Exponent") + print("Enter the base number: ") + x = enter_num() + print("Enter the exponent") + y = enter_num() + result = calc.exp(x, y) + return result + + elif Operation == 7: + print("Square Root") + print("Enter the number: ") + x = enter_num() + result = calc.square_root(x) + return result + + elif Operation == 8: + print("Inverse") + print("Enter the number: ") + x = enter_num() + result = calc.inv(x) + return result + +##TRIG FUNCTIONS +##must add statements for degrees/radians + + elif Operation == 9: + print("Sine") + print("Enter the number: ") + x = enter_num() + result = calc.sine(x) + return result + + + elif Operation == 10: + print("Cosine") + print("Enter the number: ") + x = enter_num() + result = calc.cosine(x) + return result + + + elif Operation == 11: + print("Tangent") + print("Enter the number: ") + x = enter_num() + result = calc.tangent(x) + return result + + + elif Operation == 12: + print("Inverse Sine") + print("Enter the number: ") + x = enter_num() + result = calc.invsine(x) + return result + + elif Operation == 13: + print("Inverse Cosine") + print("Enter the number: ") + x = enter_num() + result = calc.invcosine(x) + return result + + elif Operation == 14: + print("Inverse Tangent") + print("Enter the number: ") + x = enter_num() + result = calc.invtangent(x) + return result + +##MEMORY FUNCTIONS + + elif Operation == 15: + print("M+") + print("Enter the number: ") + memStore = float(input("Enter a value to store: ")) + print(memStore) + return memStore + + + elif Operation == 16: + print("MC") + if type(memStore) != None: + print(memStore) + + elif Operation == 17: + print("MCR") + store_choice = input("Do you want to clear memory? Y or N (case sensitive): ") + if store_choice == "Y": + memStore = 0 + else: + pass + print(memStore) + return memStore + + elif Operation == 18: + condi = False + print("Thank you for pushing my buttons!") + +##LOOPING OPERATIONS +###DEF FOR SECONDARY OPERATIONS + + +def choose_data_type(): + data_choice = int(input('Enter data type: 1. Decimal 2. Hexadecimal 3. Binary 4. Octal ')) + if data_choice == 1: + print(returned_result) + return returned_result + elif data_choice == 2: + print(hex(int(returned_result))) + return hex(int(returned_result)) + elif data_choice == 3: + print(bin(int(returned_result)).replace("0b", "")) + return bin(int(returned_result)).replace("0b", "") + elif data_choice == 4: + print(oct(int(returned_result))) + return oct(int(returned_result)) + + +def secondary_operation(): + print("1: Add 5: Square 9: M+") + print("2: Subtract 6: Square Root 10: MC") + print("3: Multiply 7: Exponent 11: MRC") + print("4: Divide 8: Inverse 12: Exit") + print("") + Operation = int(input('Choose an operation: ')) + + memStore = None + + if Operation == 1: + print("Add") + print("Enter number to add: ") + x = enter_num() + result = calc.add2(x) + return result + + elif Operation == 2: + print("Subtract") + print("Enter the number to subtract: ") + x = enter_num() + result = calc.subtract2(x) + return result + + elif Operation == 3: + print("Multiply") + print("Enter the second number: ") + x = enter_num() + result = calc.multiply2(x) + return result + + elif Operation == 4: + print("Divide") + print("Enter the second number: ") + x = enter_num() + result = calc.divide2(x) + return result + + elif Operation == 5: + print("Square") + result = square2(returned_result) + return result + + elif Operation == 6: + print("Exponent") + print("Enter the exponent: ") + x = enter_num() + result = calc.exp2(x) + return result + + elif Operation == 7: + print("Square Root") + result = calc.square_root2(returned_result) + return result + + elif Operation == 8: + print("Inverse") + result = inv2(returned_result) + return result + + elif Operation == 9: + memStore = returned_result + print(memStore) + return memStore + + elif Operation == 10: + if type(memStore) != None: + print(memStore) + else: + print('Empty') + + elif Operation == 11: + store_choice = input("Do you want to clear memory? Y or N (case sensitive): ") + if store_choice == "Y": + memStore = 0 + else: + pass + print(memStore) + return memStore + + elif Operation == 12: + condi = False + print('Goodbye!') +###################################################################### +## on screen ## -# main start -def main(): - calc = Calculator() - performCalcLoop(calc) - print("Done Calculating.") +print("Welcome to our Calculator!") +print("How can we help you?") +print("") -if __name__ == '__main__': - main() +condi = True +while condi: + returned_result = screen_options() + print(f"Result: {returned_result}") + choose_data_type() + print("") + cont = input("Continue with current value? Y or N (case sensitive): ") + print('') + if cont == "Y": + condi2 = True + while condi2: + returned_result = secondary_operation() + print(f"Result: {returned_result}") + choose_data_type() + print("") + cont2 = input("Continue with current value? Y or N (case sensitive): ") + print("") + if cont2 == "Y": + condi2 = True + else: + break \ No newline at end of file