adding calculator class

This commit is contained in:
Mark McIntyre 2022-06-28 17:55:53 -04:00
parent 0b32fad38d
commit 10c73653cf

97
calculator.py Normal file
View File

@ -0,0 +1,97 @@
class Calculator():
def __init__(self):
self.total = 0.0
self.actions = []
def add(self, val1, val2=None):
running_total = self.total
if val2:
total = val1 + val2
print(total)
self.actions.append(f"{val1} + {val2}")
else:
total = val1
self.actions.append(f"{val1} + {running_total}")
self.total += total
self.actions.append(f" = {self.total}")
def sub(self, val1, val2=None):
running_total = self.total
if val2:
total = val1 - val2
print(total)
self.actions.append(f"{val1} - {val2}")
else:
total = val1
self.actions.append(f"{val1} - {running_total}")
self.total -= total
self.actions.append(f" = {self.total}")
def mult(self, val1, val2=None):
running_total = self.total
if val2:
total = val1 * val2
print(total)
self.actions.append(f"{val1} * {val2}")
else:
total = val1
self.actions.append(f"{val1} * {running_total}")
self.total *= total
self.actions.append(f" = {self.total}")
def div(self, val1, val2=None):
running_total = self.total
if val2:
total = val1 / val2
print(total)
self.actions.append(f"{val1} / {val2}")
else:
total = val1
self.actions.append(f"{val1} / {running_total}")
self.total /= total
self.actions.append(f" = {self.total}")
def pow(self, val1, val2=None):
running_total = self.total
if val2:
total = val1 ** val2
print(total)
self.actions.append(f"{val1} ** {val2}")
else:
total = val1
self.actions.append(f"{val1} ** {running_total}")
self.total = total ** self.total
self.actions.append(f" = {self.total}")
def log(self, val):
from math import log
print(log(val))
self.actions.append(f"log({val})")
# self.total = log(val) ** self.total
# self.actions.append(f" = {self.total}")
def showcalc(self):
for action in self.actions:
print(action)
def total(self):
return(self.total)
def ac(self):
self.total = 0.0
self.actions = []