Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CalculatorMemory.py
Original file line number Diff line number Diff line change
@@ -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



40 changes: 39 additions & 1 deletion calctests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from calculator import Calculator
from .calculator import Calculator


class TestStringMethods(unittest.TestCase):
Expand All @@ -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()
65 changes: 62 additions & 3 deletions calculator.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,71 @@
class Calculator:
import math

class Basic_func(object):

def __init__(self):
pass
self.options = {"1": "add"}

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.
163 changes: 148 additions & 15 deletions main-app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
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():
Expand All @@ -7,27 +13,154 @@ def getTwoNumbers():
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):
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.")
# 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():
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__':
Expand Down