From 6d93a14c85b8655987612ae800abcde83a1b1986 Mon Sep 17 00:00:00 2001 From: Apoorva Mishra Date: Sun, 8 Mar 2020 14:55:51 -0400 Subject: [PATCH 1/5] changes --- CalculatorMemory.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 CalculatorMemory.py diff --git a/CalculatorMemory.py b/CalculatorMemory.py new file mode 100644 index 0000000..e69de29 From 264f91346455069b5cd6e741261368f2f0f0ea41 Mon Sep 17 00:00:00 2001 From: Apoorva Mishra Date: Sun, 8 Mar 2020 14:59:05 -0400 Subject: [PATCH 2/5] changes --- calculator.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/calculator.py b/calculator.py index 3c85ead..4c8da4b 100644 --- a/calculator.py +++ b/calculator.py @@ -1,3 +1,5 @@ +import math + class Calculator: def __init__(self): @@ -7,6 +9,63 @@ def add(self, x, y): return x + y def sub(self, x, y): - return 0 + return x - y + + def mul(self, x, y): + return x * y + + def div(self, x, y): + return x / y + + def square(self, x): + return x * x + + def square_rt(self, x): + return math.sqrt(x) + + def inverse(self,x): + if x == 0: + return "ERR" + else: + return 1 / x + + def invert_sign(self, x): + return (-1) * x + + + def cal_sin(self, x, unit): + if unit == 2: + return math.asin(x) + elif unit == 1: + return math.sin(x) + + def cal_cosin(self, x, unit): + if unit == 2: + print("RAdian Value") + return math.acos(x) + elif unit == 1: + print("Degree Value") + return math.cos(x) + + def cal_tang(self, x, unit): + if unit == 2: + return math.atan(x) + elif unit == 1: + return math.tan(x) + + def inverse_sin(self, x, unit): + val = self.cal_sin(x, unit) + return 1/val + + def inverse_cosin(self, x, unit): + val = self.cal_cosin(x, unit) + return 1/val + + def inverse_tang(self,x, unit): + val = self.cal_tang(x, unit) + return 1/val + + + # add lots more methods to this calculator class. From 1bf86c5939a47d0314cd8cf3caca272bec6e6bc2 Mon Sep 17 00:00:00 2001 From: Apoorva Mishra Date: Sun, 8 Mar 2020 14:59:20 -0400 Subject: [PATCH 3/5] changes --- calctests.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/calctests.py b/calctests.py index 1964570..0bd37f9 100644 --- a/calctests.py +++ b/calctests.py @@ -1,5 +1,5 @@ import unittest -from calculator import Calculator +from .calculator import Calculator class TestStringMethods(unittest.TestCase): @@ -20,6 +20,44 @@ def test_sub(self): c = Calculator() self.assertEqual(c.sub(9, 3), 6) + def test_mul(self): + c = Calculator() + self.assertEqual(c.mul(6, 8), 48) + + + def test_div(self): + c = Calculator() + self.assertEqual(c.div(10, 2), 5) + + + def test_inverse(self): + c = Calculator() + self.assertEqual(c.inverse(10), 0.1) + + + def test_invert_sign(self): + c = Calculator() + self.assertEqual(c.invert_sign(10), -10) + + + def test_square(self): + c = Calculator() + self.assertEqual(c.square(10), 100) + + + def test_square_rt(self): + c = Calculator() + self.assertEqual(c.square_rt(4), 2) + + + + + + + + + + if __name__ == '__main__': unittest.main() From 7174a656ff70a301bfd25f1cec76bfeba43f068f Mon Sep 17 00:00:00 2001 From: Apoorva Mishra Date: Sun, 8 Mar 2020 14:59:29 -0400 Subject: [PATCH 4/5] changes --- main-app.py | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 100 insertions(+), 3 deletions(-) diff --git a/main-app.py b/main-app.py index a7cc4e2..ac0f3a0 100644 --- a/main-app.py +++ b/main-app.py @@ -1,30 +1,127 @@ from calculator import Calculator - def getTwoNumbers(): a = float(input("first number? ")) b = float(input("second number? ")) return a, b +def getOneNumber(): + a = float(input("Input number? ")) + return a + + +def switchDisplayUnitMode(displayUnitMode : str): + global switchUnit + if displayUnitMode == 'DE': + switchUnit = 1 + print("Degree") + elif displayUnitMode == 'RA': + switchUnit = 2 + print("Radians") + + print(switchUnit) + +def switchDisplayMode(displayMode : str): + global switch_display + if displayMode == 'B': + switch_display = 1 + print("Binary") + elif displayMode == 'O': + switch_display = 2 + print("Octal") + elif displayMode == 'H': + switch_display = 3 + print("HexaDecimal") + elif displayMode == 'D': + switch_display = 0 + print("Decimal") + + + + print(switch_display) + + def displayResult(x: float): - print(x, "\n") + + global switch_display + if switch_display == 1: + print("Approximate Binary Representation: "+bin(int(x)), "\n") + elif switch_display == 2: + print("Approximate Octal Representation: "+oct(int(x)), "\n") + elif switch_display == 3: + print("Approximate Hexadecimal Representation: "+hex(int(x)), "\n") + elif switch_display == 0: + + print(x, "\n") def performCalcLoop(calc): + global switchUnit while True: - choice = input("Operation? ") + choice = input("Operation ? ") if choice == 'q': break # user types q to quit calulator. elif choice == 'add': a, b = getTwoNumbers() displayResult(calc.add(a, b)) + elif choice == 'sub': + a, b = getTwoNumbers() + displayResult(calc.sub(a, b)) + elif choice == 'mul': + a, b = getTwoNumbers() + displayResult((calc.mul(a, b))) + elif choice == 'div': + a, b = getTwoNumbers() + displayResult(calc.div(a, b)) + elif choice == 'inverse': + a = getOneNumber() + displayResult(calc.inverse(a)) + elif choice == 'invert_sign': + a = getOneNumber() + displayResult(calc.invert_sign(a)) + elif choice == 'square': + a = getOneNumber() + displayResult(calc.square(a)) + elif choice == 'square_rt': + a = getOneNumber() + displayResult(calc.square_rt(a)) + elif choice == 'sdm': + displayMode = input(" Select Display Mode B:Binary, O:Octal, H:HexaDecimal, D:Decimal ") + switchDisplayMode(displayMode) + elif choice == 'cal_sin': + a = getOneNumber() + displayResult(calc.cal_sin(a, switchUnit)) + elif choice == 'cal_cosin': + a = getOneNumber() + displayResult(calc.cal_cosin(a, switchUnit)) + elif choice == 'cal_tang': + a = getOneNumber() + displayResult(calc.cal_tang(a, switchUnit)) + elif choice == 'inverse_sin': + a = getOneNumber() + displayResult(calc.inverse_sin(a, switchUnit)) + elif choice == 'stum': + displayUnitMode = input(" Select Display TRIG Mode DE:Degree, RA:Radians ") + switchDisplayUnitMode(displayUnitMode) + + + + + + + else: print("That is not a valid input.") + # main start def main(): + global switch_display + global switchUnit + switchUnit = 1 + switch_display = 0 calc = Calculator() performCalcLoop(calc) print("Done Calculating.") From ff35f1023fda11936229baf9d6f51c1450eee8f7 Mon Sep 17 00:00:00 2001 From: Apoorva Mishra Date: Thu, 26 Mar 2020 21:02:28 -0400 Subject: [PATCH 5/5] changes --- CalculatorMemory.py | 21 ++++++ calculator.py | 98 +++++++++++++------------- main-app.py | 166 +++++++++++++++++++++++++++----------------- 3 files changed, 171 insertions(+), 114 deletions(-) diff --git a/CalculatorMemory.py b/CalculatorMemory.py index e69de29..ed4f9e9 100644 --- a/CalculatorMemory.py +++ b/CalculatorMemory.py @@ -0,0 +1,21 @@ + + +class MemoryFn: + + def __init__(self, calcVal): + self.calcVal = calcVal + + + def resetMem(self): + self.calcVal = 0 + + + def getlastMemVal(self): + return self.calcVal + + + def setMemVal(self,displayedVal): + self.calcVal = displayedVal + + + diff --git a/calculator.py b/calculator.py index 4c8da4b..676144d 100644 --- a/calculator.py +++ b/calculator.py @@ -1,9 +1,9 @@ import math -class Calculator: +class Basic_func(object): def __init__(self): - pass + self.options = {"1": "add"} def add(self, x, y): return x + y @@ -17,53 +17,53 @@ def mul(self, x, y): def div(self, x, y): return x / y - def square(self, x): - return x * x - - def square_rt(self, x): - return math.sqrt(x) - - def inverse(self,x): - if x == 0: - return "ERR" - else: - return 1 / x - - def invert_sign(self, x): - return (-1) * x - - - def cal_sin(self, x, unit): - if unit == 2: - return math.asin(x) - elif unit == 1: - return math.sin(x) - - def cal_cosin(self, x, unit): - if unit == 2: - print("RAdian Value") - return math.acos(x) - elif unit == 1: - print("Degree Value") - return math.cos(x) - - def cal_tang(self, x, unit): - if unit == 2: - return math.atan(x) - elif unit == 1: - return math.tan(x) - - def inverse_sin(self, x, unit): - val = self.cal_sin(x, unit) - return 1/val - - def inverse_cosin(self, x, unit): - val = self.cal_cosin(x, unit) - return 1/val - - def inverse_tang(self,x, unit): - val = self.cal_tang(x, unit) - return 1/val + # def square(self, x): + # return x * x + # + # def square_rt(self, x): + # return math.sqrt(x) + # + # def inverse(self,x): + # if x == 0: + # return "ERR" + # else: + # return 1 / x + # + # def invert_sign(self, x): + # return (-1) * x + # + # + # def cal_sin(self, x, unit): + # if unit == 2: + # return math.asin(x) + # elif unit == 1: + # return math.sin(x) + # + # def cal_cosin(self, x, unit): + # if unit == 2: + # print("RAdian Value") + # return math.acos(x) + # elif unit == 1: + # print("Degree Value") + # return math.cos(x) + # + # def cal_tang(self, x, unit): + # if unit == 2: + # return math.atan(x) + # elif unit == 1: + # return math.tan(x) + # + # def inverse_sin(self, x, unit): + # val = self.cal_sin(x, unit) + # return 1/val + # + # def inverse_cosin(self, x, unit): + # val = self.cal_cosin(x, unit) + # return 1/val + # + # def inverse_tang(self,x, unit): + # val = self.cal_tang(x, unit) + # return 1/val diff --git a/main-app.py b/main-app.py index ac0f3a0..16dd83a 100644 --- a/main-app.py +++ b/main-app.py @@ -1,4 +1,11 @@ -from calculator import Calculator +from calculator import Basic_func +from . import CalculatorMemory + +def show_calc_mode_options(calc_options) -> None: + + for key, value in calc_options.items(): + print(f'{key}: {value}') + def getTwoNumbers(): a = float(input("first number? ")) @@ -56,75 +63,104 @@ def displayResult(x: float): print(x, "\n") -def performCalcLoop(calc): - global switchUnit - 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)) - elif choice == 'sub': - a, b = getTwoNumbers() - displayResult(calc.sub(a, b)) - elif choice == 'mul': - a, b = getTwoNumbers() - displayResult((calc.mul(a, b))) - elif choice == 'div': - a, b = getTwoNumbers() - displayResult(calc.div(a, b)) - elif choice == 'inverse': - a = getOneNumber() - displayResult(calc.inverse(a)) - elif choice == 'invert_sign': - a = getOneNumber() - displayResult(calc.invert_sign(a)) - elif choice == 'square': - a = getOneNumber() - displayResult(calc.square(a)) - elif choice == 'square_rt': - a = getOneNumber() - displayResult(calc.square_rt(a)) - elif choice == 'sdm': - displayMode = input(" Select Display Mode B:Binary, O:Octal, H:HexaDecimal, D:Decimal ") - switchDisplayMode(displayMode) - elif choice == 'cal_sin': - a = getOneNumber() - displayResult(calc.cal_sin(a, switchUnit)) - elif choice == 'cal_cosin': - a = getOneNumber() - displayResult(calc.cal_cosin(a, switchUnit)) - elif choice == 'cal_tang': - a = getOneNumber() - displayResult(calc.cal_tang(a, switchUnit)) - elif choice == 'inverse_sin': - a = getOneNumber() - displayResult(calc.inverse_sin(a, switchUnit)) - elif choice == 'stum': - displayUnitMode = input(" Select Display TRIG Mode DE:Degree, RA:Radians ") - switchDisplayUnitMode(displayUnitMode) - - - - - - - - else: - print("That is not a valid input.") +# def performCalcLoop(calc, memory): +# global switchUnit +# while True: +# choice = input("Operation ? ") +# if choice == 'q': +# break # user types q to quit calulator. +# elif choice == 'add': +# a, b = getTwoNumbers() +# calc.state = calc.add(a, b) +# displayResult(calc.state) +# elif choice == 'sub': +# a, b = getTwoNumbers() +# calc.state = calc.sub(a, b) +# displayResult(calc.state) +# elif choice == 'mul': +# a, b = getTwoNumbers() +# calc.state = calc.mul(a, b) +# displayResult(calc.state) +# elif choice == 'div': +# a, b = getTwoNumbers() +# calc.state = calc.div(a, b) +# displayResult(calc.state) +# elif choice == 'inverse': +# a = getOneNumber() +# calc.state = calc.inverse(a) +# displayResult(calc.state) +# elif choice == 'invert_sign': +# a = getOneNumber() +# calc.state = calc.invert_sin(a) +# displayResult(calc.state) +# elif choice == 'square': +# a = getOneNumber() +# calc.state = calc.square(a) +# displayResult(calc.state) +# elif choice == 'square_rt': +# a = getOneNumber() +# calc.state = calc.square_rt(a) +# displayResult(calc.state) +# elif choice == 'sdm': +# displayMode = input(" Select Display Mode B:Binary, O:Octal, H:HexaDecimal, D:Decimal ") +# switchDisplayMode(displayMode) +# elif choice == 'cal_sin': +# a = getOneNumber() +# displayResult(calc.cal_sin(a, switchUnit)) +# elif choice == 'cal_cosin': +# a = getOneNumber() +# displayResult(calc.cal_cosin(a, switchUnit)) +# elif choice == 'cal_tang': +# a = getOneNumber() +# displayResult(calc.cal_tang(a, switchUnit)) +# elif choice == 'inverse_sin': +# a = getOneNumber() +# displayResult(calc.inverse_sin(a, switchUnit)) +# elif choice == 'stum': +# displayUnitMode = input(" Select Display TRIG Mode DE:Degree, RA:Radians ") +# switchDisplayUnitMode(displayUnitMode) +# elif choice == 'reset': +# memory.resetMem() +# elif choice == 'getMem': +# displayResult(memory.getLatMemVal() +# else: +# print("That is not a valid input.") # main start def main(): - global switch_display - global switchUnit - switchUnit = 1 - switch_display = 0 - calc = Calculator() - performCalcLoop(calc) - print("Done Calculating.") + # global switch_display + # global switchUnit + # switchUnit = 1 + # switch_display = 0 + calc_mode_options = { + "1":"Basic" + } + basic_calc = Basic_func() + # performCalcLoop(calc) + # print("Done Calculating.") + calculator_mode = { + "1": basic_calc, + # 2: "Intermediate\n", + # 3: "Advanced\n" + } + print("Please choose a Mode: ") + show_calc_mode_options(calc_mode_options) + + + + user_input = input("Enter your choice of mode: ") + print("Enter function option: ") + show_calc_mode_options(calculator_mode[str(user_input)].options) + # global switch_display + # global switchUnit + # switchUnit = 1 + # switch_display = 0 + # calc = Calculator(0) + # memory = CalculatorMemory(0) + # performCalcLoop(calc, memory) + # print("Done Calculating.") if __name__ == '__main__':